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

The schema defined in the content of RequestBody ignores the setting of discriminator #356

Closed
lawli opened this issue Jan 20, 2020 · 5 comments
Assignees

Comments

@lawli
Copy link

lawli commented Jan 20, 2020

It seems in the method of getSchemaFromAnnotation in AnnotationsUtils, it does not consider copying discriminatorProperty and discriminatorMapping.

@bnasslahsen
Copy link
Collaborator

bnasslahsen commented Jan 21, 2020

Can you please add more details about your request :

  • What modules of springdoc-openapi are you using?
  • Provide with a sample code of HelloController to reproduce it?
  • What is the actual result using OpenAPI Description (yml or json)?
  • What is the expected result using OpenAPI Description (yml or json)?

Thank you in advance.

@lawli
Copy link
Author

lawli commented Jan 21, 2020

I am using the following annotation in the code

@operation(
description = "Sample",
requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
content = @content(schema = @Schema(
oneOf = {Debit.class, Credit.class},
discriminatorProperty = "transactionType",
discriminatorMapping = {
@DiscriminatorMapping(value = "DEBIT", schema = Debit.class),
@DiscriminatorMapping(value = "CREDIT", schema = Credit.class)
}
))
)
)

The expect result is

"requestBody": {
"content": {
"application/json": {
"schema": {
"discriminator": {
"propertyName": "transactionType",
"mapping": {
"DEBIT": "#/components/schemas/Debit",
"CREDIT": "#/components/schemas/Credit"
}
},
"oneOf": [
{
"$ref": "#/components/schemas/Debit"
},
{
"$ref": "#/components/schemas/Credit"
}
]
}
}
}
}

But the actual result is

"requestBody": {
"content": {
"application/json": {
"schema": {
"oneOf": [
{
"$ref": "#/components/schemas/Debit"
},
{
"$ref": "#/components/schemas/Credit"
}
]
}
}
}
}

While I debug into the code, the requestbody is constructed in org.springdoc.core.RequestBodyBuilder and the Schema of the Content is extracted from method getSchemaFromAnnotation in class io.swagger.v3.core.util.AnnotationsUtils

@bnasslahsen
Copy link
Collaborator

Hi @lawli,

As you have described, this behaviour is mainly related to swagger-api.
The main root cause issue, is the following:

You can look at the sample how to generate the discriminator in the documentation.
Your @Schema annotation, just needs to be added on the superclass.

The following issue, contains an example of how to get the discriminator in the documentation:

@lawli
Copy link
Author

lawli commented Jan 21, 2020

I don't think it is the problem of swagger-api. If I populate the ComposedSchema manually, it shows the expected result.

public OpenApiCustomiser apiCustomerser() {
    return openApi -> {
        MediaType md = new MediaType();
        Discriminator ds = new Discriminator();
        ds.setPropertyName("transactionType");
        ds.mapping("DEBIT", "#/components/schemas/Debit");
        ds.mapping("CREDIT", "#/components/schemas/Credit");
        ComposedSchema sc = new ComposedSchema();
        List oneOf = Arrays.asList(
                new Schema().$ref("#/components/schemas/Debit"),
                new Schema().$ref("#/components/schemas/Credit")
        );
        sc.setOneOf(oneOf);
        sc.setDiscriminator(ds);
        md.schema(sc);

        openApi.getPaths().entrySet().stream()
                .flatMap(entry -> entry.getValue().readOperations().stream())
                .forEach(op -> op.getRequestBody().getContent().addMediaType("application/json", md));
    };
}

@bnasslahsen
Copy link
Collaborator

Using the OpenApiCustomiser shoud be the silver bullet and the last solution to choose:

Your requirement looks quite standard and doesn't require any customisations.
When you use OpenApiCustomiser, then you are not using the native swagger-api annotations.

As mentioned, you can simply add the @Schema declaration on your class (and not on the @RequestBody) as proposed on swagger-api/swagger-core#3046. This is a safe workaround, until
swagger-api/swagger-core#3421 is resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants