jakartaee / jakartaee/jsonb-api
enhance PropertyVisibilityStrategy and visibility strategy to cope with class hierarchies
- Dominant language
- Java
- Stars
- 95
- Forks
- 41
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 35
Description
JSONB currently has some weakness when it comes to class hierarchies like `class A extends B`.
In 3.7.1 of the JSONB-1.0 spec it states that
For a serialization operation, if a matching public getter method exists, the method is called to obtain the value of the property. If a matching getter method with private, protected, or defaulted to package-only access exists, then this field is ignored. If no matching getter method exists and the field is public, then the value is obtained directly from the field.
This can get changed using a @JsonbVisibility
4.6 Custom visibility
To customize scope and field access strategy as specified in section 3.7.1, it is possible to specify javax.json.bind.annotation.JsonbVisibility annotation or to override default behavior globally calling JsonbConfig::withPropertyVisibilityStrategy method with given custom property visibility strategy.
There are 2 problems:
1.) 3.7.1 does not state what happens in the case of the public field being in class B `long B#myVal` and a getter exists in class A extends B `public long A#getMyVal()`.
2.) `PropertyVisibilityStrategy` only has an interface `public boolean isVisible(final Field field)`. But `Field` does not contain the information that the visibility of the field myVal needs to get checked for class A. Thus it would not even be possible to implement the rules of 3.7.1 via a portable PropertyVisibilityStrategy if we assume that the getter in the subclass is taken into account.
---
Another (related) question: what effective visibility do we get if we have the following situation and try to serialise an instance of A?
```
@JsonbVisibility(VisibleAllFields.class)
public class B {
public long myVal;
public long getMyVal() { return myVal*2;}
...
}
@JsonbVisibility(VisibleAllMethods.class)
public class A extends B {
public int otherVal;
public long getMyVal() { return myVal*4;}
public int getOtherVal() { return otherVal+5;}
....
}
```
What do we get in the end? does VisibleAllFields apply to the parts from class B? Or does VisibleAllMethods apply to the whole class hierarchy? Afaict this is not yet clarified, isn't it?
Contributor guide
Research direction
Start by reading JSON-B specification sections 3.7.1 and 4.6, then inspect PropertyVisibilityStrategy, JsonbVisibility, and JsonbConfig::withPropertyVisibilityStrategy. Define the expected behavior for inherited fields, getters, and class-level visibility strategies, and document or implement the resulting API and specification changes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100