Azure / azure-functions-openapi-extension

This extension provides an Azure Functions app with Open API capability for better discoverability to consuming parties
https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.OpenApi/
MIT License
370 stars 195 forks source link

OpenApi Document generated does not include the definition for nested class #406

Open baskarmib opened 2 years ago

baskarmib commented 2 years ago

Describe the issue OpenApi Document generation is not including the schema definition of the type of Array.

To Reproduce Steps to reproduce the behavior:

  1. With Visual Studio 2022, Create New Function App with HttpTrigger and OpenAPI template.
  2. Create a request entity which has basic fields and array fields.
  3. Add the required OpenAPI declarators to method and entities.
  4. Run the Function and Access OpenAPI document

Request Class -

[OpenApiExample(typeof(ParametersExample))]
    public class FunctionRequest
    {
        /// <summary>The body contains the array of html content to convert</summary>
        [OpenApiPropertyDescription("The body contains the array of html content to convert")]  
        public List<Body> body { get; set; }
    }

    [OpenApiExample(typeof(BodyExample))]
    public class Body 
    {

        /// <summary>The Content accepts the html string to convert to WML</summary>
        [OpenApiPropertyDescription("Content Field accepts string in html format")]
        public string Content { get; set; } 
    }

    public class ParametersExample : OpenApiExample<FunctionRequest>
    {
        public override IOpenApiExample<FunctionRequest> Build(NamingStrategy namingStrategy = null)
        {

            Body bodyExample = new Body();

            bodyExample.Content = "<html><body><p>This is a test</p></body></html>";
            Body bodyExample2 = new Body();

            bodyExample2.Content = "<html><body><p>This is a test</p></body></html>";
            this.Examples.Add(
                OpenApiExampleResolver.Resolve(
                    "ParametersExample",
                    new FunctionRequest()
                    {
                        body = new List<Body> { bodyExample, bodyExample2 }
                    },
                    namingStrategy
                ));

            return this;
        }

    }

    public class BodyExample : OpenApiExample<Body>
    {
        public override IOpenApiExample<Body> Build(NamingStrategy namingStrategy = null)
        {

            Body bodyExample = new Body();

            bodyExample.Content = "<html><body><p>This is a test</p></body></html>";

            this.Examples.Add(
                OpenApiExampleResolver.Resolve(
                    "BodyExample",
                    new Body()
                    {
                        Content = bodyExample.Content
                    },
                    namingStrategy
                ));

            return this;
        }
    }
 [OpenApiOperation(operationId: "Run")]
        [OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)]
        [OpenApiRequestBody(contentType: "application/json; charset=utf-8", bodyType: typeof(FunctionRequest), Description ="Sample Request",Required =true)]        
        [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json; charset=utf-8", bodyType: typeof(FunctionResponse), Description = "The OK response")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
        {

Expected behavior

Schemas should have body and convertedPayload "components": { "schemas": { "functionRequest": { "type": "object", "properties": { "body": { "type": "array", "items": { "$ref": "#/components/schemas/body" }, "description": "The body contains the array of html content to convert" } }, "example": "{\"body\":[{\"content\":\"

This is a test

\"},{\"content\":\"

This is a test

\"}]}" }, "functionResponse": { "type": "object", "properties": { "convertedPayload": { "type": "array", "items": { "$ref": "#/components/schemas/convertedPayload" } }, "errorCode": { "type": "string" }, "errorDescription": { "type": "string" } } } },

Environment (please complete the following information, if applicable):

Additional context It would be a great to add an example using nested class example.

rdhaundiyal commented 2 years ago

I was wondering if the same is case with inherited classes. I am inheriting a child class from base and using the typeof(childclass) in OpenApiRequestBody but getting empty object in swagger ui

baskarmib commented 2 years ago

I have not tested this with inherited classes but might be a related issue @rdhaundiyal . Looking forward to hear from the team.

eddedre commented 1 year ago

I'm curious on this item as well.