Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
          }
        }
      }
    }
  }

?

like image 599
mojajoja Avatar asked Jan 22 '26 23:01

mojajoja


1 Answers

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 !

like image 185
faysdev Avatar answered Jan 25 '26 12:01

faysdev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!