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

[Java] Add proper support for allOf anyOf oneOf

Open MarvGilb opened this issue 6 years ago • 21 comments

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"
}

MarvGilb avatar Jul 24 '18 10:07 MarvGilb

Related issues:

  • oneOf: discussion take place in #15.
  • anyOf: #537, #559.
  • allOf: there are also some issues

jmini avatar Jul 24 '18 10:07 jmini

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

ghost avatar Aug 21 '18 07:08 ghost

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

mwoodland avatar Aug 30 '18 15:08 mwoodland

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.

tnmtechnologies avatar Sep 10 '18 09:09 tnmtechnologies

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

YeTingGe avatar Nov 01 '18 02:11 YeTingGe

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.

wing328 avatar Nov 01 '18 17:11 wing328

Any news?

fboucquez avatar Jul 29 '19 13:07 fboucquez

Any news?

jorgerod avatar Apr 17 '20 11:04 jorgerod

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

wing328 avatar Apr 17 '20 16:04 wing328

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

fantapop avatar Apr 26 '20 16:04 fantapop

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

wing328 avatar Jun 17 '20 16:06 wing328

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?

tr7zw avatar Nov 09 '21 19:11 tr7zw

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?

baczek00 avatar Nov 25 '21 22:11 baczek00

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.

sfuhrm avatar Apr 05 '22 15:04 sfuhrm

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

Thul95 avatar Sep 22 '22 13:09 Thul95

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

kaushik-revefi avatar Aug 02 '23 04:08 kaushik-revefi

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 avatar Sep 15 '23 03:09 wing328

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.

wing328 avatar Sep 15 '23 03:09 wing328

@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?

ayhanap avatar May 08 '24 19:05 ayhanap

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

wing328 avatar May 10 '24 14:05 wing328

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

ayhanap avatar May 10 '24 14:05 ayhanap