openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [JAVA] when using discriminator pattern the java generator creates a class with many properties instead of an interface

Open FinKingma opened this issue 3 years ago • 0 comments

Description

The issue seems to occur in the java generator start in version 5.0.0, and still occurs in the latest version 6.1.0. When using the discriminator pattern as described in the documentation, we no longer receive an interface object for the base class, but a class that contains all of the properties of all the OneOf objects. In the old version of the generator (4.3.1) an interface is generated, which is implemented by the classes generated by the oneOf's. I have omitted the generated annotations, since the problem lies in the java objects being generated.

4.3.1 situation
public interface PetResponse  {
    public String getPetType();
}
public class Lizard implements PetResponse {
...
5.0.0 and on (6.1.0 still occurs)
public class PetResponse {
public static final String JSON_PROPERTY_CAT_NAME = "catName";
  private String catName;

  public static final String JSON_PROPERTY_PET_TYPE = "petType";
  protected String petType;

  public static final String JSON_PROPERTY_BARK = "bark";
  private String bark;

  public static final String JSON_PROPERTY_LOVES_ROCKS = "lovesRocks";
  private Boolean lovesRocks;
...
public class Lizard {
  public static final String JSON_PROPERTY_LOVES_ROCKS = "lovesRocks";
  private Boolean lovesRocks;

  public static final String JSON_PROPERTY_PET_TYPE = "petType";
  private String petType;
....
openapi-generator version

This still worked in version 4.3.1 it broke in version 5.0.0 it still fails in 6.1.0

OpenAPI declaration file content or url
openapi: "3.0.3"
info:
  title: MS Pet API
  version: 1.0.0
  description: De API for testing the discriminator in openapi 6
paths:
  /api/projects:
    get:
      summary: get a pet
      operationId: getPets
      tags:
        - pets
      responses:
        200:
          description: Succes respons
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/PetResponse'

components:
  schemas:
    PetResponse:
      oneOf:
        - $ref: '#/components/schemas/Cat'
        - $ref: '#/components/schemas/Dog'
        - $ref: '#/components/schemas/Lizard'
      discriminator:
        propertyName: petType
    Cat:
      type: object
      # all other properties specific to a `Cat`
      required:
        - petType
      properties:
        catName:
          type: string
        petType:
          type: string
    Dog:
      type: object
      # all other properties specific to a `Dog`
      required:
        - petType
      properties:
        bark:
          type: string
        petType:
          type: string
    Lizard:
      type: object
      # all other properties specific to a `Lizard`
      required:
        - petType
      properties:
        lovesRocks:
          type: boolean
        petType:
          type: string
Generation Details

We use the java generator, with both the WebClient and RestTemplate libraries. We also use the spring generator, but the interface is generated correctly there, so the problem seems to lie in the java generator.

Steps to reproduce

clone the minimal reproduction repo: https://github.com/FinKingma/specUsingDiscriminator/blob/main/spec.yaml

run ./gradlew openApiGenerate

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/8495

FinKingma avatar Sep 21 '22 08:09 FinKingma