OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.4k stars 6.47k forks source link

Java enums are not generated correctly with anyOf #798

Open srividhyak27 opened 6 years ago

srividhyak27 commented 6 years ago

Using openapi-generator-maven-plugin 3.1.2

EventType:
  anyOf:
  - type: string
    enum:
      - "A"
      - "B"
      - "C"
      - "D"
  - type: string

Enum is generated as a class EventType and contains only the basic equals, toString and hashCode methods. The values not not available in the class.

jmini commented 6 years ago

Can you tell the generator and the library you are using?

srividhyak27 commented 6 years ago

I am using openapi-generator maven plugin, version 3.1.2 generator is jaxrs-jersey

Below is my configuration for maven plugin. [I have changed the actual package/yaml names below].

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>3.1.2</version>
    <executions>
            <id>Service-A</id>
        <goals>
            <goal>generate</goal>
        </goals>
        <configuration>                  
                         <inputSpec>${project.basedir}/src/main/resources/Service-Ayaml</inputSpec>
                        <inputSpec>${project.basedir}/src/main/resources/Service-B.yaml</inputSpec>                                                  
                         <inputSpec>${project.basedir}/src/main/resources/Schema-A.yaml</inputSpec>
            <generatorName>jaxrs-jersey</generatorName>
            <output>${project.basedir}/src-gen</output>
            <apiPackage>com.service.a.api</apiPackage>
            <modelPackage>com.service.a.model</modelPackage>
            <configOptions>
                <sourceFolder>src/main/java</sourceFolder>
            </configOptions>
        </configuration>
        </execution>

Thanks, Srividhya

wing328 commented 6 years ago

As a workaround, what about using the spec below instead?

EventType:
  - type: string
    enum:
      - "A"
      - "B"
      - "C"
      - "D"
sanamsarath commented 6 years ago

I am seeing similar issue for C++ enums, @wing328 will the above mentioned workaround works for C ++ as well ?? https://github.com/OpenAPITools/openapi-generator/issues/958

srividhyak27 commented 6 years ago

Tried removing the "anyOf" as suggested above. Get the below error

[ERROR] Failed to execute goal org.openapitools:openapi-generator-maven-plugin:3.2.3:generate (test.yaml) on project test: Execution test of goal org.openapitools:openapi-generator-maven-plugin:3.2.3:generate failed: There were issues with the specification. The option can be disabled via validateSpec (Maven/Gradle) or --skip-validate-spec (CLI). [ERROR] | Error count: 1, Warning count: 8 [ERROR] Errors: [ERROR] -attribute components.schemas.EventType is not of type object

Even after skipping validating with spec, the enum class is not generated.

Is there some other way around this ?

Thanks, Srividhya

mudged commented 5 years ago

Is there any update on this? I'm also seeing this issue when generating a java client (maven plugin v 4.0.0-beta)

FraneJelavic commented 5 years ago

Hi, Is there any update on this issue? This affects are enumerator creation from yamls, not only anyOf, allOf, oneOf... It would be very helpful to have this issue fixed. Thanks in advance, Frane

wackykid commented 4 years ago

similiar to #2906 , i am missing anyOf classes when i try to use openapi-generator for jaxrs-jersey generation.

i want to design my API to be able to return different object types depending on the kind of input. the ability to use oneOf or anyOf will be very useful to achieve that. however, the generator is unable to generate either oneOf or anyOf classes, as a result i have to use allOf to design my API.

would be very nice if this bug gets resolved.

asjp1970 commented 2 months ago

I have also bumped into such a spec and I wonder what kind of code is expected to be generated in this case. To me, the problem lies NOT in the code generator, but in what it is meant with such construct. My interpretation of it is that it means "well, I have a bunch of possible values but, at the same time I want this information element to be left open for future, or allow clients to send any other thing here"... Having that in mind I find it hard to leave up to the code generator how to interpret that. In our case, what we do, prior code generation, is to use the replacer plugin to change this:

EventType:
  anyOf:
  - type: string
    enum:
      - "A"
      - "B"
      - "C"
      - "D"
  - type: string

into this in the general case:

EventType:
  type: string

If we are 100% sure that the server needs to control, exactly, the possible values of EventType, because we want to return, for example, 404 if something unkown arrives, then the replacement is, as someone has suggested:

EventType:
  - type: string
    enum:
      - "A"
      - "B"
      - "C"
      - "D"

But IMHO putting this as a bug on the code generator is not fair. If one uses oneOf, or anyOf for pure inheritance of attributes or polimorphism, as the OpenAPI spec documents, then the generator works perfectly... the problem, as I see it is that it is even problematic in human language saying something like "I want to restrict this to some possible values (enum) but I also want to leave it open for everything (String)". I a typed language, or you go for one, or for the other.

So what I would suggest is to close this issue, since I've seen in too many places comments like "do this temporarily with enums until #798 is fixed"... but I wonder if it should be fixed at all.