Skip to content

[BUG] Having allOf ref back to the schema using oneOf causes infinite loop during Java code generation #8468

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

Closed
6 tasks
polydectes opened this issue Jan 19, 2021 · 3 comments

Comments

@polydectes
Copy link

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Given the Attached YAML file which is an OAS3 schema with:

  • a schema Animal of type object which may be oneOf Cat or Dog
  • Animal having 2 properties, one for Genus and one for Species
  • Animal having a discriminator on a propertyName of AnimalType
  • Animal having a property named AnimalType for the discriminator
  • The AnimalType discriminator having a mapping for both Cat and Dog
  • The AnimalType property within Animal being defined with an enumeration for CAT and DOG
  • Cat being an Animal has an allOf reference back to Animal
  • Dog being an Animal has an allOf reference back to Animal

When using the openapi code generator to create Java code

Then the openapi generator encounters a StackOverflowError:

Exception in thread "main" java.lang.StackOverflowError
at org.apache.commons.lang3.StringUtils.isBlank(StringUtils.java:298)
at org.apache.commons.lang3.StringUtils.isNotBlank(StringUtils.java:327)
at org.openapitools.codegen.DefaultCodegen.addProperties(DefaultCodegen.java:2957)
at org.openapitools.codegen.DefaultCodegen.addProperties(DefaultCodegen.java:2934)
at org.openapitools.codegen.DefaultCodegen.addProperties(DefaultCodegen.java:2959)
at org.openapitools.codegen.DefaultCodegen.addProperties(DefaultCodegen.java:2944)

Due to not excluding child/parent references which have already been touched during code generation.

In this case the generator identifies the following:

Create an Animal which is of type Cat.

  • Cat is an Animal so we must include all of Animal
    -- Animal is one of Cat or Dog. Start with Cat
    --- Cat is an Animal so we must include all of Animal
    ---- ...

Instead, the OneOf should be effectively ignored in favor of the allOf references.


The oneOf delaration identifies a downward view of polymorphism where we are looking from a parent to a child. AllOf reverses that view and looks from the child to the parent.

Using oneOf would be sufficient if oneOf caused all properties defined on the "parent" to be inherited by the child (i.e. the child is generated to extend the parent). Reading the spec implies (at least to me) that this is not the expected behavior.

Using allOf would be sufficient if the spec did not require the oneOf and a discriminator to properly deserialize requests into the API based on the discriminator property.

openapi-generator version

This has been identified in the 4.3 and 5.0 family of code generators.

OpenAPI declaration file content or url

see: https://gist.github.com/polydectes/b64c7ea89d391b8f003193082304717e for example YAML file

This Gist has been linted by the latest release of the OpenAPI SwaggerEditor.

Generation Details

generated using Maven plugin

  <plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>5.0.0-beta2</version>
    <executions>

      <execution>
        <id>animal</id>
        <goals>
          <goal>generate</goal>
        </goals>
        <configuration>
          <inputSpec>${project.basedir}/src/main/specs/AnimalTest.yml</inputSpec>
          <generatorName>spring</generatorName>
          <templateDirectory>${project.basedir}/src/main/specs/templates</templateDirectory>
          <configurationFile>${project.basedir}/src/main/specs/vehicle/config.json</configurationFile>
          <generateSupportingFiles/>
          <output>${project.build.directory}/generated-sources/swagger</output>
        </configuration>
      </execution>

    </executions>
  </plugin>
</plugins>

using generic moustache templates.

Steps to reproduce

calling "mvn clean compile" will cause the generation failure.

Related issues/PRs

Possible associated issue: #7414

Suggest a fix

The code generator for Java should either:

  1. have all the oneOf referenced schemas generate classes that extend the class containing the oneOF.

or

  1. Be intelligent enough to recognize the loop issue when a schema has an allOf reference contains a oneOf reference back to itself.
@polydectes
Copy link
Author

This appears to derived from this situation: #15 (comment)

@dipika154
Copy link

HI Team Any update on this? I am having similar issue when using allof and oneof spec together

@bear43
Copy link

bear43 commented Apr 22, 2023

It is repaired in next versions starting from 6.0.0.
At least, I can not reproduce this bug

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

4 participants