RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.68k stars 1.24k forks source link

OpenAPI doc generated from minimal APIs generates all schemas and not only the ones used #4639

Open paulomorgado opened 9 months ago

paulomorgado commented 9 months ago

I have this simple APIs:

var summaries = new[]
{
    "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecast", () =>
{
    var forecast = Enumerable.Range(1, 5).Select(index =>
        new WeatherForecast
        (
            DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
            Random.Shared.Next(-20, 55),
            summaries[Random.Shared.Next(summaries.Length)]
        ))
        .ToArray();
    return forecast;
})
.WithName("GetWeatherForecast")
.WithTags("Weather")
.WithOpenApi();

app.MapGet("/fortunes", () =>
{
    return "Good luck!";
})
.WithName("GetFortune")
.WithTags("Fortunes")
.WithOpenApi();

If I try to produce 2 documents (one for each tag):

builder.Services.AddOpenApiDocument(settings =>
{
    settings.DocumentName = "Weather";
    settings.OperationProcessors.Add(new OperationProcessor(ctx => ctx.OperationDescription.Operation.Tags.Contains("Weather")));
});

builder.Services.AddOpenApiDocument(settings =>
{
    settings.DocumentName = "Fortunes";
    settings.OperationProcessors.Add(new OperationProcessor(ctx => ctx.OperationDescription.Operation.Tags.Contains("Fortunes")));
});

I get this for the Fortunes API:

{
  "x-generator": "NSwag v13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v10.0.0.0))",
  "openapi": "3.0.0",
  "info": {
    "title": "My Title",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://localhost:7202"
    }
  ],
  "paths": {
    "/fortunes": {
      "get": {
        "tags": [
          "Fortunes"
        ],
        "operationId": "GetFortune",
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "WeatherForecast": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "date": {
            "type": "string",
            "format": "date"
          },
          "temperatureC": {
            "type": "integer",
            "format": "int32"
          },
          "summary": {
            "type": "string",
            "nullable": true
          },
          "temperatureF": {
            "type": "integer",
            "format": "int32"
          }
        }
      }
    }
  }
}

This happens for both 13.20.0 and 14.0.0-preview012.

One way of working around this has been remving the definitions afterwards:

builder.Services.AddOpenApiDocument(settings =>
{
    settings.DocumentName = "Fortunes";
    settings.OperationProcessors.Add(new OperationProcessor(ctx => ctx.OperationDescription.Operation.Tags.Contains("Fortunes")));
    settings.PostProcess = document =>
    {
        document.Definitions.Remove("WeatherForecast");
    };
});
paulomorgado commented 9 months ago

Looks like this was due to filtering out the operations after they had their parameters and responses processed.

Changed to this and it works:

builder.Services.AddOpenApiDocument(settings =>
{
    settings.DocumentName = "Weather";
    InsertTagSelectorOperationProcessor(settings.OperationProcessors, ctx => ctx.OperationDescription.Operation.Tags.Contains("Weather"));
});

builder.Services.AddOpenApiDocument(settings =>
{
    settings.DocumentName = "Fortunes";
    InsertTagSelectorOperationProcessor(settings.OperationProcessors,ctx => ctx.OperationDescription.Operation.Tags.Contains("Fortunes"));
});

// ...

static void InsertTagSelectorOperationProcessor(OperationProcessorCollection operationProcessors, Func<OperationProcessorContext, bool> func)
{
    var i = 0;
    while (i < operationProcessors.Count)
    {
        if (operationProcessors[i++] is OperationTagsProcessor)
        {
            break;
        }
    }

    if (i == operationProcessors.Count)
    {
        operationProcessors.Add(new OperationProcessor(func));
    }
    else
    {
        operationProcessors.Insert(i, new OperationProcessor(func));
    }
}