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

[BUG][Spring] code generation fails if oneOf discriminator is enum

Open jan-mawh opened this issue 2 years ago • 8 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I want to limit the possible entries for the discriminator using oneOf. Therefore i want to set the discriminator as enum (See OpenAPI declaration file) below.

The openapi-generator-maven-plugin says: [WARNING] 'cargoParent' defines discriminator 'unit', but the referenced schema 'TONS' is incorrect. invalid type for unit, set it to string and the code finally fails to compile.

The Problem is that the generator creates the method

public TonsUnitEnum getUnit() {
    return unit;
  }

in the TONS.java class. But the CargoParents requires a method returning String:

public interface CargoParent {
    String getUnit();
}
openapi-generator version

openapi-generator-maven-plugin 7.7.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: test
  contact:
    email: [email protected]
  license:
    name: company Licence
    url: https://company.de/
  version: 0.1.1
servers:
  - url: https://webservice.company.org
paths:
  /doSomething:
    post:
      summary: test
      description: description for test operation
      operationId: doSomething
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/doSomethingRequest'
        required: true
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/doSomethingResponse'
components:
  schemas:
    doSomethingRequest:
      title: doSomethingRequest
      type: object
      properties:
        cargo:
          $ref: '#/components/schemas/cargoParent'
      required:
        - cargo
    doSomethingResponse:
      title: doSomethingResponse
      type: object
    TonsUnitEnum:
      type: string
      enum:
        - Tons
    TONS:
      title: TONS
      type: object
      properties:
        unit:
          $ref: '#/components/schemas/TonsUnitEnum'
        amount:
          type: number
          format: double
          example: 40
          minimum: 0
      required:
        - unit
        - amount
    KILOGRAMS:
      title: KILOGRAMS
      type: object
      properties:
        unit:
          type: string
          pattern: ^KILOGRAMS$
          example: KILOGRAMS
        amount:
          type: number
          format: double
          example: 1000
          minimum: 0
      required:
        - unit
        - amount
    cargoParent:
      title: cargoParent
      type: object
      oneOf:
        - $ref: '#/components/schemas/TONS'
        - $ref: '#/components/schemas/KILOGRAMS'
      discriminator:
        propertyName: unit
        mapping:
          TONS: '#/components/schemas/TONS'
          KILOGRAMS: '#/components/schemas/KILOGRAMS'

Related issues/PRs

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

jan-mawh avatar Jul 18 '24 09:07 jan-mawh

bye the way: when removing the enum from TonsUnitEnum everything works fine. So with:

TonsUnitEnum:
      type: string

code compiles perfectly

jan-mawh avatar Jul 18 '24 09:07 jan-mawh

bye the way: when removing the enum from TonsUnitEnum everything works fine. So with:

TonsUnitEnum:
      type: string

code compiles perfectly

But does the data type on your concrete class become a String after removing the enum from TonsUnitEnum? This is what happens for me - which is not ideal...

michael-adjaye-nutmeg avatar Sep 05 '24 14:09 michael-adjaye-nutmeg

But does the data type on your concrete class become a String after removing the enum from TonsUnitEnum? This is what happens for me - which is not ideal...

Yes it becomes a string - which is (as you already mentioned) not ideal. I would like it to be an enum. I figured out a workaround using a regex pattern (see example with KILOGRAMS) - but this is also not ideal:

    KILOGRAMS:
      title: KILOGRAMS
      type: object
      properties:
        unit:
          type: string
          pattern: ^KILOGRAMS$
          example: KILOGRAMS

Below is an even better example why i would like to use an enum. TONS and KILOGRAMS both include cargoGeneralParameter with an allOf. And cargoGeneralParameter has the unit discrimitator which should either be "TONS " or "KILOGRAMS " - that's why an enum would be much better than a string:

openapi: 3.0.3
info:
  title: test
  contact:
    email: [email protected]
  license:
    name: company Licence
    url: https://company.de/
  version: 0.1.1
servers:
  - url: https://webservice.company.org
paths:
  /doSomething:
    post:
      summary: test
      description: description for test operation
      operationId: doSomething
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/doSomethingRequest'
        required: true
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/doSomethingResponse'
components:
  schemas:
    doSomethingRequest:
      title: doSomethingRequest
      type: object
      properties:
        cargo:
          $ref: '#/components/schemas/cargoInterface'
      required:
        - cargo
    doSomethingResponse:
      title: doSomethingResponse
      type: object
    cargoInterface:
      oneOf:
        - $ref: '#/components/schemas/TONS'
        - $ref: '#/components/schemas/KILOGRAMS'
      discriminator:
        propertyName: unit
    TONS:
      allOf:
        - $ref: '#/components/schemas/cargoGeneralParameter'
        - type: object
          properties:
            amount:
              type: integer
          required:
            - amount
    KILOGRAMS:
      allOf:
        - $ref: '#/components/schemas/cargoGeneralParameter'
        - type: object
          properties:
            amount:
              type: number
              format: double
              example: 1000
              minimum: 0
          required:
            - amount
    cargoGeneralParameter:
      type: object
      properties:
        unit:
          type: string
          enum:
            - KILOGRAMS
            - TONS
        cargoId:
          type: integer
      required:
        - unit
        - cargoId

jan-mawh avatar Sep 06 '24 07:09 jan-mawh

Hey did anyone find a fix for this without needing regex?

mattmcc-attest avatar Feb 03 '25 12:02 mattmcc-attest

This bug is still present and leads to generated code that does not compile.

I am currently using a work-around whereby I post-process the generated code to ensure the methods in the sub-classes match the OneOf interface methods, e.g.

public TypeEnum getType() {
    return type;
}

Becomes

public String getType() {
    return type.getValue();
}

It's not pretty but it works.

Another work-around I'm considering (and I don't know if it's better) is to use a custom oneof_interface.mustache template where I have removed the discriminator getter. Effectively this creates an empty (sealed) interface, for example:

public sealed interface Entity permits SubType1, Subtype2 {

}

(With the right @JsonSubTypes and @JsonTypeInfo annotations of course)

I haven't fully tested that yet.

Ultimately of course it would be nice if the generator is able to handle this use-case and doesn't generate code that doesn't compile.

DennisPallett avatar Feb 24 '26 16:02 DennisPallett

Should have been resolved with https://github.com/OpenAPITools/openapi-generator/pull/24158, please get back to me if the issue persist

Mattias-Sehlstedt avatar Aug 27 '26 19:08 Mattias-Sehlstedt

@Mattias-Sehlstedt I just tested it with v7.25.0 and it still fails so the bug is still there unfortunately.

Btw I have switched to another work-around. I now use a custom oneof_interface.mustache template in my project whereby the discriminator getter has been removed in the interface.

DennisPallett avatar Sep 01 '26 14:09 DennisPallett

Ah I see the issue. I had not read the entire body of the reported issue in detail.

The issue here is similar to https://github.com/OpenAPITools/openapi-generator/issues/24769, where the children narrows an enum down to their isolated discriminator value. This then makes it impossible to have a parent that captures both unless you makes assumptions and either widen the enum that the children carries, or makes the discriminator String and then have the children express their enum as String values too. What I had fixed is for the more loose pattern where you allow the children to reference the entire parent and contain all discriminator values.

The linked issue is investigated how to best handle this, but in general I believe skipping the interface method entirely is the best current approach (which is also what you have done).

Mattias-Sehlstedt avatar Sep 01 '26 15:09 Mattias-Sehlstedt