Jakub Urban
Jakub Urban

Reputation: 31

Swashbuckle - use custom JSON schema for operation

I have a specific case - I'm bulding an Azure Function endpoint that can take dynamic request payload, after that function is executed it takes the request body, and validates it against correct JSON schema that is stored somewhere in the storage. The schema is chosen based on the URL parameter.

Here's a snippet of the function:

[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(PublishResult))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(PublishResult))]
[FunctionName("Publish")]
public async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "post", Route = "publish/{eventType}")] HttpRequest req,
    [Blob("storage/{eventType}.json", FileAccess.Read)] Stream validationSchema,
    string eventType, ExecutionContext ctx)
{
    JObject jResult;
    (isValid, error, jResult) = await ValidateRequestObject(req, eventType, validationSchema);
    if (!isValid)
    {
        _log.LogWarning("Invalid object. {Id}, {ObjectError}", id, error);
        return new BadRequestObjectResult(new PublishResult(id, error));
    }

    // function logic
}

So my issue here is that I'd like to configure Swashbuckle so that it generates different request body based on these JSON schemas that I have. I can't figure out how to tell Swashbuckle to use my own custom JSON schemas for this.

I know that it should probably be done by implementing IOperationFilter but I can't really get it right. I've found few cases that are similar but not quite what I'm looking for.

Help would be much appreciated!

Upvotes: 3

Views: 1761

Answers (1)

krishg
krishg

Reputation: 6508

You might want to have a look at a community project (nuget) which lets you add different Swagger endpoint in function which you can leverage to create your custom swagger response by selecting document (you need to create the swagger/open api sepc document in json or yaml format). Note: There is no official extension for Function yet like regular asp.net core web app. This is a community driven project.

Upvotes: 1

Related Questions