-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Missing JsonView annotation interpreted wrong #4127
Comments
Thanks for reporting this and for related #4128. I am however not sure about reversing current behavior, and not sure about your comment: According to docs This is also clarified in the Baeldung tutorial you mentioned:
Applying #4128 would reverse this behavior to match scenarios with Possibly many user disable |
Thanks for replying. I didn't know that this was configurable (and that the default is different). Therefore I investigated and found the reasons for my ignorance: the default is actually the opposite in spring only, see Jackson2ObjectMapperBuilder. Now that I know about that, I'll try to create a second PR which checks for the actual value and adapts it dynamically. |
As promised I created a new PR with a much more gentle change: if no annotation is present, check said feature and return accordingly. This of course has to be combined with a change in the springdoc library which calls swagger-core, which I also created a PR for: springdoc/springdoc-openapi#1924 |
Thanks for your effort! Unfortunately the solution in #4295 is targeting a "different mapper" and wouldn't work in most scenarios. When using Swagger Core usually two mappers are involved (when using Jackson based payloads binding framework/lib):
These two mappers can and usually have different configurations, as they serve different needs, just as example for date serialization, null handling, mixins, etc. #4295 is targeting 2. (the swagger one), while One solution can be one of the options mentioned in comment above, with no "auto sync" with the project mapper configuration (you would need to manually pass the option/annotation to inform swagger about wished behavior) Another better solution, also open more possibilities for other "configuration sync" would be allowing to pass to swagger a ref to the second mapper (the spring one). Then it would be easy to check its configuration and update behavior accordingly, also for other areas. Both are possibly not trivial unfortunately, but not terribly complex either, they involve however an update of various parts of the code, e.g. configuration, resolving logic, ModelResolver/Context. A different approach with current code is providing a custom You would override public class JsonViewAwareModelResolver extends ModelResolver {
public JsonViewAwareModelResolver(ObjectMapper mapper) {
super(mapper);
}
@Override
protected boolean hiddenByJsonView(Annotation[] annotations,
AnnotatedType type) {
JsonView jsonView = type.getJsonViewAnnotation();
if (jsonView == null)
return false;
Class<?>[] filters = jsonView.value();
for (Annotation ant : annotations) {
if (ant instanceof JsonView) {
Class<?>[] views = ((JsonView) ant).value();
for (Class<?> f : filters) {
for (Class<?> v : views) {
if (v == f || v.isAssignableFrom(f)) {
return false;
}
}
}
}
}
return true;
}
} registering it either programmatically with Just for future ref, here is a comment with some more details about mappers and Swagger |
The
JsonView
annotation is interpreted slightly wrong: fields which are not annotated with it are actually not serialized if a view is specified, they are only included in the default serialization. This is described in this blogpost and I'm using this behaviour in my own code. The original MR for this (#2662) stated it differently, namely that it's always included.The affected method is here:
swagger-core/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java
Line 2183 in bae7c5c
The text was updated successfully, but these errors were encountered: