unchase / Unchase.Swashbuckle.AspNetCore.Extensions

:hammer: A library contains a bunch of extensions (filters) for Swashbuckle.AspNetCore.
https://www.nuget.org/packages/Unchase.Swashbuckle.AspNetCore.Extensions
Apache License 2.0
115 stars 16 forks source link

Swagger route shows enum names but response schema does not #11

Closed Brutiquzz closed 4 years ago

Brutiquzz commented 4 years ago

I have installed Unchase.Swashbuckle.AspNetCore.Extensions v2.3.8 Swashbuckle.AspNetCore v5.5.1

When using swagger I am trying to make it show me the enum names both when filling parameters but also when reading the response schema. Just like your sample picture for a SamplePerson title illustrates it.

However I am only getting the results you are showing for parameters but not for the response schema. image

I have pretty much copy pasted the code from your example. image

Can you tell me what I am missing or doing wrong?

unchase commented 4 years ago

Hi, @Brutiquzz Can you send me your generated specification json-file (or part of it) to spiritkola@hotmail.com? I will check what the problem is.

unchase commented 4 years ago

@Brutiquzz Can you try again with the latest version (v 2.3.10)?

hahn-kev commented 4 years ago

I'm also seeing this issue :( with version 2.3.10. Target framework core app 3.1, same swashbuckle version as OP.

unchase commented 4 years ago

I'm also seeing this issue :( with version 2.3.10. Target framework core app 3.1, same swashbuckle version as OP.

Hi, @hahn-kev

Can you show me your json-specification?

hahn-kev commented 4 years ago

I've edited it down a bit, to make it simpler for you and because I'm not sure how public it's allowed to be.

{
  "openapi": "3.0.1",
  "info": {
    "title": "API",
    "version": "v1"
  },
  "paths": {
    "/v1/payments": {
      "post": {
        "tags": [
          "Payments"
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/AuthorizePaymentBody"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/WebshopAuthorizePaymentResponse"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "AuthorizePaymentBody": {
        "required": [
          "Amount"
        ],
        "type": "object",
        "properties": {
          "amount": {
            "type": "number",
            "description": "Order amount",
            "format": "double"
          }
        },
        "additionalProperties": false
      },
      "PaymentErrorCode": {
        "enum": [
          0,
          1,
          2
        ],
        "type": "integer",
        "format": "int32",
        "x-enumNames": [
          "noError",
          "typeMissMatch",
          "unexpected"
        ]
      },
      "WebshopAuthorizePaymentResponse": {
        "type": "object",
        "properties": {
          "errorCode": {
            "$ref": "#/components/schemas/PaymentErrorCode"
          }
        },
        "additionalProperties": false,
        "example": {
          "ErrorCode": 0
        }
      }
    }
  }
}
unchase commented 4 years ago

Hi, @hahn-kev

I can't reproduce the issue with your json-specification:

success

Can you show your controller code?

hahn-kev commented 4 years ago

I can't as this is for a closed source project. What's serving the Swagger UI you showed in your screenshot? Looking at the versions object in the console my swagger UI version is 3.25 maybe you're testing with a newer version?

unchase commented 4 years ago

I used 3.26 version.

unchase commented 4 years ago

I can't as this is for a closed source project. What's serving the Swagger UI you showed in your screenshot? Looking at the versions object in the console my swagger UI version is 3.25 maybe you're testing with a newer version?

You can share only the part of controller code. For example, only the method signature.

hahn-kev commented 4 years ago

Sure, it's something like this Task<ActionResult<WebshopAuthorizePaymentResponse>> AuthorizePayment( [FromBody] AuthorizePaymentBody checkout)

unchase commented 4 years ago

@hahn-kev @Brutiquzz I created a repository with a test project for the issue, in which the description for the enum is displayed correctly. Hope it helps you.

ghost commented 4 years ago

I have a similar issue, please see attached swagger.txt swagger.txt

image


services.AddSwaggerGen(c =>
            {
                c.AddEnumsWithValuesFixFilters();
            });

and

 app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Internal DropShip API");
            });
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.1" />
<PackageReference Include="Unchase.Swashbuckle.AspNetCore.Extensions" Version="2.3.11" />
unchase commented 4 years ago

Hi, @IainCoSource

Should use:

services.AddSwaggerGen(c =>
{
    c.AddEnumsWithValuesFixFilters();

    var xmlFilePath = Path.Combine(AppContext.BaseDirectory, "<your_project_name>.xml");
    options.IncludeXmlComments(xmlFilePath);
});

with setting:

xml

hahn-kev commented 4 years ago

I thought we only needed to include that if we want to include XML comments, is that required for the Enum values?

ghost commented 4 years ago

@unchase I implemented the following

  var mvcXmlFilePath = Path.Combine(AppContext.BaseDirectory, "Pims.Mvc.xml");
  c.IncludeXmlComments(mvcXmlFilePath);

  var enumXmlFilePath = Path.Combine(AppContext.BaseDirectory, "Updater4.Data.xml");
  c.IncludeXmlComments(enumXmlFilePath);

The first one has added some comments, the second does nothing for the enums.

image

example of the compiler generated xml.

 <member name="F:Updater4.Data.Pims.DropShipOrderStatusType.SupplierError">
            <summary>
            Supplier Error.
            </summary>
            <remarks>IsError: true.</remarks>
        </member>
unchase commented 4 years ago

@Brutiquzz @hahn-kev @IainCoSource Fixed in v 2.3.12

You can use:

services.AddSwaggerGen(c =>
{
    c.AddEnumsWithValuesFixFilters();
});
ghost commented 4 years ago

@unchase :) New version is awesome

image