Nswag: How to set DefaultPropertyNameHandling in NSwag v12.3.1?

Created on 7 Jun 2019  路  6Comments  路  Source: RicoSuter/NSwag

Since the newest version of the Swagger this code (because of GeneratorSettings) is now obsolete:
``` C#
app.UseSwaggerUi(typeof(Startup).GetTypeInfo().Assembly, settings =>
{
settings.GeneratorSettings.DefaultPropertyNameHandling =
PropertyNameHandling.CamelCase;
});

Instead, we should use:
```  C#
services.AddSwaggerDocument(cfg =>
        {
           ...
        });

However, I have no idea where to set-up the camel case thing now. Can anyone help me with this?

done question

All 6 comments

DefaultPropertyNameHandling is deprecated because you should just change the json serializer so that the spec AND serializer work the same:

Will update the wiki soon with:

public class Startup
{
    ...

    public void ConfigureServices(IServiceCollection services)
    {
        // SetCompatibilityVersion is only needed for the UseSwagger() methods on ASP.NET Core >= 2.1
        services.AddMvc()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
            .AddJsonOptions(options =>
            {
                // Use camel case properties in the serializer and the spec
                options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                // Use string enums in the serializer and the spec
                options.SerializerSettings.Converters.Add(new StringEnumConverter());
            });

        // Add OpenAPI/Swagger document
        services.AddOpenApiDocument(); // registers a OpenAPI v3.0 document with the name "v1" (default)
        // services.AddSwaggerDocument(); // registers a Swagger v2.0 document with the name "v1" (default)
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseMvc();

        // Add OpenAPI/Swagger middlewares
        app.UseSwagger(); // Serves the registered OpenAPI/Swagger documents by default on `/swagger/{documentName}/swagger.json`
        app.UseSwaggerUi3(); // Serves the Swagger UI 3 web ui to view the OpenAPI/Swagger documents by default on `/swagger`

Using your code:

Sending request, and receiving JSON response look to me like properties are always lower cased. However, when opening Swagger UI in browser, looks like the property names are actually upper cased.

Annotation 2019-06-07 142843

Not sure if this is just UI bug, or the code is not working as intended, here's part of JSON code:

{
    "type": "integer",
    "name": "Size",
    "in": "query",
    "format": "int32",
    "maximum": 100.0,
    "minimum": 1.0,
    "x-nullable": false
}

Query params do not use the json serializer but can be renamed with the FromQuery(.. attribute

Alright, thanks a lot!

A question - what if we are not using Mvc ? For example, I am using NSwag in Azure Functions.

You should be able to change the contract resolver via the generator settings

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Peter554 picture Peter554  路  3Comments

akamyshanov picture akamyshanov  路  4Comments

saephraim picture saephraim  路  3Comments

rh78 picture rh78  路  3Comments

RawsomeGH picture RawsomeGH  路  4Comments