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

Allow use of @JsonView on class #2053

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import io.smallrye.openapi.api.constants.JsonbConstants;
import io.smallrye.openapi.runtime.io.schema.SchemaConstant;
import io.smallrye.openapi.runtime.scanner.spi.AnnotationScannerContext;
import io.smallrye.openapi.runtime.util.Annotations;
import io.smallrye.openapi.runtime.util.JandexUtil;
import io.smallrye.openapi.runtime.util.TypeUtil;

Expand Down Expand Up @@ -586,7 +587,7 @@ private static boolean isViewable(AnnotationScannerContext context, AnnotationTa
return true;
}

Type[] applicableViews = context.annotations().getAnnotationValue(propertySource, JacksonConstants.JSON_VIEW);
Type[] applicableViews = getJsonViews(context, propertySource);

if (applicableViews != null && applicableViews.length > 0) {
return Arrays.stream(applicableViews).anyMatch(activeViews::contains);
Expand All @@ -595,6 +596,31 @@ private static boolean isViewable(AnnotationScannerContext context, AnnotationTa
return true;
}

/**
* Find {@literal @}JsonView annotations on the field/method, or on the
* declaring class when the field/method itself it not directly targeted by the
* annotation.
*/
private static Type[] getJsonViews(AnnotationScannerContext context, AnnotationTarget propertySource) {
Annotations annotations = context.annotations();

Type[] directViews = annotations.getAnnotationValue(propertySource, JacksonConstants.JSON_VIEW);

if (directViews != null) {
return directViews;
}

ClassInfo declaringClass;

if (propertySource.kind() == Kind.FIELD) {
declaringClass = propertySource.asField().declaringClass();
} else {
declaringClass = propertySource.asMethod().declaringClass();
}

return annotations.getAnnotationValue(declaringClass, JacksonConstants.JSON_VIEW);
}

/**
* Determine if the target should be exposed in the API or ignored. Explicitly un-hiding a property
* via the <code>@Schema</code> annotation overrides configuration to ignore the same property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ class InnerBean1 {
}

@Schema(name = "BeanName")
@com.fasterxml.jackson.annotation.JsonView(Views.Internal.class) // class default is internal
class Bean {
@com.fasterxml.jackson.annotation.JsonView(Views.Internal.class)
String id;
@com.fasterxml.jackson.annotation.JsonView(Views.Public.class)
String name;
@com.fasterxml.jackson.annotation.JsonView(Views.WriteOnly.class)
String secret;
@Schema
@com.fasterxml.jackson.annotation.JsonView()
InnerBean1 inner1;
}

Expand Down