Skip to content

[BUG][JAVA (potentially all langs)] StackOverflowException with combination of allOf oneOf without explicit declaration of discriminator property in "properties" #15298

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
5 of 6 tasks
bear43 opened this issue Apr 24, 2023 · 1 comment · May be fixed by #15299

Comments

@bear43
Copy link

bear43 commented Apr 24, 2023

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

Previously related issue #8468 is actually fixed since version 6.0.0 but only if discriminator property is defined in "properties" explicitly. If the discriminator property definition is provided only in "discriminator"."propertyName" without any other information in "properties" then it leads to StackOverflowException during schemas introspection. The information should contain values of discriminator property or, at least, just an explicit declaration of the property. An example YAML in #8468 is completely covered the correct case without StackOverflowException.

But YAML without values of a discriminator property or even without declaration of discriminator property in "properties" is a valid OpenAPI specification (according to https://apitools.dev/swagger-parser/online/).

StackTrace without values of discriminator property or without declaration of discriminator property:

java.lang.StackOverflowError
	at java.util.regex.Pattern.error(Pattern.java:1969)
	at java.util.regex.Pattern.<init>(Pattern.java:1354)
	at java.util.regex.Pattern.compile(Pattern.java:1054)
	at java.lang.String.replace(String.java:2239)
	at org.openapitools.codegen.utils.ModelUtils.getSimpleRef(ModelUtils.java:401)
	at org.openapitools.codegen.utils.ModelUtils.getReferencedSchema(ModelUtils.java:802)
	at org.openapitools.codegen.DefaultCodegen.discriminatorFound(DefaultCodegen.java:3173)
	at org.openapitools.codegen.DefaultCodegen.discriminatorFound(DefaultCodegen.java:3206)
	at org.openapitools.codegen.DefaultCodegen.discriminatorFound(DefaultCodegen.java:3195)
	at org.openapitools.codegen.DefaultCodegen.discriminatorFound(DefaultCodegen.java:3206)
	at org.openapitools.codegen.DefaultCodegen.discriminatorFound(DefaultCodegen.java:3195)
	at org.openapitools.codegen.DefaultCodegen.discriminatorFound(DefaultCodegen.java:3206)
	at org.openapitools.codegen.DefaultCodegen.discriminatorFound(DefaultCodegen.java:3195)
openapi-generator version

Using generator versions from 6.0.0 to 6.5.0. Also the master branch version.

OpenAPI declaration file content or url
openapi: 3.0.2
info:
  title: Polymorphism Java Example
  version: 1.0.0
  contact:
    name: polydectes
    email: [email protected]
servers:
  - url: 'https://localhost'
tags:
  - name: Example
paths:
  '/animal':
    post:
      tags:
        - Example
      operationId: createAnAnimal
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Animal'
      responses:
        '200':
          $ref: '#/components/responses/200-OK'
        '400':
          $ref: '#/components/responses/400-BadRequest'
components:
  responses:
    200-OK:
      description: OK
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Animal'
    400-BadRequest:
      description: Error
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
  schemas:
    Animal:
      type: object
      oneOf:
        - $ref: '#/components/schemas/Cat'
        - $ref: '#/components/schemas/Dog'
      discriminator:
        propertyName: animalType
    Cat:
      type: object
      allOf:
        - $ref: '#/components/schemas/Animal'
      properties:
        acceptableAsHousepet:
          type: boolean
    Dog:
      type: object
      allOf:
        - $ref: '#/components/schemas/Animal'
      properties:
        canBeTrained:
          type: string
    OkResponse:
      type: object
      properties:
        id:
          type: string
    ErrorResponse:
      type: object
      properties:
        message:
          type: string
Generation Details
            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>6.5.0</version>
                <executions>
                    <execution>
                        <id>generate-poly-api</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>
                                ${project.basedir}/src/main/resources/specs/issue_spring_codegen_polymorphism.yaml
                            </inputSpec>
                            <modelPackage>poly.model</modelPackage>
                            <apiPackage>poly.api</apiPackage>
                            <generateSupportingFiles>false</generateSupportingFiles>
                            <generatorName>spring</generatorName>
                            <library>spring-cloud</library>
                            <configOptions>
                                <dateLibrary>java8-localdatetime</dateLibrary>
                                <serializableModel>true</serializableModel>
                                <interfaceOnly>true</interfaceOnly>
                                <useOneOfInterfaces>false</useOneOfInterfaces>
                                <hideGenerationTimestamp>true</hideGenerationTimestamp>
                                <useTags>true</useTags>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Steps to reproduce

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

Related issues/PRs

Bug issue #8468

Suggest a fix

Either remember visited schemas in disciminatorFound recursive method or add the demand of declaration discriminator property in "properties" with error log level.

private CodegenProperty discriminatorFound(String composedSchemaName, Schema sc, String discPropName, OpenAPI openAPI, Set<Schema> visitedSchemas) {...}
bear43 pushed a commit to bear43/openapi-generator that referenced this issue Apr 24, 2023
@bear43 bear43 mentioned this issue Apr 24, 2023
6 tasks
@bear43 bear43 changed the title [BUG][JAVA (potentially all langs)] [BUG][JAVA (potentially all langs)] StackOverflowException with combination of allOf oneOf without explicit declaration of discriminator property in "properties" Apr 24, 2023
bear43 added a commit to bear43/openapi-generator that referenced this issue May 16, 2023
bear43 added a commit to bear43/openapi-generator that referenced this issue May 16, 2023
bear43 added a commit to bear43/openapi-generator that referenced this issue May 16, 2023
@wing328
Copy link
Member

wing328 commented Oct 12, 2024

tested with the latest master and no longer seeing the exception:

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g spring --library spring-cloud -i /tmp/aa.yaml -o /tmp/go234 --openapi-normalizer REFACTOR_ALLOF_WITH_PROPERTIES_ONLY=true

happy to reopen the issue if needed

@wing328 wing328 closed this as completed Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants