azure-functions-openapi-extension
azure-functions-openapi-extension copied to clipboard
Error generating swagger when using JsonProperty in class
Describe the issue When using [OpenApiRequestBody("application/json", typeof(CatInfo), Description = "See schema", Required = true)] with class object
{
[FunctionName("HelloCatFunction")]
[OpenApiOperation(operationId: "Run", tags: new[] { "name" })]
[OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)]
[OpenApiParameter(name: "name", In = ParameterLocation.Path, Required = true, Type = typeof(string), Description = "The **Name** parameter")]
[OpenApiRequestBody("application/json", typeof(CatInfo), Description = "See schema", Required = true)]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(CatFuture), Description = "The OK response")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "cat/future/{name}")] HttpRequest req, string name,
ILogger log)
{
log.LogInformation($"Predict future for cat {name}");
try
{
var requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var catInformation = JsonConvert.DeserializeObject<CatInfo>(requestBody);
if (catInformation == null) return new BadRequestResult();
var futurePrediction = new CatFuture()
{
Name = catInformation.Name,
Age = catInformation.Age,
FuturePrediction = Prediction()
};
return new OkObjectResult(futurePrediction);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
using Newtonsoft.Json;
namespace Cat.Api
{
public class CatFuture
{
[JsonProperty(propertyName: nameof(Name))]
public string? Name { get; set; }
[JsonProperty(propertyName: nameof(Age))]
public int? Age { get; set; }
[JsonProperty(propertyName: nameof(FuturePrediction))]
public string FuturePrediction { get; set; }
}
}
Swagger ui fails to load
To Reproduce Steps to reproduce the behavior:
- Create a Azure function using .net core 3.1
- Creata a simple httptrigger and add OpenApiRequestBody (Im using nuget Microsoft.Azure.WebJobs.Extensions.OpenApi version 1.0.0)
- Add a class and use newtonsoft: JsonProperty(propertyName: nameof(Name))] Like the class above. Then add the object in typeof() in OpenApiRequestBody
- See error
Expected behavior Should work.
Screenshots If applicable, add screenshots to help explain your issue.
Environment (please complete the following information, if applicable):
- Azure function . net core 3.1
- Microsoft Visual Studio Professional 2019 Version 16.11.3
- Browser Chrome - Version 96.0.4664.45
- nuget Microsoft.Azure.WebJobs.Extensions.OpenApi version 1.0.0
Additional context Add any other context about the problem here.
Take a look at #181 @MatsVivanco , this may solve your problem!