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

@Schema oneOf config is ignored when generate the api-docs #2705

Closed
abccbaandy opened this issue Sep 12, 2024 · 2 comments
Closed

@Schema oneOf config is ignored when generate the api-docs #2705

abccbaandy opened this issue Sep 12, 2024 · 2 comments
Labels
incomplete incomplete description: Make sure you Provide a Minimal, Reproducible Example - with HelloController

Comments

@abccbaandy
Copy link

  • What version of spring-boot you are using?
    3.3.3

  • What modules and versions of springdoc-openapi are you using?
    2.6.0

  • What is the actual and the expected result using OpenAPI Description (yml or json)?

                    "MyPojo": {
                        "oneOf": [
                            {
                                "$ref": "#/components/schemas/Child1"
                            },
                            {
                                "$ref": "#/components/schemas/Child2"
                            },
                            {
                                "$ref": "#/components/schemas/Child3"
                            }
                        ]
                    },

should be

                    "MyPojo": {
                        "oneOf": [
                            {
                                "$ref": "#/components/schemas/Child1"
                            },
                            {
                                "$ref": "#/components/schemas/Child2"
                            }
                        ]
                    },
  • Provide with a sample code (HelloController) or Test that reproduces the problem
    @Schema(oneOf = {Child1.class, Child2.class})
    private Parent obj;

Also, according to this issue
#2704
I think this issue relate to Springdoc now, instead swagger core, because even I use my own converter to "fix" it, it will overwrite by Springdoc's converter.

@bnasslahsen
Copy link
Contributor

@abccbaandy,

There is no definition of Child3 in you code.
Not reproducible.
Feel free to provide a Minimal, Reproducible Example - with HelloController that reproduces the problem.

This ticket will be closed, but can be reopened if your provide the reproducible sample.

@bnasslahsen bnasslahsen added the incomplete incomplete description: Make sure you Provide a Minimal, Reproducible Example - with HelloController label Sep 29, 2024
@abccbaandy
Copy link
Author

@bnasslahsen

Here is the smallest working code for the issue.

pojo define

@JsonSubTypes({
        @JsonSubTypes.Type(value = Child1.class),
        @JsonSubTypes.Type(value = Child2.class),
        @JsonSubTypes.Type(value = Child3.class),
})
@Data
public abstract class Parent {
    private String parentProperty;
}

@EqualsAndHashCode(callSuper = true)
@Data
public class Child1 extends Parent {
    private String childProperty1;
}

@EqualsAndHashCode(callSuper = true)
@Data
public class Child2 extends Parent {
    private String childProperty2;
}

@EqualsAndHashCode(callSuper = true)
@Data
public class Child3 extends Parent {
    private String childProperty3;
}

usage in controller

@Data
public class MyRequest {
    @Schema(oneOf = {Child1.class, Child2.class})
    private Parent parent;
}

@PostMapping("swaggerTest")
public String swaggerTest(@RequestBody MyRequest myRequest) {
  return null;
}

api-docs

    "components": {
        "schemas": {
            "Child1": {
                "type": "object",
                "allOf": [
                    {
                        "$ref": "#/components/schemas/Parent"
                    },
                    {
                        "type": "object",
                        "properties": {
                            "childProperty1": {
                                "type": "string"
                            }
                        }
                    }
                ]
            },
            "Child2": {
                "type": "object",
                "allOf": [
                    {
                        "$ref": "#/components/schemas/Parent"
                    },
                    {
                        "type": "object",
                        "properties": {
                            "childProperty2": {
                                "type": "string"
                            }
                        }
                    }
                ]
            },
            "Child3": {
                "type": "object",
                "allOf": [
                    {
                        "$ref": "#/components/schemas/Parent"
                    },
                    {
                        "type": "object",
                        "properties": {
                            "childProperty3": {
                                "type": "string"
                            }
                        }
                    }
                ]
            },
            "MyRequest": {
                "type": "object",
                "properties": {
                    "parent": {
                        "oneOf": [
                            {
                                "$ref": "#/components/schemas/Child1"
                            },
                            {
                                "$ref": "#/components/schemas/Child2"
                            },
                            {
                                "$ref": "#/components/schemas/Child3"
                            }
                        ]
                    }
                }
            },
            "Parent": {
                "type": "object",
                "properties": {
                    "parentProperty": {
                        "type": "string"
                    }
                },
                "oneOf": [
                    {
                        "$ref": "#/components/schemas/Child1"
                    },
                    {
                        "$ref": "#/components/schemas/Child2"
                    }
                ]
            }
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
incomplete incomplete description: Make sure you Provide a Minimal, Reproducible Example - with HelloController
Projects
None yet
Development

No branches or pull requests

2 participants