domaindrivendev / Swashbuckle.AspNetCore

Swagger tools for documenting API's built on ASP.NET Core
MIT License
5.25k stars 1.31k forks source link

Wrong JsonPatchDocument type reference in V6 #2029

Closed knoxi closed 3 years ago

knoxi commented 3 years ago

After creating the swagger schema, the patch type definition looks different and the generated client fails to compile. Is there a new behavior for the patch methods? Or are there new generator settings where we can handle the patch type?

V5.6.3

      "patch": {
        ...
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "array",
                "items": {
                  "$ref": "#/components/schemas/Operation"
                },
                "nullable": true
              }
            }
          }
        },
        ...

V6:

      "patch": {
        ...
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ValueDtoJsonPatchDocument"
              }
            }
          }
        },
        ...
domaindrivendev commented 3 years ago

Difficult to troubleshoot this without more info. Could you please create a minimal app (i.e. start from empty project) that repro's the issue so I can pull it down and troubleshoot? Thanks

knoxi commented 3 years ago

I have attached a simplified sample of our implementation which creates and saves the swagger file in the project folder ./Swagger/service_v1.json. There you can see that there is defined a custom type for patch but this one doesn't exists somewhere.

      "patch": {
        "tags": [
          "WeatherForecast"
        ],
        "summary": "PatchWeatherForecast",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "date-time"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/WeatherForecastJsonPatchDocument"
              }
            }
          }
        },

Sample.zip

poveilleux commented 3 years ago

I have encountered the same issue today upgrading from 5.6.3 to 6.1.1. I will have to wait until this is fixed before moving to the latest version.

poveilleux commented 3 years ago

I was able to fix the schema by doing the following MapType to basically mimic what version 5.6.3 was doing before.

options.MapType(typeof(JsonPatchDocument<>), () =>
{
    return new OpenApiSchema
    {
        Type = "array",
        Items = new OpenApiSchema
        {
            Type = "object",
            Properties =
            {
                ["value"] = new OpenApiSchema { Nullable = true },
                ["path"] = new OpenApiSchema { Type = "string", Nullable = true },
                ["op"] = new OpenApiSchema { Type = "string", Nullable = true },
                ["from"] = new OpenApiSchema { Type = "string", Nullable = true },
            },
            AdditionalPropertiesAllowed = false
        }
    };
});
domaindrivendev commented 3 years ago

Investigate further and confirmed this is a bug. The code was (incorrectly) assuming that ParameterInfo.ParameterType and the corresponding ModelMetadata.ModelType generated by the ApiExplorer would always have the same value and could be used interchangeably. However, in some special cases incl. IJsonPatchDocument, ApiExplorer will alter ModelMetadata.ModelType to reflect serialization behavior. So, the fix was to favor ModelMetadata.ModelType as per 95cb4d370e08e54eb04cf14e7e6388ca974a686e

domaindrivendev commented 3 years ago

Released with 6.1.2