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

Spring Hateoas - Extending EntityModel #798

Closed
kiwisincebirth opened this issue Jul 23, 2020 · 1 comment
Closed

Spring Hateoas - Extending EntityModel #798

kiwisincebirth opened this issue Jul 23, 2020 · 1 comment
Labels
enhancement New feature or request

Comments

@kiwisincebirth
Copy link

kiwisincebirth commented Jul 23, 2020

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

@bnasslahsen
Copy link
Contributor

@kiwisincebirth,

Great analysis!
Thank you for speeding up the resolution of this issue.

@bnasslahsen bnasslahsen added the enhancement New feature or request label Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants