Bertus van Zyl
Bertus van Zyl

Reputation: 592

Turn off CamelCase for property names on c# objects

This is the DTO C# class:

public class WeatherForecast
        {
            public string DateFormatted { get; set; }
        }

This is what is generated in the swagger definition:

{"WeatherForecast":{"type":"object","properties":{"dateFormatted":{"type":"string"}}}}

The problem is that when I generate an XML example:

<?xml version="1.0" encoding="UTF-8"?>
<WeatherForecast>
    <dateFormatted>string</dateFormatted>
</WeatherForecast>

The problem is that when I send that XML back to the API, it does not populate the DateFormatted property. If I change the case so that it is no longer CamelCase (DateFormatted) it works properly, and the property is populated with "string".

How do I switch off CamelCase when generating the swagger definition?

Upvotes: 1

Views: 927

Answers (1)

bank balder
bank balder

Reputation: 21

In aspnet the camelcase properties name by default so you change Startup from

services.AddMvc();

to

services
        .AddMvc()
        .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

Reference: aspnet

Upvotes: 1

Related Questions