Azure / Azure/azure-functions-openapi-extension

Error generating swagger when using JsonProperty in class

Open
#326 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
388
Forks
202
PR merge metrics
No merged PRs in 30d

Description

**Describe the issue**
When using
[OpenApiRequestBody("application/json", typeof(CatInfo), Description = "See schema", Required = true)]
with class object

``` public static class HelloCatFunction
{
[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 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(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
![image](https://user-images.githubusercontent.com/25725877/143043593-8275ca54-0cec-42fa-ac8f-79ca4f491058.png)

**To Reproduce**
Steps to reproduce the behavior:
1. Create a Azure function using .net core 3.1
2. Creata a simple httptrigger and add OpenApiRequestBody (Im using nuget Microsoft.Azure.WebJobs.Extensions.OpenApi version 1.0.0)
3. Add a class and use newtonsoft: JsonProperty(propertyName: nameof(Name))]
Like the class above. Then add the object in typeof() in OpenApiRequestBody
4. 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.

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure with Microsoft.Azure.WebJobs.Extensions.OpenApi 1.0.0 using OpenApiRequestBody with CatInfo or CatFuture properties decorated by Newtonsoft.Json JsonProperty. Start at schema generation for the OpenApiRequestBody entry point and compare the generated schema with the Swagger UI failure. Done means the documented reproduction generates a schema and Swagger UI loads successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.