Swagger文档中的camelCase

问题描述 投票:1回答:1

是否可以在Swagger文档中将属性名称更改为camelCase?我正在使用.NET Core 2.2和Swashbuckle.AspNetCore 5.2.1。我尝试在Startup.cs中使用DescribeAllParametersInCamelCase()方法,但没有任何改变。帮助!

我的模特:

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

和在POST方法中为它生成的swagger.json:

 "components": {
    "schemas": {
      "TestModel": {
        "type": "object",
        "properties": {
          "MyProperty1": {
            "type": "integer",
            "format": "int32"
          },
          "MyProperty2": {
            "type": "integer",
            "format": "int32"
          },
          "MyProperty3": {
            "type": "integer",
            "format": "int32"
          }
        }
      }
    }
  }

如何将其更改为:

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

json swagger asp.net-core-webapi swagger-ui camelcasing
1个回答
0
投票
我实现了执行以下操作的自定义过滤器(ISchemaFilter)

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); } }

也许我不知道有更可靠的解决方案。这件事很重要,因为openapi定义是生成api客户端的基础。 openapi生成器将使用PascalCase属性名称构建类,使用时将由api抛出该属性名称...

希望有帮助!
© www.soinside.com 2019 - 2024. All rights reserved.