Skip to content

HAL representation (_embedded and _links) in definitions when application returning plain json #870

Closed
@edgeofoblivion

Description

@edgeofoblivion

Describe the bug
DTO class defined as follows:

public class Item extends RepresentationModel<Item> {
  @NonNull private String description;
}

A rest controller returning a collection of Items:

@GetMapping(produces = {MediaType.APPLICATION_JSON_VALUE})
  public ResponseEntity<CollectionModel<Item>> getAllItems() {
...
}

Application property to prevent application/hal+json responses

spring.hateoas.use-hal-as-default-json-media-type=false
spring.jackson.default-property-inclusion=NON_ABSENT

Plain json response from the application as expected (response properties are links and content):

{
    "links": [
        {
            "rel": "self",
            "href": "http://localhost:8080/v1/items"
        }
    ],
    "content": [
        {
            "description": "foo",
            "links": []
        },
        {
            "description": "bar",
            "links": []
        }
    ]
}
{
    "openapi": "3.0.1",
    "info": {
        "title": "OpenAPI definition",
        "version": "v0"
    },
    "servers": [
        {
            "url": "http://localhost:8080",
            "description": "Generated server url"
        }
    ],
    "tags": [
        {
            "name": "Item",
            "description": "The Item API"
        }
    ],
    "paths": {
        "/v1/items": {
            "get": {
                "tags": [
                    "Item"
                ],
                "summary": "Get all items",
                "operationId": "getAllItems",
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/CollectionModelItem"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "CollectionModelItem": {
                "type": "object",
                "properties": {
                    "_embedded": {
                        "type": "object",
                        "properties": {
                            "itemList": {
                                "type": "array",
                                "items": {
                                    "$ref": "#/components/schemas/Item"
                                }
                            }
                        }
                    },
                    "_links": {
                        "$ref": "#/components/schemas/Links"
                    }
                }
            },
            "Item": {
                "type": "object",
                "properties": {
                    "description": {
                        "type": "string"
                    },
                    "_links": {
                        "$ref": "#/components/schemas/Links"
                    }
                }
            },
            "Links": {
                "type": "object",
                "additionalProperties": {
                    "$ref": "#/components/schemas/Link"
                }
            },
            "Link": {
                "type": "object",
                "properties": {
                    "href": {
                        "type": "string"
                    },
                    "hreflang": {
                        "type": "string"
                    },
                    "title": {
                        "type": "string"
                    },
                    "type": {
                        "type": "string"
                    },
                    "deprecation": {
                        "type": "string"
                    },
                    "profile": {
                        "type": "string"
                    },
                    "name": {
                        "type": "string"
                    },
                    "templated": {
                        "type": "boolean"
                    }
                }
            }
        }
    }
}

Note the hal representation, e.g. see _links in Item , expecting links
And properties for CollectionModelItem are _embedded and _links, expecting content and links

To Reproduce
Steps to reproduce the behavior:

  • spring-boot 2.3.3.RELEASE

  • spring-boot-starter-hateoas

  • spring-boot-starter-web

  • springdoc-openapi-ui:1.4.5

  • springdoc-openapi-hateoas:1.4.5

  • Example project attached
    demo.zip

Expected behavior

  • I would expect the generated json/Swagger-UI to match what the application is returning

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions