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

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

Closed
FinKingma opened this issue Sep 21, 2022 · 4 comments · Fixed by #15042 · May be fixed by #14284

Comments

@FinKingma
Copy link

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

#8495

@GithubUser8080
Copy link

Hi, did you find a solution for this? Is interface generation not possible at all now?

@igauravsehrawat
Copy link

Can someone help me find a solution to this bug?
Can I write some witty code in java and make this work?

@hvoss
Copy link

hvoss commented Mar 21, 2023

I also run into that issue.
Looking at the code I'm not quite sure what to do. Just enable property parsing (#13490) or enable the feature by default?!

@robbertvanwaveren
Copy link
Contributor

See my (approved) PR above.

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