Skip to content

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

Closed
@DaveChamberlinKidd

Description

@DaveChamberlinKidd
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions