Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CamelCase NamingStrategy for enums not working with System.Text.Json #1491

Closed
gobixm opened this issue Feb 8, 2022 · 0 comments
Closed

CamelCase NamingStrategy for enums not working with System.Text.Json #1491

gobixm opened this issue Feb 8, 2022 · 0 comments

Comments

@gobixm
Copy link
Contributor

gobixm commented Feb 8, 2022

CamelCase NamingStrategy for enums not working with System.Text.Json
NSwag version: 13.15.7

To reproduce:

using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc;
using NswagApi;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer()
    .AddOpenApiDocument(settings => settings.Title = "Title")
    .Configure<JsonOptions>(options =>
    {
        options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
    });

var app = builder.Build();

app.MapGet("/", () => new SampleDto
{
    Enum = SampleEnum.Foo
});
app.UseOpenApi();

app.Run();

Generated json:

"SampleEnum": {
        "type": "string",
        "description": "",
        "x-enumNames": [
          "Foo",
          "Bar"
        ],
        "enum": [
          "Foo",
          "Bar"
        ]
      }

Looks like it's somehow related to this code in SystemTextJsonUtilities.cs:

public static JsonSerializerSettings ConvertJsonOptionsToNewtonsoftSettings(dynamic serializerOptions)
        {
            var settings = new JsonSerializerSettings
            {
                ContractResolver = new SystemTextJsonContractResolver(serializerOptions)
            };

            if (((IEnumerable)serializerOptions.Converters).OfType<object>().Any(c =>
                c.GetType().IsAssignableToTypeName("System.Text.Json.Serialization.JsonStringEnumConverter", TypeNameStyle.FullName)))
            {
                settings.Converters.Add(new StringEnumConverter());
            }

            return settings;
        }

It simply creates StringEnumConverter without naming strategy.

RicoSuter added a commit that referenced this issue Feb 21, 2022
….Json (#1492)

* fix: system.text.json enum naming policy

* Update SystemTextJsonUtilities.cs

Co-authored-by: Rico Suter <mail@rsuter.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants