Search code examples
jsonswaggerasp.net-core-webapiswagger-uicamelcasing

camelCase in Swagger documentation


is it possible to change property names to camelCase in Swagger documentation? I'm using .NET Core 2.2 and Swashbuckle.AspNetCore 5.2.1. I've tried to use DescribeAllParametersInCamelCase() method in Startup.cs 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"
          }
        }
      }
    }
  }

?


Solution

  • I faced the same issue and here is my workaround :

    I implemented a custom filter (ISchemaFilter) that does the following

        public class CamelCasingPropertiesFilter : ISchemaFilter {
            public void Apply(OpenApiSchema model, SchemaFilterContext context)
            {
                model.Properties =
                    model.Properties.ToDictionary(d => d.Key.Substring(0, 1).ToLower() + d.Key.Substring(1), d => d.Value);
            }
        }
    

    There is maybe a more reliable solution I don't know. The matter is important as the openapi definition serves as a basis for generating api clients. openapi generator will build classes with PascalCase property names, which will be thrown by the api when used ...

    Hope it helps !