Closed stone89son closed 6 months ago
Are you using Pascal naming convention or why do you want to serialize them that way?
By default Swashbuckle serializes the contract using the same serialization settings what you have defined for you web api. If you are using Newtonsoft serialization for API you need to enable Swashbuckle.AspNetCore.Newtonsoft https://github.com/domaindrivendev/Swashbuckle.AspNetCore?tab=readme-ov-file#systemtextjson-stj-vs-newtonsoft
If you are using system.text.json serializer then you can define the Web API serialization settings in your Startup.cs file following way.
services
.AddControllers()
.AddJsonOptions(
(c) => c.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.KebabCaseLower
);
Similar approach works for Newtonsoft.
There is no option for PascalCase naming policy in STJ, I believe it is so uncommon so it is not implemented by default, but you could always implement it yourself or use some Nuget package.
Btw. There is newer version of Swashbuckle called DotSwashbuckle which is for .NET 8 and later. It has bunch of bugs fixes. https://github.com/Havunen/DotSwashbuckle?tab=readme-ov-file https://www.nuget.org/packages/DotSwashbuckle.AspNetCore
Thank you for your support, but that's not what I'm configuring. I'm configuring to make changes in the components section of the file at https://localhost:7126/swagger/v1/swagger.json. However, I don't know how to configure it and also don't see any instructions in the documentation.
The components section properties are serialized by default the same way you are serializing your API requests, this is why I'm asking are you using PascalCase naming convention in your API or why you want to make them UpperCase ?
thank for support. I want to keep the defined C# property name unchanged. I want to in file swagger.json keep format.
not same.
I have fixed it.
Add:
.AddJsonOptions(opt => opt.JsonSerializerOptions.PropertyNamingPolicy = null);
to keep the property names intact like in C#.
Thank you for taking the time to help.
Closing as resolved - arbitrary changes to the generated schemas can be made with schema filters (example).
is it possible to change property names in Swagger documentation(https://localhost:7126/swagger/v1/swagger.json)? I'm using .NET 8 and Swashbuckle.AspNetCore 6.4. I've tried search on google but nothing has changed. Help!
My model:
and swagger.json which was generated for it in POST method:
How to change it to: