【问题标题】:Swagger converting underscore to camelcaseSwagger 将下划线转换为驼峰式
【发布时间】:2021-08-20 17:58:09
【问题描述】:

所以,我在使用带有 Swagger(核心 1.5.7)的 XML 对象定义时遇到了问题。这是我的 XML 代码:

<result status="ok">
    <another>
        <cards/>
        <customer my_super_id="2349027834"/>
        <someothers>
            <someother name="who-is-this" />
        </someothers>
    </another>
</result>

这是我的 yml 代码:

result:
   type: object
   description: Some random description
   properties:
     status:
       type: string
       xml:
         attribute: true
     another:
       type: object
       properties:
         customer:
           type: object
           properties:
             superId:
               type: string
               xml:
                 name: my_super_id
                 attribute: true

我可以毫无问题地获得status,但my_super_idnull,因为Swagger 正在使用CamelCase 中的参数生成模型类,即mySuperId 而不是my_super_id。 在我生成的模型类中,我有这个: public ResultAnotherCustomer(@JsonProperty("mySuperId") final String mySuperId)

有没有办法解决这个问题?

【问题讨论】:

  • 当你说模型类在你的服务器代码中?谁具体抛出错误?是 XML 反序列化器吗?
  • 是的,Swagger在构建过程中生成的模型类。我在帖子中添加了其他信息,即生成的模型类中的 CamelCase 输出。
  • 也许我遗漏了一些东西 - 为什么你不能改变模型,让它显示@JsonProperty("my_super_id")?
  • 模型是根据我在 yml 文件中的内容自动生成的。我认为这是与 swagger-codegen 相关的错误,但我不知道为什么。
  • 是的,我的错误在于没有使用最新版本。感谢您的帮助!

标签: swagger


【解决方案1】:

虽然很愚蠢,但我似乎没有使用最新版本的swagger-codegen ,因此将其更新到最新版本 (2.1.5) 即可解决此问题。

【讨论】:

    【解决方案2】:

    我在使用 Swagger Open API 规范的 Java Springboot App 中遇到了同样的问题。我结束了覆盖一个 Bean 以使其工作。但这也是我在 Springboot 中使用 OAS 修复的方式。

    @Bean
    public ModelResolver modelResolver(ObjectMapper objectMapper) {
      return new ModelResolver(objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE));
    }
    

    【讨论】:

      【解决方案3】:

      我使用 Swagger 编码器生成器的经验是,它可以为我提供大约 80% 的我想要的东西,然后我必须自己完成其余的工作。如果您认为这是一个错误,可以在此处记录: https://github.com/swagger-api/swagger-codegen/issues

      Java 通常更喜欢 CamelCase 而不是 snake_case,所以我猜这是故意的。

      【讨论】:

      【解决方案4】:

      我在生成 swagger 客户端时也遇到了这个问题

      模型从蛇盒“aa_ba_ca”重命名为骆驼盒“AaBaCa”

      调用 api_instance.method(swagger_client.model(parameters)) 时未找到模型而出现错误

      错误: AttributeError 招摇模块没有属性

      【讨论】:

        猜你喜欢
        • 2015-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-01
        • 2020-01-08
        • 1970-01-01
        • 2011-07-08
        • 2016-08-07
        相关资源
        最近更新 更多