Skip to content

Spring Hateoas - Extending EntityModel #798

Closed
@kiwisincebirth

Description

@kiwisincebirth

Describe the bug

  • There is a minor issue with Spring Hateoas support provided by the opendoc-hateoas module
  • This issue can be attributed to a single line of code that needs to be changed, this change has been tested and works
  • The issue is related to the handling of Spring Hateoes PagedModel and CollectionModel in conjunction with EntityModel
  • The issue is cause because we have extended The spring Hateoas EntityModel class, and this is not correctly handled

To Reproduce
We are using the following modules

  • Spring-boot Version 2.2.8
  • springdoc-openapi Version 1.4.3
  • springdoc-openapi Modules ( common , data-rest, hateoas, kotlin, security, ui, web-mvc-core )

Steps to reproduce the behavior:

  • We have defined a custom extension to EntityModel i.e to provide custom behaviour

public class HalEntityModel<T> extends EntityModel<T> { ... }

  • Our API uses this class in its responses on the API. A typical controller method looks like this

@Operation(summary = "Gets clients. Used to view clients in the system ") @GetMapping() public PagedModel<HalEntityModel<ClientDTO>> findClient( ... ) { ... }

Actual behavior
Schema Generated for the return type this looks like this. Noting the Embedded Property Name is "halEntityModels"

PagedModelHalEntityModelClient:
  type: object
  properties:
    _embedded:
      type: object
      properties:
        halEntityModels:
          type: array
          items:
            $ref: '#/components/schemas/HalEntityModelClient'
    _links:
      type: array
      items:
        $ref: '#/components/schemas/Link'
    page:
      $ref: '#/components/schemas/PageMetadata'

Expected behavior
The expected result is as below noting the Embedded Property Name is "clients"

PagedModelHalEntityModelClient:
  type: object
  properties:
    _embedded:
      type: object
      properties:
        clients:
          type: array
          items:
            $ref: '#/components/schemas/HalEntityModelClient'
    _links:
      type: array
      items:
        $ref: '#/components/schemas/Link'
    page:
      $ref: '#/components/schemas/PageMetadata'

Resolution
The issue has been tracked down to the following line of code inside Class : CollectionModelContentConverter : lIne 89
if (containerEntityType.isAssignableFrom(EntityModel.class)) {

the line should read
if ( EntityModel.class.isAssignableFrom(containerEntityType) ) {

The issue is since the containerEntity type (at runtime) is referring to the HalEntityModel class which extends EntityModel. The line of code as written, evaluates to false since Entity Model cannot be assigned to a Sub class. However in the corrected code the expression is true because HalEntityModel is a subtype of Entity Model.

Additional context
This has been tested and return the Schema in the expected format

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions