Enhanced handling for @JsonView
array annotation in the context of Rest controller method mapping
#32084
Labels
in: web
Issues in web modules (web, webmvc, webflux, websocket)
status: invalid
An issue that we don't feel is valid
Affects: <all versions including latest>
This limitation should relate to the way
@JsonView
annotation was implemented in spring:@#11815
@JsonView
annotation allows to specify multiple views for a class or field using this syntax:By design Spring does not interpret the syntax fully, as it is ignoring multiple view classes in the
@JsonView
array annotation {}Code sample:
ObjectPublicViews.Attributes_Roles.class (and any other view classes in the array) is ignored, just first one is taken into account
Code will behave like having just one view class:
This is how sample domain classes might look like:
This limitation of Spring's handling of
@JsonView
in controller methods could not exactly be a defect but rather a design decision, where each endpoint corresponds to a specific view of the data. Maybe idea was to simplify the controller's logic by binding a single view to an endpoint.However, since JSON does provide such annotation syntax, and because developers may have already defined all JSONViews for each particular association, it would be beneficial to construct a controller JSON view from existing defined views. Consider a scenario where you need to save an object graph to a database, and graph navigation is performed through reachability.
In such cases, a solution to this limitation in Spring is to statically create a new
@JsonView
(EventPrivateViews.ClientEventServiceRequirement_PutView.class in sample code) or add this new view class to an existing@JsonView
annotation and repeat process for each class down the object graph tree using this new View class. This process must be repeated for each new graph view of the existing data (leading to domain recompilation and so on). Additionally, the number of combinations can grow to the point where they become unmanageable. This is the solution I adopted in the included sample classes above.Instead, developers should be able to assemble a new object graph view from existing discrete association-defined views.
I understand that there might be other solutions to overcome this challenge, but it's worth noting that developers often use an injected ObjectMapper per controller instance and prefer not to handle that instance directly, let alone create new instances for each REST call (...out of question) to handle dynamic situations that cannot be managed by annotations.
So, it would be nice to be able to dynamically specify an array with classes for the JsonView annotation for the controller.
In such case spring should analyze the view classes and spring ObjectMapper autowired instance should lazy initialize the object graph (tree) before serialization (as it does now), but this time taking into account all specified view classes. It should parse the annotations and create a union (set) for the final view with all related attributes. ObjectMapper should not retain any configuration state (like it correctly does now) for this dynamic view.
Some ideea:
ObjectWriter writer = objectMapper.writerWithView(spring_internally_assembled_view);
The text was updated successfully, but these errors were encountered: