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

ArraySchema with anyOf is setting type of the items to "string" #4624

Open
NiFNi opened this issue Mar 5, 2024 · 0 comments
Open

ArraySchema with anyOf is setting type of the items to "string" #4624

NiFNi opened this issue Mar 5, 2024 · 0 comments

Comments

@NiFNi
Copy link

NiFNi commented Mar 5, 2024

Hi!

I am using Spring Boot (3.2.3) with springdoc-openapi-starter-webmvc-ui (2.2.20). I have a controller annotated like this:

    @ApiResponse(responseCode = "200",
            content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                    array = @ArraySchema(schema = @Schema(anyOf = {
                            Dto1.class,
                            Dto2.class
                    }))))
    @GetMapping
    public List<? extends AbstractDto> get() {
        return List.of();
    }

This results in an API specification looking like this:

{
   "openapi": "3.0.1",
   "info": {
       "title": "OpenAPI definition",
       "version": "v0"
   },
   "servers": [
       {
           "url": "http://localhost:8080",
           "description": "Generated server url"
       }
   ],
   "paths": {
       "/": {
           "get": {
               "tags": [
                   "controller"
               ],
               "operationId": "get_1",
               "responses": {
                   "200": {
                       "description": "OK",
                       "content": {
                           "application/json": {
                               "schema": {
                                   "type": "array",
                                   "items": {
                                       "type": "string",
                                       "anyOf": [
                                           {
                                               "$ref": "#/components/schemas/Dto1"
                                           },
                                           {
                                               "$ref": "#/components/schemas/Dto2"
                                           }
                                       ]
                                   }
                               }
                           }
                       }
                   }
               }
           }
       }
   },
   "components": {
       "schemas": {
           "Dto1": {
               "type": "object"
           },
           "Dto2": {
               "type": "object"
           }
       }
   }
}

As you can see the type of the items in the array is set to string which looks wrong and also results in swagger-ui rendering the API wrong.
Tracking the code led me to this line which sets the type to "string":

arraySchemaFromAnnotation.get().getItems().setType("string");

I don't really understand why this is done. As a workaround to get it working correctly I added an explicit type to the items like this which atleast makes swagger-ui render the anyOf correctly:

    @ApiResponse(responseCode = "200",
            content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                    array = @ArraySchema(schema = @Schema(anyOf = {
                            Dto1.class,
                            Dto2.class
                    }, type = "object"))))
    @GetMapping
    public List<? extends AbstractDto> get() {
        return List.of();
    }

What I would have expected in the first place is that the type is not added to the items at all if an anyOf is included.

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

1 participant