spring-projects / spring-projects/spring-data-rest
Using @AccessType(AccessType.Type.PROPERTY) changes how instance is converted for persistence [DATAREST-360]
@odrotbohm is already working on this.
Since Dec 31, 2020.
- Dominant language
- Java
- Stars
- 958
- Forks
- 568
- PR merge metrics
- No merged PRs in 30d
Description
Aaron Loes opened DATAREST-360 and commented
Given the following class:
public class OAuth2Authorization extends PersistentEntity {
private static final long serialVersionUID = -3568636024666420442L;
private OAuth2AccessToken oAuth2AccessTokenBlah;
private OAuth2Authentication oAuth2AuthenticationBlah;
/* snip other member variables */
/* snip zero arg and full arg constructors */
public OAuth2AccessToken getOAuth2AccessToken() {
return oAuth2AccessTokenBlah;
}
public void setOAuth2AccessToken(OAuth2AccessToken oAuth2AccessToken) {
this.oAuth2AccessTokenBlah = oAuth2AccessToken;
}
public OAuth2Authentication getOAuth2Authentication() {
return oAuth2AuthenticationBlah;
}
public void setOAuth2Authentication(OAuth2Authentication oAuth2Authentication) {
this.oAuth2AuthenticationBlah = oAuth2Authentication;
}
/* snip getters and setters for other member variables */
}
Prior to adding @AccessType(AccessType.Type.PROPERTY) on the class, the objects in MongoDB looked like:
{
"_id" : ObjectId("53ab27eb4cd2e9d01e1adc92"),
"_class" : "package.for.OAuth2Authorization",
"oAuth2AccessToken" : {
...snip...
},
"oAuth2Authentication" : {
...snip...
},
"authenticationKey" : "b95989bb4605fb24c795a5e35caf6996",
"clientId" : "c43b3a7e253b902c7ca60cc4f5c775037d7ce89d163f97808f65d091c599a0ad"
}
After adding @AccessType(AccessType.Type.PROPERTY) on the class, the objects in MongoDB look like:
{
"_id" : ObjectId("53ab27eb4cd2e9d01e1adc92"),
"_class" : "package.for.OAuth2Authorization",
"OAuth2AccessToken" : {
...snip...
},
"OAuth2Authentication" : {
...snip...
},
"authenticationKey" : "b95989bb4605fb24c795a5e35caf6996",
"clientId" : "c43b3a7e253b902c7ca60cc4f5c775037d7ce89d163f97808f65d091c599a0ad"
}
Notice the difference in case on "oAuth2AccessToken" and "oAuth2Authentication" between the two. After some testing, it appears that w/o the annotation, the field name is used and with the annotation, it is using the method name minus the get/set. I would have expected that it would always use the field name no matter which AccessType was used. This also affects any queries written to access data based on the names of those fields.
On a side note, why wouldnt you always access a field value through its getter method? Not doing so makes an assumption on how the domain model is coded as a simple pojo. Developers may put in extra logic in their getters/setters that should be exercised when creating the JSON for API and for persistence.
Affects: 2.1.1 (Dijkstra SR1)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.