RicoSuter / NSwag

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

ConfigureForNodaTime not map Type correctly #4841

Open darix795 opened 3 months ago

darix795 commented 3 months ago

After .NET 8 migration I move NSwag from v13 to v14. I have this Startup json api configuration:

.AddJsonOptions(options =>
{
    options.JsonSerializerOptions.Converters.Add(new DictionaryEnumConverter());
    options.JsonSerializerOptions.Converters.Add(
        new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
    options.JsonSerializerOptions.Converters.Add(new JsonDocumentCamelCaseConverter());
    options.JsonSerializerOptions.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
});

It seems that json converter ConfigureForNodaTime not work as expected on SchemaSettings, but work good on .NET Api json options. For example type Period not converted to string but as complex object similar to C# class. Nswag has a wrong behavior because correct conversion produce a string like described on NodaTime source code (https://github.com/nodatime/nodatime/blob/main/src/NodaTime/Text/PeriodPattern.cs#L70).

I write a workaround for this specific issue that map manually Period type:

if (document.SchemaSettings is SystemTextJsonSchemaGeneratorSettings schemaSettings)
{
    schemaSettings.TypeMappers.Add(new PrimitiveTypeMapper(typeof(Period), s =>
    {
        s.Type = JsonObjectType.String;
        s.Format = "period";
    }));
}

but i expect that ConfigureForNodaTime work fine like other converters for example JsonStringEnumConverter.