5

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?

Schrödinger's Box
  • 3,218
  • 1
  • 30
  • 51

5 Answers5

3

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));
}
Sourabh
  • 413
  • 5
  • 17
2

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.

Schrödinger's Box
  • 3,218
  • 1
  • 30
  • 51
2

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.

0

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.

Charlie
  • 2,004
  • 6
  • 20
  • 40
  • Can you share a bit more about the rest that you need to do manually? Thanks. Please share your feedback with us via https://github.com/swagger-api/swagger-codegen/issues – William Cheng Apr 06 '16 at 04:47
-1

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

user3712978
  • 167
  • 2
  • 6