domaindrivendev / Swashbuckle.AspNetCore

Swagger tools for documenting API's built on ASP.NET Core
MIT License
5.24k stars 1.31k forks source link

Change field name swagger documentation #2766

Closed stone89son closed 6 months ago

stone89son commented 8 months ago

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:

    {
        public int MyProperty1 { get; set; }
        public int MyProperty2 { get; set; }
        public int MyProperty3 { get; set; }
    }

and swagger.json which was generated for it in POST method:

 "components": {
    "schemas": {
      "TestModel": {
        "type": "object",
        "properties": {
          "myProperty1": {
            "type": "integer",
            "format": "int32"
          },
          "myProperty2": {
            "type": "integer",
            "format": "int32"
          },
          "myProperty3": {
            "type": "integer",
            "format": "int32"
          }
        }
      }
    }
  }

How to change it to:

 "components": {
    "schemas": {
      "TestModel": {
        "type": "object",
        "properties": {
          "MyProperty1": {
            "type": "integer",
            "format": "int32"
          },
          "MyProperty2": {
            "type": "integer",
            "format": "int32"
          },
          "MyProperty3": {
            "type": "integer",
            "format": "int32"
          }
        }
      }
    }
  }
Havunen commented 8 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

stone89son commented 8 months ago

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.

Havunen commented 8 months ago

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 ?

stone89son commented 7 months ago

thank for support. I want to keep the defined C# property name unchanged. I want to in file swagger.json keep format.

not same. giaodien1 giaodien2

stone89son commented 7 months ago

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.

martincostello commented 6 months ago

Closing as resolved - arbitrary changes to the generated schemas can be made with schema filters (example).