Skip to content

Polymorphic fields on polymorphic parents don't get correct oneOf docs generatedΒ #2597

Closed

Description

@rsescu

Hello

Describe the bug

It seems that when having polymorphic types that contain attribute that are of polymorphic type themselves the generated docs bail on describing it with the oneOf schema.
Given the structure

AbstractParent - >
    ParentType1
        abstractChild
    ParentType2
    
AbstractChild -> 
    ChildType1
    ChildType2

Seems to generate the correct definitions if the AbstractChild class is used in the controller directly but it doesn't generate a ref to be used in ParentType1.

Here is a sample repo showcasing the issue: https://github.com/rsescu/springdoc-nested-oneof-issue

Seems different from #2575 since after downgrading to 2.3.0 the behaviour is the same.

To Reproduce
Steps to reproduce the behaviour:

Using
org.springframework.boot:3.2.5
org.springdoc:springdoc-openapi-starter-webmvc-ui:2.5.0

Have a abstract class parent that has two implementation. If one the the implementations uses an abstract child class field The definition for the Child class will not show it polymorphic but only as the base class with the field from the base class only.
Assuming the abstract base class is named AbstractChild and it's implementation are ChildType1 and ChildType2 I get the output:

"abstractChild": {
  "$ref": "#/components/schemas/AbstractChild"
}

Expected behavior

I would expect the output for the nested child to have a oneOf schema definition. It just contains a reference to the base class definition now.

"abstractChild": {
    "oneOf": [
      {
        "$ref": "#/components/schemas/ChildType1"
      },
      {
        "$ref": "#/components/schemas/ChildType2"
      }
    ]
  }

Repo https://github.com/rsescu/springdoc-nested-oneof-issue

Activity

  1. aykborstelmann commented on Jun 5, 2024

    @aykborstelmann

    As already mentioned in #2614:

    If verified that this is indeed a bug, I am willing to propose a solution.

    While digging through the code, I found that this is because io.swagger.v3.core.jackson.ModelResolver#resolveSubtypes in ModelResolver:1542 does create the AnnotatedType with (implicitly) resolveAsRef=false.

    Thus the ref is never set and thus resolvedSchema.get$ref() == null is false in PolymorphicModelConverter.java:79 and no oneOfs are resolved as expected.

    An easy fix would be:

        if (type.getParent() != null) {
          type.setResolveAsRef(true);
        }

    at the start of PolymorphicModelConverter#resolve.

  2. bnasslahsen commented on Jun 16, 2024

    @bnasslahsen
    Collaborator

    @aykborstelmann,

    Thanks for your investigation.
    The workaround creates regressions for test on spring-boot-starter-hateoas and spring-boot-starter-data-rest.
    You can run the tests to double check.

    I have added a fix for it, let's see how it goes :)

  3. added a commit that references this issue on Jul 2, 2024
    935b984
  4. added a commit that references this issue on May 17, 2025
    ed488a0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions