I have the following simple DTO:
public class AccountDto {
@Schema(example = "1", hidden = true)
private Long id;
@Schema(example = "100")
@NonNull private BigDecimal balance;
@Schema(example = "USD")
@NonNull private Currency currency;
}
And the following endpoint which returns a ResponseEntity
with a body of org.springframework.hateoas.CollectionModel
over org.springframework.hateoas.EntityModel
s of AccountDto
s:
@GetMapping("/account")
public ResponseEntity<CollectionModel<EntityModel<AccountDto>>> getAllAccounts() {
return ResponseEntity.ok(
accountModelAssembler.toCollectionModel(accountService.getAllAccounts()));
}
An example of what my endpoint returns is:
{
"_embedded": {
"accountDtoList": [
{
"id": 7,
"balance": 100,
"currency": "AFN",
"_links": {
"self": {
"href": "http://localhost:8080/bankapi/account/7"
},
"all_accounts": {
"href": "http://localhost:8080/bankapi/account"
}
}
},
{
"id": 8,
"balance": 120,
"currency": "USD",
"_links": {
"self": {
"href": "http://localhost:8080/bankapi/account/8"
},
"all_accounts": {
"href": "http://localhost:8080/bankapi/account"
}
}
},
{
"id": 9,
"balance": 100,
"currency": "USD",
"_links": {
"self": {
"href": "http://localhost:8080/bankapi/account/9"
},
"all_accounts": {
"href": "http://localhost:8080/bankapi/account"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/bankapi/account"
}
}
}
I'm trying to emulate this pattern in the example response body of my OpenAPI specification. Influenced by the accepted answer here, I have this above my endpoint's definition in getAllAccounts()
(notice the ref
attribute of the @Schema
annotation):
@Operation(summary = "Get all accounts")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "All accounts successfully retrieved",
content = {
@Content(mediaType = "application/json",
schema = @Schema(ref = "#/components/schemas/CollectionModelAccountDto")) }),
@ApiResponse(responseCode = "401", description = "Unauthenticated user",
content = @Content) })
and in the declaration of my @OpenAPI
bean I attempt to define the schema CollectionModelAccountDto
as follows:
return new OpenAPI()
.components(
new Components()
.addSchemas(
"CollectionModelAccountDto",
new Schema<CollectionModel<EntityModel<AccountDto>>>()
.addProperty(
"_embedded",
new ObjectSchema()
.addProperty(
"accountDtoList",
new ArraySchema().addProperty("foo", new ObjectSchema())))
.addProperty("_links", new JsonSchema()))
.
.
.
My problem is that when I use the embedded ArraySchema
as the value of the property "accountDtoList"
that I have in the embedded ObjectSchema
, OpenAPI does not seem to render the ObjectSchema
at all:
If, however, I change the type of the embedded Schema
from ArraySchema
to ObjectSchema
(or even JsonSchema
), then I get the entire object rendered at full depth:
Of course this is not what I want, since the value associated with "accountDtoList"
needs to be a list, not an object, but I don't quite understand why the rendering of the embedded ArraySchema
does not work. Appreciate any advice.