swagger-api / swagger-codegen-generators

Apache License 2.0
284 stars 424 forks source link

[Java] allOf discriminator property not declared #617

Closed rrockx-trifork closed 4 years ago

rrockx-trifork commented 4 years ago

When I use the following schema (see also: allOf example):

openapi: 3.0.0
paths:
  /pets:
    patch:
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/Cat'
                - $ref: '#/components/schemas/Dog'
              discriminator:
                propertyName: pet_type
      responses:
        '200':
          description: Updated
components:
  schemas:
    Pet:
      type: object
      required:
        - pet_type
      properties:
        pet_type:
          type: string
      discriminator:
        propertyName: pet_type
    Dog:     # "Dog" is a value for the pet_type property (the discriminator value)
      allOf: # Combines the main `Pet` schema with `Dog`-specific properties 
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Dog`
          properties:
            bark:
              type: boolean
            breed:
              type: string
              enum: [Dingo, Husky, Retriever, Shepherd]
    Cat:     # "Cat" is a value for the pet_type property (the discriminator value)
      allOf: # Combines the main `Pet` schema with `Cat`-specific properties 
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Cat`
          properties:
            hunts:
              type: boolean
            age:
              type: integer

to generate Java client code with the following command:

java -jar swagger-codegen-cli.jar generate -l java -i codegen/openapi.yaml -o allof-example

the classes Pet and Body contains invalid code:

public class Pet {
  @SerializedName("pet_type")
  private String petType = null;

  public Pet() {
    this.pet_type = this.getClass().getSimpleName();
  }
  public Pet petType(String petType) {
    this.petType = petType;
    return this;
  }

The property pet_type is not declared, resulting in compilation errors:

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/ramon/temp/java-oas2-oneof2/src/main/java/io/swagger/client/model/Body.java:[25,9] cannot find symbol
  symbol: variable pet_type
[ERROR] /home/ramon/temp/java-oas2-oneof2/src/main/java/io/swagger/client/model/Pet.java:[35,9] cannot find symbol
  symbol: variable pet_type

I used the latest version of the code generation client (3.0.18).

Maybe this is related to #291, #296 ?

HugoMario commented 4 years ago

fixed with #620. So, closing now. In case is still present please let me know and i'll work back on it asap.

sanjothmr commented 3 years ago

Same issue exists in swagger-codegen-cli-3.0.25, but works with older version swagger-codegen-cli-3.0.19 When can I expect a fix for latest version of codegen?