Swagger将下划线转换为驼峰字体

u3r8eeie  于 2022-11-06  发布在  其他
关注(0)|答案(5)|浏览(574)

因此,我在Swagger(Core 1.5.7)中使用XML对象定义时遇到了问题。

  1. <result status="ok">
  2. <another>
  3. <cards/>
  4. <customer my_super_id="2349027834"/>
  5. <someothers>
  6. <someother name="who-is-this" />
  7. </someothers>
  8. </another>
  9. </result>

下面是我的yml代码:

  1. result:
  2. type: object
  3. description: Some random description
  4. properties:
  5. status:
  6. type: string
  7. xml:
  8. attribute: true
  9. another:
  10. type: object
  11. properties:
  12. customer:
  13. type: object
  14. properties:
  15. superId:
  16. type: string
  17. xml:
  18. name: my_super_id
  19. attribute: true

我可以毫无问题地得到status,但是my_super_idnull,因为Swagger用CamelCase中的参数生成了模型类,即mySuperId而不是my_super_idpublic ResultAnotherCustomer(@JsonProperty("mySuperId") final String mySuperId)
有什么办法解决这个问题吗?

nhjlsmyf

nhjlsmyf1#

愚蠢的是,我似乎没有使用最新版本的swagger-codegen,所以更新到最新版本(2.1.5)修复了这个问题。

omqzjyyz

omqzjyyz2#

我在使用Swagger Open API规范的Java Springboot应用程序中遇到了同样的问题。我结束了覆盖一个Bean来使它工作。但这也是我在使用OAS的Springboot中修复的方法。

  1. @Bean
  2. public ModelResolver modelResolver(ObjectMapper objectMapper) {
  3. return new ModelResolver(objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE));
  4. }
vktxenjb

vktxenjb3#

我使用Swagger编码器生成器的经验是,它能让我得到我想要的80%,然后我必须自己做剩下的。如果你认为这是一个bug,你可以在这里记录它:https://github.com/swagger-api/swagger-codegen/issues
Java通常更喜欢CamelCase而不是snake_case,所以我猜这是故意的。

7qhs6swi

7qhs6swi4#

老主题了,但是我在直接从swagger.io生成typescript-angularjs时遇到了同样的问题。Snake -〉Camel转换。在生成代码时,modelPropertyNaming必须配置为snake_case,默认情况下是Camel。
https://github.com/swagger-api/swagger-codegen/wiki/FAQ#typescript
使用Docker的示例。

  1. docker pull swaggerapi/swagger-codegen-cli
  2. docker run --rm -v ${PWD}:/local swaggerapi/swagger-codegen-cli generate -i /local/swagger.yaml --additional-properties modelPropertyNaming=snake_case -l typescript-angularjs -o /local/typescript-angularjs/

如果能帮到你们的话,伙计们。太好了。

iq3niunx

iq3niunx5#

我在生成一个昂首阔步的客户端时也遇到了这个问题
将模型从snake case“aa_ba_ca”重命名为camel case“AaBaCa”
调用api_instance.method(swagger_client.model(parameters))时未找到模型,因此出错
错误:AttributeError swagger模块没有属性

相关问题