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

[BUG] OneOf java classes not getting generated as part of codeGen in gradle plugin 4.0.0

Open samarthgupta437 opened this issue 6 years ago • 23 comments

When I generate sever stub for spec having OneOf tags, the code generation gradle task is successful , but in the generated models package OneOf*.java classes are missing.

I am using org.openapitools:openapi-generator-gradle-plugin:4.0.0 for "jaxrs-jersey" generatorName.

For example for below YAML file class "OneOfSofa1Sofa2" is not getting generated , but it is referred for variable "sofaStyle" in generated class Home.java

I have attached the YML as txt home.txt

Below gradle task was used: openApiGenerate { generatorName = "jaxrs-jersey" inputSpec = "$rootDir/src/main/resources/home.yml".toString() outputDir = "$rootDir".toString() apiPackage = "com.home.rest.resources" invokerPackage = "com.home.rest" modelPackage = "com.home.rest.model" modelFilesConstrainedTo = [] configOptions = [ dateLibrary: "java8" ] }

Screenshot of how it looks in editor

Screen Shot 2019-05-16 at 10 26 17 AM

samarthgupta437 avatar May 16 '19 03:05 samarthgupta437

OneOf classes also not being generated for the PHP client. The expected class name seems appropriate\ OneOfType1Type2Etc with each data type of reference name listed, but the classes are just not created.

I'm mentioning this in case it's a problem with the underlying spec parsing rather than the language generator.

This is in my spec for a response object property:

          "currentAuthorisation": {
            "oneOf": [
              {
                "$ref": "#/components/schemas/simpleAuthorisation"
              },
              {
                "type": "string",
                "nullable": true,
                "description": "null if there is no current authorisation"
              }
            ]
          },

The class not being generated is OneOfSimpleAuthorisationString (OneOf + SimpleAuthorisation + String) though the deserialiser is expecting it when currentAuthorisation is not null.

judgej avatar May 28 '19 18:05 judgej

For Java, I'm hitting the same issues using both the CLI and the gradle plugin: Oneof classes are not generated. Also the case for Python code, I'm guessing this is more of a general issue than a language-specific one.

audricschiltknecht avatar Jun 25 '19 16:06 audricschiltknecht

Facing the same problem with the java maven plugin: OneOf* classes are not generated, hence failing the compile task in the maven build.

ChintanSK avatar Aug 07 '19 13:08 ChintanSK

Same issue here.

alfirin avatar Sep 04 '19 08:09 alfirin

Same here.

Frontrider avatar Sep 23 '19 07:09 Frontrider

so when is this going to be resolved? I am using the latest openapi-generator and I too get this issue. luckily I am doing the design of the API so I can redesign my API to NOT use oneOf to avoid this. but i will love very much to implement oneOf as some of my APIs are designed to return different object types depending on the input.

wackykid avatar Oct 03 '19 08:10 wackykid

do we know when is it going to be resolved?

giannisla avatar Oct 04 '19 08:10 giannisla

Same for me for generating Java server.

gigaga avatar Oct 07 '19 08:10 gigaga

Same issue here. This is a critical blocker for using openapi generator in our project as we deal with polymorphic APIs.

Has anyone at least found a workaround for this?

Alex118 avatar Oct 11 '19 08:10 Alex118

A workaround is to create those classes manually.

Frontrider avatar Oct 11 '19 10:10 Frontrider

A workaround is to create those classes manually.

any examples on how these classes should look like..?

wackykid avatar Nov 08 '19 02:11 wackykid

Depends on your setup. We just extend one of the classes that it generated as the parent of the ones we need, then implement all of the unique parts. That will look like the class we need.

Frontrider avatar Nov 08 '19 09:11 Frontrider

Same issue here using the spring-cloud generator 4.0.0. Any plan to fix this or do you believe it is not meant to be fixed?

Seems to be a duplicate of https://github.com/OpenAPITools/openapi-generator/issues/634.

And as far as I have seen on this ticket everything related to one of should be in https://github.com/OpenAPITools/openapi-generator/issues/15

ghilainm avatar Nov 13 '19 16:11 ghilainm

I'm having the same issue with the jaxrs-cxf generator. Currently we are using the version 3.3.4. There the OneOf... Class doesn't get rendered. Instead Object is used. This works for our use case.

steeeve1510 avatar Nov 15 '19 10:11 steeeve1510

Same issue with a JHipster API-first development project, which uses: <openapi-generator-maven-plugin.version>4.1.3</openapi-generator-maven-plugin.version>

nickshoe avatar Jan 29 '20 13:01 nickshoe

It has seemingly fixed by this PR #5120

dbaltor avatar Feb 06 '20 22:02 dbaltor

We are using the openapi-generator-gradle plugin to do the generation, and seeing this issue too with version 4.3.0. Tried the 5.0.0-beta version (released June 2020) and still seeing this issue, so either that merge didn't make it in to either version, or it is not fixed.

bspies-work avatar Jul 15 '20 18:07 bspies-work

Im having some issue... using openapi-generator-maven-plugin at 4.3.1 version. So do u have any updates regarding this issue?

The merge was really made?

karllosernnesto-vakt avatar Aug 31 '20 11:08 karllosernnesto-vakt

Currently, only java client (jersey2 library) generator has better support for oneOf and anyOf. Please give it a try to see if you've any feedback on the implementation.

wing328 avatar Aug 31 '20 13:08 wing328

@wing328 I have tried using the jersey2 library, but I it is complaining that it is unable to find symbol: class JsonTypeInfo and class JsonSubTypes. I have the dependency:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.11.2</version>
</dependency>

The generated OneOf class doesn't seem to be adding the imports for them:

import java.util.Objects;
import java.util.Arrays;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import javax.validation.constraints.*;
import javax.validation.Valid;

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2020-09-22T14:51:06.694+01:00[Europe/London]")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "", visible = true)
@JsonSubTypes({
})

public interface TransactionsAllOfTransactionsOneOf  {
}

Any ideas?

rj93 avatar Sep 22 '20 13:09 rj93

I think we also had some import problems, but I'm not sure, as I'm not the one directly working with these.

Frontrider avatar Sep 23 '20 19:09 Frontrider

A (not type-safe) workaround is to enumerate the class names generated from oneOf types in the following command line switches when running the generator: --language-specific-primitives=OneOfTypeATypeB,OneOfTypeCTypeD --type-mappings=OneOfTypeATypeB=java.lang.Object,OneOfTypeCTypeD=java.lang.Object

thSoft avatar Jan 09 '21 14:01 thSoft

In 7.8.0, it is generating both classes for OneOf, means this is fixed ?

sann3 avatar Aug 26 '24 05:08 sann3