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

Polymorphism - documentation not generated for subtypes when using JsonTypeInfo.As.WRAPPER_OBJECT and JsonTypeInfo.Id.NAME #1799

Closed
didjoman opened this issue Aug 18, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@didjoman
Copy link

didjoman commented Aug 18, 2022

Describe the bug

If a controller returning a response containing an interface or class that is annotated with:

@JsonTypeInfo(include= JsonTypeInfo.As.WRAPPER_OBJECT, use=JsonTypeInfo.Id.NAME)
@JsonSubTypes({
    @JsonSubTypes.Type(value = Dog.class, name = "dog"),
})

Then, the documentation generated does not contain the schemas of child classes.

How to reproduce

git clone https://github.com/didjoman/springdoc-polymorphism-issue-v1.6.10
Run the application (spring boot).
Run curl --location --request GET 'http://localhost:8080/doc' --header 'Content-Type: application/json'

Expected behavior

The json representation of the Cat and Dog (subtypes of Animal class) schemas should be returned:

{
    "openapi": "3.0.1",
    "info": {
        "title": "OpenAPI definition",
        "version": "v0"
    },
    "servers": [
        {
            "url": "http://localh",
            "description": "Generated server url"
        }
    ],
    "paths": {
        "/test": {
            "get": {
                "tags": [
                    "basic-controller"
                ],
                "summary": "get",
                "description": "Provides an animal.",
                "operationId": "get",
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "*/*": {
                                "schema": {
                                    "oneOf": [
                                        {
                                            "$ref": "#/components/schemas/Cat"
                                        },
                                        {
                                            "$ref": "#/components/schemas/Dog"
                                        }
                                    ]
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "Animal": {
                "type": "object",
                "description": "Represents an Animal class."
            },
            "Cat": {
                "type": "object",
                "description": "Represents a Cat class.",
                "allOf": [
                    {
                        "$ref": "#/components/schemas/Animal"
                    },
                    {
                        "type": "object",
                        "properties": {
                            "speed": {
                                "type": "integer",
                                "format": "int32"
                            }
                        }
                    }
                ]
            },
            "Dog": {
                "type": "object",
                "description": "Represents a Dog class.",
                "allOf": [
                    {
                        "$ref": "#/components/schemas/Animal"
                    },
                    {
                        "type": "object",
                        "properties": {
                            "name": {
                                "type": "string"
                            },
                            "age": {
                                "type": "integer",
                                "format": "int32"
                            }
                        }
                    }
                ]
            }
        }
    }
}

Actual behavior

Only the Animal (parent class) schema is described:

{
    "openapi": "3.0.1",
    "info": {
        "title": "OpenAPI definition",
        "version": "v0"
    },
    "servers": [
        {
            "url": "http://localh",
            "description": "Generated server url"
        }
    ],
    "paths": {
        "/test": {
            "get": {
                "tags": [
                    "basic-controller"
                ],
                "summary": "get",
                "description": "Provides an animal.",
                "operationId": "get",
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "*/*": {
                                "schema": {
                                    "oneOf": [
                                        {
                                            "$ref": "#/components/schemas/Cat"
                                        },
                                        {
                                            "$ref": "#/components/schemas/Dog"
                                        }
                                    ]
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "Animal": {
                "type": "object",
                "description": "Represents an Animal class."
            },
            "Cat": {
                "type": "object",
                "description": "Represents a Cat class.",
                "allOf": [
                    {
                        "$ref": "#/components/schemas/Animal"
                    },
                    {
                        "type": "object",
                        "properties": {
                            "speed": {
                                "type": "integer",
                                "format": "int32"
                            }
                        }
                    }
                ]
            },
            "Dog": {
                "type": "object",
                "description": "Represents a Dog class.",
                "allOf": [
                    {
                        "$ref": "#/components/schemas/Animal"
                    },
                    {
                        "type": "object",
                        "properties": {
                            "name": {
                                "type": "string"
                            },
                            "age": {
                                "type": "integer",
                                "format": "int32"
                            }
                        }
                    }
                ]
            }
        }
    }
}

Additional context

The issue started to occur from version 1.5.8 onwards. From 1.5.8 to 1.6.9 (inlcuded) there was a nullPointer in the logs.
Since 1.6.10, the NullPointerException is fixed, but the documentation is still incorrect (not generating subtypes documentation).

See also the same demo using SpringDoc 1.5.7 that worked: https://github.com/didjoman/springdoc-no-polymorphism-issue-v1.5.7

@didjoman didjoman changed the title Polymorphism - documentation not generated for subtypes when using JsonTypeInfo.As.WRAPPER_OBJECT Polymorphism - documentation not generated for subtypes when using JsonTypeInfo.As.WRAPPER_OBJECT and JsonTypeInfo.Id.NAME Aug 18, 2022
@DVD27
Copy link

DVD27 commented Aug 19, 2022

Hello,

I have the same issue but I success to get arround using this annotation :

@Schema(type = "object", oneOf = { Dog.class, Cat.class })

But It will be great to fix this bug ;)

@ruckc
Copy link

ruckc commented Aug 19, 2022

Running into this today. I have the issue even with @Schema on my interface.

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonSubTypes({
    @JsonSubTypes.Type(value = A.class, name = "a"),
    @JsonSubTypes.Type(value = B.class, name = "b"),
    @JsonSubTypes.Type(value = C.class, name = "c"),})
@Schema(type = "object", oneOf = {A.class, B.class, C.class})
public interface SomeInterface extends Serializable {

}

Initially my annotations looked like this, which also resulted in the error, using CUSTOM with a TypeIdResolver implementation.

@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, include = JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonTypeIdResolver(MyResolver.class)
public interface SomeInterface extends Serializable {

}

@bnasslahsen bnasslahsen added the bug Something isn't working label Aug 20, 2022
bnasslahsen added a commit that referenced this issue Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants