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

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

Open
rsescu opened this issue May 22, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@rsescu
Copy link

rsescu commented May 22, 2024

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

@aykborstelmann
Copy link

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.

@bnasslahsen
Copy link
Contributor

bnasslahsen commented Jun 16, 2024

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

It's better to report it in the swagger-core project instead, so it's corrected in the source.

@bnasslahsen bnasslahsen added the enhancement New feature or request label Jun 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants