domaindrivendev / Swashbuckle.AspNetCore

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

JsonConverter applied to an enum property is not respected #1897

Closed sherlock1982 closed 5 months ago

sherlock1982 commented 3 years ago

I'm using Swashbuckle.AspNetCore and Swashbuckle.AspNetCore.Newtonsoft 5.6.3. Consider the following example:

public enum LicenseInfoReceivingStatus
{
    Success,
    OtherError,
}

public class LicenseFqdnInfo
{
    [JsonConverter(typeof(StringEnumConverter))]
    public LicenseInfoReceivingStatus Status { get; set; }
}

In this case Newtonsoft.Json will serialize this enum as string. Swashbuckle current behavior: In schema it will be reported as int enum

Swashbuckle expected behavior: In schema to be reported as string enum

As a workaround i can apply [JsonConverter(typeof(StringEnumConverter))] to a type itself but this is not correct in all cases. Another workaround will be to DescribeAllEnumsAsStrings() and this is also not always correct.

harvzor commented 3 years ago

Looks like this may be a duplicate of https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1459?

You're halfway there by installing Swashbuckle.AspNetCore.Newtonsoft.

You also need to add this line in your startup:

services.AddSwaggerGenNewtonsoftSupport();

Check the docs here for more details:

https://github.com/domaindrivendev/Swashbuckle.AspNetCore#systemtextjson-stj-vs-newtonsoft

github-actions[bot] commented 5 months ago

This issue is stale because it has been open for 60 days with no activity. It will be automatically closed in 14 days if no further updates are made.

github-actions[bot] commented 5 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.