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

[BUG] [spring][server] oneOf Polymorphism support for spring boot #5381

Closed
dkirrane opened this issue Feb 20, 2020 · 5 comments · May be fixed by #5661
Closed

[BUG] [spring][server] oneOf Polymorphism support for spring boot #5381

dkirrane opened this issue Feb 20, 2020 · 5 comments · May be fixed by #5661

Comments

@dkirrane
Copy link

Description

Currently, openapi-generator doesn't support oneOf. It currently generates the following broken code, outputting UNKNOWN_BASE_TYPE rather than Pet for the example below.
The code is broken with or without the discriminator in the Spec.

    default ResponseEntity<Void> _petsPost(@ApiParam(value = ""  )  @Valid @RequestBody(required = false) UNKNOWN_BASE_TYPE UNKNOWN_BASE_TYPE) {
        return petsPost(UNKNOWN_BASE_TYPE);
    }
openapi-generator version

pom.xml

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>4.2.3</version>
    <executions>
        <execution>
            <id>generate</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
                <generatorName>spring</generatorName>
                <generateSupportingFiles>true</generateSupportingFiles>
                <configOptions>
                    <sourceFolder>src/gen/java/main</sourceFolder>
                    <library>spring-boot</library>
                    <java8>true</java8>
                    <interfaceOnly>true</interfaceOnly>
                    <skipDefaultInterface>false</skipDefaultInterface>
                    <delegatePattern>true</delegatePattern>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>
OpenAPI Spec

api.yaml

openapi: "3.0.2"
info:
  version: 1.0.0
  title: Polymorphism
paths:
  /pets:
    post:
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/Cat'
                - $ref: '#/components/schemas/Dog'
              discriminator:
                propertyName: pet_type
      responses:
        '200':
          description: created
components:
  schemas:
    # Parent
    Pet:
      type: object
      required:
        - pet_type      
      properties:
        pet_type:
          type: string
        eats:
          type: string
      discriminator:
        propertyName: pet_type
    # Child 1
    Dog:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Dog`
          properties:
            bark:
              type: boolean
    # Child 2
    Cat:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Cat`
          properties:
            climbs:
              type: boolean
Command line used for generation
Steps to reproduce
  • create a pom.xml with openapi-generator-maven-plugin 4.2.3
  • create api.yaml file above
  • mvn clean compile
  • see broken code under target directory \target\generated-sources\openapi\src\gen\java\main\org\openapitools\api\PetsApi.java
Related issues/PRs

This bug is related to the Server side Java code (SpringBoot)
@bkabrda has already merged PR for client Jackson Client side: #5120

Suggest a fix

Try apply client side fix from #5120 to server side.

@auto-labeler
Copy link

auto-labeler bot commented Feb 20, 2020

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@dkirrane
Copy link
Author

Line it's failing on DefaultCodegen.java#L5404

@bkabrda Any pointers on possible fix here? I assume SpringCodegen needs to postProcess the model in some way to configure the Base interface/class to use for each anyOf schema?

public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {

@bkabrda
Copy link
Contributor

bkabrda commented Mar 2, 2020

@dkirrane hey 👋 so in order to use the whole oneOf machinery I implemented, you'll need to do couple of things (your previous comment says "anyOf", but I think that's a typo and should have been "oneOf" - what I did only supports oneOf right now):

  • The generator needs to set useOneOfInterfaces = true (and since it's Java, you'll also have to add addOneOfInterfaceImports = true).
  • If the generator class (or any superclass of the generator class) overrides preprocessOpenAPI and/or postProcessAllModels, you need to make sure they call super.<theMethod> so that the whole machinery is actually called.
  • You'll likely also need to implement a addImportsToOneOfInterface method on the generator (take a look at JavaClientCodegen, I'd think that it would contain pretty much what you need, maybe you could even move it to AbstractJavaCodegen and get that shared for the server codegen).

Let me know if this helps. Thanks!

alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Mar 15, 2020
copy methods from OpenAPITools#5120 (oneOf support for jackson clients) to implement same in spring
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Mar 15, 2020
generate oneOf Class that has all properties from inherited classes
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Mar 15, 2020
fix property name of inherited model for oneOf
fix imports for oneOf
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Mar 15, 2020
create oneOf stuff only if useOneOfInterfaces is setted
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Mar 15, 2020
catch NPE if no OneOf Schemas is setted
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Mar 15, 2020
catch NPE if no OneOf Schemas is setted. merged from 5.0 branch

[Rust Server] Support header objects (OpenAPITools#5337)

[Rust Server] Support header objects

Support operations with objects in the header

[Rust Server] Support objects as query parameters (OpenAPITools#5338)

- Support objects as query parameters
- Update samples

 [Rust Server] Add support for untyped properties and models (OpenAPITools#5339)

* [Rust Server] Add support for untyped properties
* [Rust Server] Improve support for untyped data
* Update samples

[Rust Server] Support RFC 7807 (OpenAPITools#5407)

* Support RFC 7807 - Problem Details for HTTP APIs
* Add test for RFC 7807
* Update samples

[Rust Server] Nullable fixes (OpenAPITools#5408)

* [Rust Server] Nullable fixes
* [Rust Server] Add tests for nullable cases
* Update samples

[Rust Server] Handle numeric response description (OpenAPITools#5452)

* [Rust Server] Handle response descriptions which start with a number.
* [Rust Server] Add test for numeric response descriptions
* Update samples

[Rust Server] Support numeric operation IDs (OpenAPITools#5453)

* [Rust Server] Support operation IDs which begin with a number
* [Rust Server] Add test for a numeric operation ID
* Update samples

[Rust Server] Support RFC 7386 (OpenAPITools#5454)

* [Rust Server] Support RFC 7386

Support application/merge-patch+json as defined by RFC 7386 -
https://tools.ietf.org/html/rfc7386

Handle exactly the same as application/json.

* [Rust Server] Add test for RFC 7386

* Update samples

[Rust Server] Suffix reserved words with _ (OpenAPITools#5455)

* [Rust Server] Suffix reserved words with _

Suffix reserved words with an underscore instead of prefixing them.

This follows convention in the Rust community, as prefixing with an underscore
indicates an unused variable in Rust.

* Update samples

[Rust Server] Don't change API version (OpenAPITools#5458)

Don't change the API version which is exposed in `crate::API_VERSION`.

- If the API version isn't set, we don't expose it.

- If it is set, we leave it well alone.

- Always pass a package version:
 - Pass in the passed package version by preference
 - Pass in the API version if it's not.
   - If the API version isn't set, we use 1.0.0
   - If it is set, and isn't a valid semver, we append .0 such that we have
     something with three digits (so that an API version of 1 works).

[Rust Server] Support OpenAPI v3 callbacks (OpenAPITools#5405)

* [Rust Server] Support Callbacks

* [Rust Server] Support callbacks in the examples

* [Rust Server] Update features documentation

* [Rust Server] Mark as supporting callbacks

* Update samples

* [Rust Server] Add tests for callbacks

* [Rust Server] Fix README

Don't suggest examples which don't exist

[Rust Server] Test allOf objects including base properties (OpenAPITools#5457)

* [Rust Server] Add operationIds for rust-server-test

* [Rust Server] Add test for allOf

* Update samples

[Rust Server] Make parse error Display-able (OpenAPITools#5490)

* [Rust Server] Make parse error displayable

  Change error type to be displayable to prevent compile errors

* [Rust Server] Add test for enum in path

* Update samples
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Mar 15, 2020
copy methods from OpenAPITools#5120 (oneOf support for jackson clients) to implement same in spring
generate oneOf Class that has all properties from inherited classes
fix property name of inherited model for oneOf fix imports for oneOf
create oneOf stuff only if useOneOfInterfaces is setted
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Mar 15, 2020
fixed format
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Mar 22, 2020
added x-is-one-of-interface extension for oneOf interface in mustache
template
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Mar 22, 2020
fixed name of model from UNKNOWN_BASE_TYPE to right one in api: operationId + OneOf

Fix OpenAPITools#5381
parcelableModel is not required
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Mar 22, 2020
removed not needed methods
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Mar 22, 2020
catch NPE cases in preprocessOpenAPI
updated samples
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Mar 24, 2020
fixed generation of oneOf Models
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Mar 24, 2020
addOneOfInterfaceModel only for cases when useOneOfInterfaces is true and for spring
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Mar 24, 2020
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Mar 25, 2020
spring: fixed use of oneOf Models in API
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Mar 30, 2020
implementing oneOf for spring lib overriding methods with different behavior from default
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Mar 31, 2020
added x-is-one-of-interface extension for oneOf interface in mustache
template
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Mar 31, 2020
fixed name of model from UNKNOWN_BASE_TYPE to right one in api: operationId + OneOf

Fix OpenAPITools#5381
removed not needed methods

Fix OpenAPITools#5381
fixed generation of oneOf Models

Fix OpenAPITools#5381
addOneOfInterfaceModel only for cases when useOneOfInterfaces is true and for spring

Fix OpenAPITools#5381
NPE fix for tests
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Apr 1, 2020
fixed handing of composed schema with array
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Apr 1, 2020
fixed NPE in addOneOfInterfaceModel
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Apr 2, 2020
fixed generation of oneOf models with descriminator
@mmmsalo
Copy link

mmmsalo commented Jul 29, 2020

@bkabrda Hey! Do you know if there are any updates on this issue? It would be really nice to have this feature :)

@aollag09
Copy link

aollag09 commented Nov 5, 2020

Hello @bkabrda !! Would be great if your java client oneOf interface behaviour is extended to all java generators ...
Any update or by-pass available today ?
Thanks

alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Feb 4, 2021
alexsuperdev added a commit to alexsuperdev/openapi-generator that referenced this issue Feb 4, 2021
@falnyr falnyr mentioned this issue Sep 1, 2021
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment