Swagger converting underscore to camelcase

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?

Upvotes: 5

Views: 10189

Answers (5)

Mathieu Boivin
Mathieu Boivin

Reputation: 29

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.

Upvotes: 3

Sourabh
Sourabh

Reputation: 481

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

Upvotes: 3

user3712978
user3712978

Reputation: 167

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

Upvotes: -1

Stupid as it is, it seems I wasn't using the latest version of swagger-codegen , so updating it to the latest version (2.1.5) fixed the problem.

Upvotes: 2

Charlie
Charlie

Reputation: 2261

My experience working with the Swagger coder generator is that it gets me about 80% of what I want and then I have to do the rest myself. If you think this is a bug than you can log it here: https://github.com/swagger-api/swagger-codegen/issues

Java generally favors CamelCase over snake_case so I'm guessing that it's intentional.

Upvotes: 0

Related Questions