Skip to content

Allow customizing / disabling PolymorphicModelConverter #1334

Closed
@MHajoha

Description

@MHajoha

Is your feature request related to a problem? Please describe.
A request or response body referencing a polymorphic base class is represented in the generated OpenAPI spec using a oneOf of the inheritors of the base class, instead of the base class itself.

See https://github.com/MHajoha/springdoc-poly-demo for an example project. Springdoc currently generates both the request and response bodies of that example as:

"schema": {
  "oneOf": [
    {
      "$ref": "#/components/schemas/Impl1"
    },
    {
      "$ref": "#/components/schemas/Impl2"
    }
  ]
}

This strikes me as unnecessarily complicated and also seems to lose the discriminator information from BaseClass. Instead, I would like SpringDoc to generate:

"schema": {
  "$ref": "#/components/schemas/BaseClass"
}

This is what the model converter chain generates up until PolymorphicModelConverter, which unwraps BaseClass into the above oneOf

Describe the solution you'd like
Some simple way of either globally of on a per-model basis disabling the PolymorphicModelConverter, such as a spring boot property.

Describe alternatives you've considered
This problem can be worked around by replacing the PolymorphicModelConverter with one which simply delegates to the rest of the chain:

@Bean
public PolymorphicModelConverter noopPolymorphicModelConverter() {
	return new PolymorphicModelConverter() {

		@Nullable
		@Override
		public Schema<?> resolve(
				final AnnotatedType type,
				final ModelConverterContext context,
				final Iterator<ModelConverter> chain
		) {
			if (chain.hasNext()) {
				return chain.next().resolve(type, context, chain);
			} else {
				return null;
			}
		}
	};
}

But a better solution would be great, ideally on a per-model basis.

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