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] Add proper support for allOf anyOf oneOf #634

Open
MarvGilb opened this issue Jul 24, 2018 · 21 comments
Open

[Java] Add proper support for allOf anyOf oneOf #634

MarvGilb opened this issue Jul 24, 2018 · 21 comments

Comments

@MarvGilb
Copy link
Contributor

Description

In the openApiSpec it is possible to define a schema with properties, where one property can be defined directly or be using oneOf anyOf or allOf and passing the some provided Schemas.

Currently only allOf is considered and only the first of these schemas is taken (allOf requires all of them!). The other two delimiter are not even considered.

The following example makes it only possible to set the CustomProperties.

openapi-generator version

3.1.1

OpenAPI declaration file content or url

Sample Snippet.:

info:
  title: test
  version: 1.0.0
paths:
  /test:
    get:
      responses:
        '200':
          description: All good
          content:
            '*/*':
              schema:
                $ref: '#/components/schemas/ObjectWithProps'
      operationId: get
      summary: Get object with properties.
components:
  schemas:
    ObjectWithProps:
      properties:
        properties:
          allOf:
           - $ref: '#/components/schemas/CustomProperties'
           - $ref: '#/components/schemas/Properties'
      description: An object with difined and undifined keys.
    CustomProperties:
      description: The custom properties key.
      type: object
      properties:
        'someDefinedKey':
          type: object
          additionalProperties:
            type: object
    Properties:
      description: The wildcard properties key.
      type: object
      additionalProperties:
        type: object
openapi: 3.0.0
Steps to reproduce
java -jar ${PathToOpenApiGeneratorJAR3.1.1} generate \
 -i /path/swagger3.yaml \
 -o /path/swagger3-client \
 --api-package "com.123" \
 --model-package "com.123" \
 --group-id "com.123" \
 --artifact-id "123" \
 --artifact-version "SNAPSHOT" \
 -c ./java-options.json \
 -g java

JavaOptions:

{
  "java8": true,
  "dateLibrary": "java8"
}
@jmini
Copy link
Member

jmini commented Jul 24, 2018

@ghost
Copy link

ghost commented Aug 21, 2018

Looking forward to this feature ! I will be happy to help testing it if you need some help.

@mwoodland
Copy link
Contributor

I've just run into the same problem with the Spring generator.

@tnmtechnologies
Copy link
Contributor

I've just run into the same problem with the jaxrs-resteasy (3.2.0).
I will also be happy to help testing it if you need some help.

@YeTingGe
Copy link

YeTingGe commented Nov 1, 2018

I have the same issue, is there any plan to support "anyOf" tag?

@wing328
Copy link
Member

wing328 commented Nov 1, 2018

Hi folks, I've added better support of allOf, anyOf and oneOf via #1360. Please give it a try and let us know if you've any feedback. Clearly, some works need to be done in the Java client templates to better support oneOf and anyOf.

@fboucquez
Copy link

Any news?

@jorgerod
Copy link
Contributor

Any news?

@wing328
Copy link
Member

wing328 commented Apr 17, 2020

Heys, can you guys please try the branch "improve-java" (https://github.com/OpenAPITools/openapi-generator/tree/improve-java) ? I've only added oneOf support but anyOf support can be easily added if you guys are ok with my approach.

Example:

git clone https://github.com/OpenAPITools/openapi-generator
git checkout improve-java
mvn clean package -DskipTests
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java --library jersey2-experimental -i https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/oneOf.yaml -o /tmp/oneOfTest/ --skip-validate-spec

@fantapop
Copy link

@wing328 i don’t see the branch anymore. Did it get merged?

@wing328
Copy link
Member

wing328 commented Jun 17, 2020

Please check out the latest master: java generator with the jersey2library

@tr7zw
Copy link

tr7zw commented Nov 9, 2021

It's been 3 years now, and from what I can tell this is not supported by any client currently(even removed the(incomplete) feature from jersey2), so any hope that this will ever be supported? I see the Hacktoberfest label got added, but what happend to the ~4 different implementations that where in pr's/in the 4.3 releases?

@baczek00
Copy link

Hello,
If nothing changes and there are still problems, how do you (users) deal with this problem? Do you make a separate end point for each type?

@sfuhrm
Copy link

sfuhrm commented Apr 5, 2022

I had a workaround that was patching the generated sources. It was mapping the missing class references to java.util.Map instances.
It does

  • remove the import of the missing classes
  • replace references to the missing classes with references to java.util.Map

It worked with openapi-generator 5.2.0. Unfortunately this workaround does not work with newer versions of openapi-generator.

AnyOfXXX are the names of the missing AnyOf classes.

for clazz in AnyOfClusterCreateAttributemap AnyOfFolderCreateAttributemap AnyOfFolderUpdateAttributemap AnyOfHostCreateAttributemap AnyOfHostUpdateAttributemap; do
    echo "Replacing $clazz"
    find src -iname *.java | while read file; do
        sed -i -e"s#^import .*\.$clazz;#// removed import#" -e"s/$clazz/java.util.Map<Object, Object>/" $file
    done
done

Maybe someone finds this concept useful and comes up with a better way.
File post processing 1 would be one option here.

@Thul95
Copy link

Thul95 commented Sep 22, 2022

This is still an issue in the Java generator in version 6.1.0

@kaushik-revefi
Copy link

Still an issue with (openapi-generator-cli 6.6.0).

@wing328
Copy link
Member

wing328 commented Sep 15, 2023

I'd think in java the first AllOf could generate as an extends and the rest should maybe turn interfaces with getter/setter.

Have you tried the OpenAPI Normalizer with the rule REF_AS_PARENT_IN_ALLOF enabled?

@wing328
Copy link
Member

wing328 commented Sep 15, 2023

I have no idea how anyof would work in java.

anyOf should work in okhttp-gson and jersey2. Please give these a try when you've time.

@ayhanap
Copy link

ayhanap commented May 8, 2024

@wing328 I guess this works for okhttp-gson, jersey2 and jersey3. I am looking for resteasy support. Since both jersey and resteasy uses jackson, their models should be identical? Could we fix it for resteasy by just copying model mustaches to resteasy folder?

@wing328
Copy link
Member

wing328 commented May 10, 2024

@ayhanap would you like to contribute a PR for that?

@ayhanap
Copy link

ayhanap commented May 10, 2024

Sure, would give it a go when I can free some time.

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

No branches or pull requests