Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger converting underscore to camelcase

Tags:

swagger

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?

like image 469
Schrödinger's Box Avatar asked Nov 09 '22 18:11

Schrödinger's Box


2 Answers

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.

like image 108
Schrödinger's Box Avatar answered Jan 04 '23 02:01

Schrödinger's Box


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));
}
like image 21
Sourabh Avatar answered Jan 04 '23 03:01

Sourabh