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

Exclude decoratedClass from JSON links, created from Projection and received with Spring @RestController [DATACMNS-909] #1367

Closed
spring-projects-issues opened this issue Sep 8, 2016 · 4 comments
Assignees
Labels
type: bug

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Sep 8, 2016

Dmitry Stolbov opened DATACMNS-909 and commented

Experiment with spring-boot version: in 1.3.7.RELEASE (spring data 2.4.4) decoratedClass is absent, but in 1.4.0.RELEASE (spring data 2.5.2) it is present

I'd like get clean JSON, when using Projection as DTO with RestController. However, in the best case we get JSON, most of which contain links to dekoratedClass.
{"name":"Dmitry","id":1,"decoratedClass":"ru.inkontext.domain.Person", "adress":{"id":1,"decoratedClass":"ru.inkontext.domain.Adress","city":"Surgut"}}

Decide by Oliver Gierke

Looks like this is caused by proxies created through Spring's ProxyFactory now also implementing a new DecoratingProxy, which exposes getDecoratedClass() and our TargetAware interface that's in place to mask the artificial proxy properties doesn't mask that newly introduced attribute. I can adapt our TargetAware accordingly.
A temporary workaround would be to redeclare the getDecoratedClass() method on your projection interface and annotate it with @JsonIgnore.

Tests below imaging matter of question.

RestController

Without Projection

mockMvc.perform(get("/rest/persons/1"))
.andExpect(jsonPath("id").value(1)
.andExpect(jsonPath("decoratedClass").doesNotExist());

Projection created by Repository

mockMvc.perform(get("/rest/persons/1/projected"))
.andExpect(jsonPath("id").value(1))
.andExpect(jsonPath("decoratedClass").value("java.util.HashMap"));

Projection created by Repository with Generic

mockMvc.perform(get("/rest/persons/1/projectedClass"))
.andExpect(jsonPath("id").value(1))
.andExpect(jsonPath("decoratedClass").value("java.util.HashMap"));

Projection created by Projection Factory

mockMvc.perform(get("/rest/persons/1/adressCity"))
.andExpect(jsonPath("id").value(1))
.andExpect(jsonPath("decoratedClass").value("ru.inkontext.domain.Person"));

Standard Sring Data REST API

without Projection

mockMvc.perform(get("/rest/api/persons/1"))
.andExpect(jsonPath("id").doesNotExist())
.andExpect(jsonPath("_links").exists());

with Projection

mockMvc.perform(get("/rest/api/persons/1?projection=adressCity"))
.andExpect(status().isOk())
.andExpect(jsonPath("id").value(1))
.andExpect(jsonPath("_links").exists());


Affects: 1.11.4 (Gosling SR4), 1.12.2 (Hopper SR2), 1.13 M1 (Ingalls)

Reference URL: https://github.com/stolbovd/decoratedClass.git

Backported to: 1.12.3 (Hopper SR3), 1.11.5 (Gosling SR5)

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 9, 2016

Oliver Drotbohm commented

Looks like this is caused by proxies created through Spring's ProxyFactory now also implementing a new DecoratingProxy, which exposes getDecoratedClass() and our TargetAware interface that's in place to mask the artificial proxy properties doesn't mask that newly introduced attribute. I can adapt our TargetAware accordingly.

A temporary workaround would be to redeclare the getDecoratedClass() method on your projection interface and annotate it with @JsonIgnore

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 9, 2016

Oliver Drotbohm commented

That's in place, feel free to give the snapshots a try

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 10, 2016

Dmitry Stolbov commented

This decide with @JsonIgnore getDecoratedClass() is worked.
But it don't work for query methods of Repository, which return Projection:
Unable to locate Attribute with the the given name decoratedClass on this ManagedType ru.inkontext.domain.Person
I do not use this method in my project and @Ignore tests in example tests.
Thank you

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Sep 10, 2016

Dmitry Stolbov commented

Everything works fine with spring-data-commons 1.13.0.BUILD-SNAPSHOT.
Thank you very much!

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

No branches or pull requests

2 participants