Swagger converting underscore to camelcase

Viewed 6577

So, I'm having trouble using an XML object definition with Swagger (Core 1.5.7). Here's my XML code:

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

And here's my yml code:

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

I can get the status with no problem, but the my_super_id is null because Swagger is generating the model class with the parameter in CamelCase, i.e., mySuperId instead of my_super_id. In my generated model class I have this: public ResultAnotherCustomer(@JsonProperty("mySuperId") final String mySuperId)

Is there any way to solve this?

5 Answers

I was facing the same issue in Java Springboot App using Swagger Open API Specification. I ended overidding a Bean to make it work. But again this is how I fixed in Springboot with OAS.

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

Old topic, but I had the same issue when generating typescript-angularjs directly from swagger.io. Snake -> Camel conversion. A modelPropertyNaming has to be configured to snake_case when generating code, by default it is Camel.

https://github.com/swagger-api/swagger-codegen/wiki/FAQ#typescript

Example using docker.

docker pull swaggerapi/swagger-codegen-cli

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/

If it can help some of you, fellows. It is great.

I also had this issue when generating a swagger client

The models were renamed from snake case 'aa_ba_ca' to camel case 'AaBaCa'

Error arose as no model was found when calling api_instance.method(swagger_client.model(parameters))

Error: AttributeError swagger module has no attribute

Related