Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java generator produces invalid code when discriminator object names contain underscore #9205

Closed
DaveChamberlinKidd opened this issue Feb 22, 2019 · 0 comments

Comments

@DaveChamberlinKidd
Copy link
Contributor

Description

In v2.4.0 a change was made which automatically populated the discriminator variable
with the class name. However whilst the variable name was converted from the model to
a java friendly variable, for example object_type to objectType in the parent class
the set variable didn't translate accordingly and so leaves uncompilable code.

As a result the generated code produced was...

  public ParentObject() {
    this.object_type = this.getClass().getSimpleName();
  }
  public ParentObject objectType(String objectType) {
    this.objectType = objectType;
    return this;
  }

and not

    this.objectType = this.getClass().getSimpleName();

There is a workaround which is that you specify your discriminator as the camel case version of the object but I assume this only works for the Java generator (and other generators that use camel case) it stops the swagger specification from being language independent.

Swagger-codegen version

v2.4.0+

Swagger declaration file content or url
swagger: '2.0'
info:
  description: Demo error
  version: 1.0.0
  title: Demo
host: flamangoes.co.uk
schemes:
  - https
consumes:
  - text/xml
produces:
  - application/json
paths:
  /v1/demo:
    post:
      summary: Demo
      parameters: []
      responses:
        '404':
          schema:
            type: array
            items:
              $ref: '#/definitions/Parent_Object'
definitions:
  Parent_Object:
    type: object
    required:
      - object_type
      - type
    discriminator: object_type
    properties:
      object_type:
        type: string
  Child_Object:
    type: object
    allOf:
      - $ref: '#/definitions/Parent_Object'
      - {}
      - {}
Command line used for generation

swagger-codegen generate -i -l java

Steps to reproduce

Run the Java code gen

Related issues/PRs

Issue was cause by commit 73c338b

Suggest a fix/enhancement

I've taken a fork of the project and have a fix that works, but as I don't know the codebase it may not be how you would want to fix it. I'll raise a PR so someone can take a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant