0

I have a spring boot (2.7.11) application, where the entities are part of an inheritance tree:

@Getter @Setter @SuperBuilder
public class Foo {
    private String foo;
}
@Getter @Setter @SuperBuilder
public class Bar extends Foo {
    private String bar;
}
@Getter @Setter @SuperBuilder
public class Baz extends Foo {
    private String baz;
}

My controller returns a list auf said entities:

@RestController
@RequestMapping("/")
public class FooController {
    @GetMapping("/")
    @Operation(operationId = "getFoos", description = "get some Foos")
    public List<Foo>getFoos() {
        return List.of(
                Bar.builder().foo("a").bar("b").build(),
                Baz.builder().foo("x").baz("y").build()
        );
    }
}

Jackson is fine serializing this:

$ curl --silent http://localhost:8080/ | jq . -
[
  {
    "foo": "a",
    "bar": "b"
  },
  {
    "foo": "x",
    "baz": "y"
  }
]

However, the api documentation generated by 'org.springdoc:springdoc-openapi-ui:1.7.0' only contains the superclass, not the subclasses:

$ curl --silent http://localhost:8080/v3/api-docs/ | jq . -
{
  "openapi": "3.0.1",
  "info": {
    "title": "OpenAPI definition",
    "version": "v0"
  },
  "servers": [
    {
      "url": "http://localhost:8080/",
      "description": "Generated server url"
    }
  ],
  "paths": {
    "/": {
      "get": {
        "tags": [
          "foo-controller"
        ],
        "description": "get some Foos",
        "operationId": "getFoos",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "*/*": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/Foo"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Foo": {
        "type": "object",
        "properties": {
          "foo": {
            "type": "string"
          }
        }
      }
    }
  }
}

What can I do to have the child classes included? (Later on, the api is used to autogenerate javascript, which is then missing the types)

Sören
  • 1,803
  • 2
  • 16
  • 23

0 Answers0