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

Java enums are not generated correctly with anyOf #798

Open
srividhyak27 opened this issue Aug 13, 2018 · 9 comments
Open

Java enums are not generated correctly with anyOf #798

srividhyak27 opened this issue Aug 13, 2018 · 9 comments

Comments

@srividhyak27
Copy link

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
Copy link
Member

jmini commented Aug 13, 2018

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

@srividhyak27
Copy link
Author

srividhyak27 commented Aug 13, 2018

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
Copy link
Member

wing328 commented Aug 13, 2018

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

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

@jmini jmini changed the title Java enums are not generated correctly Java enums are not generated correctly with anyOf Aug 14, 2018
@sanamsarath
Copy link

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
Copy link
Author

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
Copy link

mudged commented Jan 21, 2019

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
Copy link

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
Copy link

wackykid commented Oct 3, 2019

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
Copy link

asjp1970 commented Jul 23, 2024

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.

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

No branches or pull requests

8 participants