swagger-core icon indicating copy to clipboard operation
swagger-core copied to clipboard

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

Open abccbaandy opened this issue 1 year ago • 3 comments

    @Schema(oneOf = {Child1.class, Child2.class})
    private Parent obj;

The result is something like this

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

Seems the result does not come from my config.

Also I think oneOf should take string($ref) instead class?

abccbaandy avatar Sep 11 '24 06:09 abccbaandy

@abccbaandy could you clarify what your expectations are? And provide a complete example.

ponelat avatar Sep 20 '24 12:09 ponelat

My expectations should be like this, only child1 and child2(from my @Schema(oneOf) config

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

@ponelat How about oneOf with $ref? Sometimes I have complex schema like Child1ForSomeBiz, seems no way to set now?

abccbaandy avatar Sep 20 '24 16:09 abccbaandy

@ponelat

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"
                    }
                ]
            }
        }
    }

abccbaandy avatar Sep 30 '24 11:09 abccbaandy