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

[REQ][JAVA] oneOf discriminator type string with enum defined #12412

Closed
tedberg opened this issue May 20, 2022 · 7 comments
Closed

[REQ][JAVA] oneOf discriminator type string with enum defined #12412

tedberg opened this issue May 20, 2022 · 7 comments

Comments

@tedberg
Copy link

tedberg commented May 20, 2022

Description

Using version 6 beta, oneOf is working well, generating POJOs. We get the interface with public String getType(); and the impl classes. The type we want to use had previously been defined as a string/enum, so it would be nice if we could define teh discriminator string to also be an enum.

openapi-generator version

6.0.0-beta

OpenAPI declaration file content or url
title: Type
type: string
default: "A"
enum:
  - "A"
  - "B"
  - "C"

Our discriminator is setup like this.

title: Example
type: object
discriminator:
  propertyName: type
  type: string
  mapping:
    A: './A.yaml'
    B: './B.yaml'
    C: './C.yaml'
oneOf:
  - $ref: './A.yaml'
  - $ref: './B.yaml'
  - $ref: './C.yaml'

The A object adds the type property, referencing the string with enum above.


title: A
type: object
properties:
  type:
    $ref: './Type.yaml'
...

If we remove the enum part, everything generates properly and works with type as a String object. Seeing that the mapping is essentially an enum itself, it would be nice, if the above would work, generating public Type getType(); in the interface. (It already generates nicely with enum in the implementing classes.)

Command line used for generation

Maven plugin,
<openapi.generator.maven.plugin.templateResourcePath>JavaSpring</openapi.generator.maven.plugin.templateResourcePath>

Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement
@pratiktiwari13
Copy link

Regarding this, the generator also generates code which is syntactically incorrect(the implmenting method inside the classes want to return an enum and the interface method wants to return a String, hence the clash).

@yekver
Copy link

yekver commented Oct 21, 2022

Are there is a chance that this issue will be solved?

bernie-schelberg-mywave added a commit to bernie-schelberg-mywave/openapi-generator that referenced this issue Oct 28, 2022
@ludovicianul
Copy link

Any plan on fixing this? Or an workaround that allows enums to be used?

@fedorenkobn
Copy link

fedorenkobn commented Aug 3, 2023

There is still a problem when modelNamePrefix and/or modelNameSuffix is used. The prefix and suffix is currently not considered

@ThomE2
Copy link

ThomE2 commented Oct 5, 2023

Hi there, does anyone know if this issue is actually closed ?
For me (using 7.0.1, useOneOfInterfaces set to true) the generated interface has a String getType(), which result in a compilation error Class is not abstract and does not override abstract method getType() in GeneratedInterface when I compile the generated client (the field type is an Enum in the generated concrete classes)

@bskorka
Copy link

bskorka commented Nov 22, 2023

Hey,
I have the same issue, code generated based on the docs (https://developer.vippsmobilepay.com/api/recurring/) leads to creating the interface with String return type while the subclasses are returning TypeEnum

...
    pricingResponse:
      oneOf:
        - $ref: '#/components/schemas/LegacyPricingResponse'
        - $ref: '#/components/schemas/VariableAmountPricingResponse'
      discriminator:
        propertyName: type
    LegacyPricingResponse:
      title: LegacyPricingResponse
      type: object
      required:
        - type
        - currency
        - amount
      properties:
        type:
          type: string
          description: The type of pricing. This decides which properties are present.
          enum:
            - LEGACY
            - VARIABLE
        currency:
          $ref: '#/components/schemas/Currency'
        amount:
          type: integer
          format: int32
          example: 1500
          description: >-
            The price of the agreement, present if type is LEGACY.


            Amounts are specified in minor units.

            For Norwegian kroner (NOK) that means 1 kr = 100 øre. Example: 499
            kr = 49900 øre.         
    VariableAmountPricingResponse:
      title: VariableAmountPricingResponse
      type: object
      required:
        - type
        - currency
        - suggestedMaxAmount
        - maxAmount
      properties:
        type:
          type: string
          description: The type of pricing. This decides which properties are present.
          enum:
            - LEGACY
            - VARIABLE
        currency:
          $ref: '#/components/schemas/Currency'
        suggestedMaxAmount:
          type: integer
          format: int32
          example: 30000
          description: >-
            The suggested max amount that the customer should choose, present if
            type is VARIABLE.


            Amounts are specified in minor units.

            For Norwegian kroner (NOK) that means 1 kr = 100 øre. Example: 499
            kr = 49900 øre.
        maxAmount:
          type: integer
          format: int32
          example: 30000
          description: >-
            The max amount chosen by the customer.


            Amounts are specified in minor units.

            For Norwegian kroner (NOK) that means 1 kr = 100 øre. Example: 499
            kr = 49900 øre.   
...

It looks like this task should not be closed or we have some regression bug. The version I'm using is 7.1.0.

@bskorka bskorka mentioned this issue Nov 22, 2023
6 tasks
@GuyT07
Copy link

GuyT07 commented Apr 18, 2024

You have to make sure you are defining the enum correctly (as component) and use it as type (add it as property). This works for me on version 7.5.0 where the type is correctly generated (enum instead of string).

Here a minimal example (should maybe be added to docs as well):

openapi: 3.0.3

info:
  description: API definition for example service
  version: 0.0.1
  title: example-service

paths:
  /api/example:
    get:
      operationId: getExample
      responses:
        '200':
          description: Return example
          content:
            application/json:
              schema:
                properties:
                  type:
                    $ref: "#/components/schemas/ExampleEnum"
                oneOf:
                  - $ref: "#/components/schemas/Example1"
                  - $ref: "#/components/schemas/Example2"
                discriminator:
                  propertyName: type
                  mapping:
                    Example1: "#/components/schemas/Example1"
                    Example2: "#/components/schemas/Example2"
components:
  schemas:
        ExampleEnum:
          type: string
          enum:
            - Example1
            - Example2
       SubExampleEnum:
          type: string
          enum:
            - SubExample1
            - SubExample2
            - SubExample3
       Example1:
         type: object
         properties:
         type:
          $ref: "#/components/schemas/ExampleEnum"
        examples:
          type: array
          items:
            properties:
              type:
                $ref: "#/components/schemas/SubExampleEnum"
            oneOf:
              - $ref: '#/components/schemas/otherRef'
              - $ref: '#/components/schemas/otherRef1'
              - $ref: '#/components/schemas/otherRef2'
            discriminator:
              propertyName: type
              mapping:
                SubExample1: '#/components/schemas/otherRef1'
                SubExample2: '#/components/schemas/otherRef2'
                SubExample3: '#/components/schemas/otherRef3'        
      required:
        - type
      discriminator:
        propertyName: type

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

Successfully merging a pull request may close this issue.

8 participants