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

ParameterizedTypeReference<CollectionModel<EntityModel<>>> returns empty content from RestTemplate / WebClient #2160

Open
serpro69 opened this issue Feb 2, 2024 · 8 comments
Assignees

Comments

@serpro69
Copy link

serpro69 commented Feb 2, 2024

I'm trying to retrieve CollectionModel<EntityModel> content from an http response body (I've tried TestRestTemplate and WebClient and both produce same results) but getting an empty content in the CollectionModel: CollectionModel { content: [], fallbackType: null, links: [] }

I can see that the response body actually has data because when I change the type to be returned as string instead of using ParameterizedTypeReference, then the response body is a valid json string like :

{
  "_embedded": {
    "entities": [...]
  },
  "_links": {...}
}

In the debug logs, the Writing part of the body also shows correct results:

[io-64000-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Writing [CollectionModel { content: [EntityModel { content: MyEntity(id=1, name=Test, (truncated)...]

So the issue seems to be in the reading mapping.

Is any special configuration required for this to work?
I've searched around and couldn't find anything on how to make it work.

At the same time, ParameterizedTypeReference<EntityModel<MyEntity>> works as expected. So there's something wrong with mapping the ParameterizedTypeReference<CollectionModel<>> in particular.

@serpro69
Copy link
Author

serpro69 commented Feb 2, 2024

I found a "kind of working thing". If I set the header of the rest template request to prs.hal-forms+json via headers.add("Accept", MediaTypes.HAL_FORMS_JSON_VALUE);, then it works so long as I don't explicitly set what the endpoint produces.

If I set produces in the GetMapping to hal+json like so: @GetMapping(produces = MediaTypes.HAL_JSON_VALUE), and update the Accept header to also accept hal+json: headers.add("Accept", MediaTypes.HAL_JSON_VALUE);, then the mapping from the response breaks and I get empty content.

@serpro69 serpro69 closed this as completed Feb 2, 2024
@serpro69 serpro69 reopened this Feb 3, 2024
@serpro69
Copy link
Author

serpro69 commented Feb 3, 2024

I've updated my comment above. It still looks a bit weird to me and feels like something is broken here (probably my code :D but who knows)
Would really appreciate some help on this.

It does make sense in a way that setting Accept to hal-forms actually produces

{
  "links": [...],
  "content": [...]
}

which has exactly same structure as CollectionModel, so no wonder object mapper is able to handle it automatically

The question is there an automatic way to process CollectionModel as hal+json ? Or do I need to write my own object mapper logic?
Because the controller returns CollectionModel and produces hal+json by default unless explicitly changed via @GetMapper(produces = ...).
But consuming it via ParameterizedTypeReference<CollectionModel<EntityModel<>>> doesn't seem to work in the same way.

@odrotbohm
Copy link
Member

Can you elaborate what exactly you're doing? Through a minimal reproducer maybe? I guess we can and should move this over to Spring HATEOAS as I don't see any Spring Data involvement here.

@odrotbohm odrotbohm transferred this issue from spring-projects/spring-data-commons Jun 25, 2024
serpro69 added a commit to serpro69/spring-hateoas-2160 that referenced this issue Jun 25, 2024
@serpro69
Copy link
Author

serpro69 commented Jun 25, 2024

Hi @odrotbohm ,

I've forked https://github.com/spring-guides/tut-rest and added a commit with a test class where I've tried to showcase the problem - serpro69/spring-hateoas-2160@387c62d

If you look at the EmployeeControllerIT.java test class, you'll notice 3 tests there:

In the first test I'm setting accept header with hal_forms_json_value:
https://github.com/serpro69/spring-hateoas-2160/blob/387c62de0fde124a57c735eb461e4b0c6fe9b61a/rest/src/test/java/EmployeeControllerIT.java#L34-L36
and that allows me to use a new ParameterizedTypeReference<CollectionModel<EntityModel<Employee>>>() {}); for the response and automatically deserialize the body with the correct content
https://github.com/serpro69/spring-hateoas-2160/blob/387c62de0fde124a57c735eb461e4b0c6fe9b61a/rest/src/test/java/EmployeeControllerIT.java#L41

The second test showcases the problem that I've tried to describe in this issue. By removing the hal_forms header, TestRestTemplate is not able to deserialize the response as ParameterizedTypeReference<CollectionModel<EntityModel<...>>> anymore and the result is that both content and links are returned empty

The third test uses a String type for response
https://github.com/serpro69/spring-hateoas-2160/blob/387c62de0fde124a57c735eb461e4b0c6fe9b61a/rest/src/test/java/EmployeeControllerIT.java#L72
just to show that the response is actually not empty and returns as hal+json (not hal-forms+json)

So basically, TestRestTemplate can't automatically deserialize CollectionModel with default hal+json accept header (and the only way I could make it work is to specify hal-forms+json accept header), even though the controller itself returns the data in hal+json format.

Don't know if this makes sense or makes things clearer. Please let me know if you'd like more details from me.

Cheers.

@odrotbohm
Copy link
Member

Thanks for that already! I'll have a look and report back!

@DawidStankiewicz
Copy link

Is there any update on this? I faced the same issue using RestClient.
I see that ParameterizedTypeReference<CollectionModel<>> always returns empty collection.

@odrotbohm
Copy link
Member

@serpro69 – From a glance, it looks nothing in your example code is preparing your client to be hypermedia aware. For the production code, Spring Boot automatically enables the HAL support if Spring HATEOAS on the class path. I.e., all RestTemplate beans in the application context will get augmented with the necessary configuration to read responses using the HAL media type. In your test case, however, you create a plain RestTemplate for use in TestRestTemplate and the former is — by definition — not equipped with the necessary configuration.

Ideally, you register a RestTemplate bean via an @TestConfiguration and use that to set up a TestRestTemplate.

@serpro69
Copy link
Author

serpro69 commented Sep 3, 2024

Hi @odrotbohm ,
Thanks for the info. I'll try that and will report back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants