From f050d0a63c5992c991ad066b15867c32ae6ded1a Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 25 Mar 2023 01:49:26 +0900 Subject: [PATCH] add support of visibility of properties not annotated with jsonview by setting DEFAULT_VIEW_INCLUSION --- .../v3/core/converter/AnnotatedType.java | 14 ++++ .../v3/core/jackson/ModelResolver.java | 2 +- .../v3/core/resolving/JsonViewTest.java | 82 +++++++++++++++++++ .../resolving/resources/JsonViewObject.java | 34 ++++++++ 4 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/JsonViewTest.java create mode 100644 modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/resources/JsonViewObject.java diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/converter/AnnotatedType.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/converter/AnnotatedType.java index 96e634f0fb..da0b642f0d 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/converter/AnnotatedType.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/converter/AnnotatedType.java @@ -22,6 +22,7 @@ public class AnnotatedType { private Annotation[] ctxAnnotations; private boolean resolveAsRef; private JsonView jsonViewAnnotation; + private boolean includePropertiesWithoutJSONView = true; private boolean skipSchemaName; private boolean skipJsonIdentity; private String propertyName; @@ -191,6 +192,19 @@ public AnnotatedType jsonViewAnnotation(JsonView jsonViewAnnotation) { return this; } + public boolean isIncludePropertiesWithoutJSONView() { + return includePropertiesWithoutJSONView; + } + + public void setIncludePropertiesWithoutJSONView(boolean includePropertiesWithoutJSONView) { + this.includePropertiesWithoutJSONView = includePropertiesWithoutJSONView; + } + + public AnnotatedType includePropertiesWithoutJSONView(boolean includePropertiesWithoutJSONView) { + this.includePropertiesWithoutJSONView = includePropertiesWithoutJSONView; + return this; + } + /** * @since 2.0.4 */ diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java index dc1a237ca3..ac92f91684 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java @@ -2879,7 +2879,7 @@ protected boolean hiddenByJsonView(Annotation[] annotations, return false; Class[] filters = jsonView.value(); - boolean containsJsonViewAnnotation = false; + boolean containsJsonViewAnnotation = !type.isIncludePropertiesWithoutJSONView(); for (Annotation ant : annotations) { if (ant instanceof JsonView) { containsJsonViewAnnotation = true; diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/JsonViewTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/JsonViewTest.java new file mode 100644 index 0000000000..3a35c4dfbc --- /dev/null +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/JsonViewTest.java @@ -0,0 +1,82 @@ +package io.swagger.v3.core.resolving; + +import com.fasterxml.jackson.annotation.JsonView; +import com.fasterxml.jackson.databind.MapperFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.swagger.v3.core.converter.AnnotatedType; +import io.swagger.v3.core.converter.ModelConverterContextImpl; +import io.swagger.v3.core.jackson.ModelResolver; +import io.swagger.v3.core.resolving.resources.JsonViewObject; +import io.swagger.v3.oas.models.media.Schema; +import org.junit.Test; +import org.testng.Assert; + +import java.lang.annotation.Annotation; +import java.util.Map; + +import static io.swagger.v3.core.resolving.SwaggerTestBase.mapper; + +public class JsonViewTest { + + @Test + @JsonView(JsonViewObject.View.Protected.class) + public void includePropertiesToWhichJsonviewIsNotAnnotated() throws NoSuchMethodException { + ObjectMapper mapper = mapper(); + final ModelResolver modelResolver = new ModelResolver(mapper); + final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver); + + Schema model = context + .resolve(new AnnotatedType(JsonViewObject.Person.class) + .jsonViewAnnotation(new JsonView() { + public Class annotationType() { + return JsonView.class; + } + public Class[] value() { + return new Class[] {JsonViewObject.View.Protected.class}; + } + }) + .ctxAnnotations( + this.getClass() + .getMethod("includePropertiesToWhichJsonviewIsNotAnnotated") + .getAnnotations())); + + Map properties = model.getProperties(); + Assert.assertEquals(properties.size(), 4); + Assert.assertNotNull(properties.get("id")); + Assert.assertNotNull(properties.get("firstName")); + Assert.assertNotNull(properties.get("lastName")); + Assert.assertNotNull(properties.get("email")); + } + + @Test + @JsonView(JsonViewObject.View.Protected.class) + public void notIncludePropertiesToWhichJsonviewIsNotAnnotated() throws NoSuchMethodException { + ObjectMapper mapper = mapper(); + mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION); + + final ModelResolver modelResolver = new ModelResolver(mapper); + final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver); + + Schema model = context + .resolve(new AnnotatedType(JsonViewObject.Person.class) + .jsonViewAnnotation(new JsonView() { + public Class annotationType() { + return JsonView.class; + } + public Class[] value() { + return new Class[] {JsonViewObject.View.Protected.class}; + } + }) + .includePropertiesWithoutJSONView(false) + .ctxAnnotations( + this.getClass() + .getMethod("includePropertiesToWhichJsonviewIsNotAnnotated") + .getAnnotations())); + + Map properties = model.getProperties(); + Assert.assertEquals(properties.size(), 3); + Assert.assertNotNull(properties.get("id")); + Assert.assertNotNull(properties.get("firstName")); + Assert.assertNotNull(properties.get("lastName")); + } +} diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/resources/JsonViewObject.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/resources/JsonViewObject.java new file mode 100644 index 0000000000..b64cb24082 --- /dev/null +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/resources/JsonViewObject.java @@ -0,0 +1,34 @@ +package io.swagger.v3.core.resolving.resources; + +import com.fasterxml.jackson.annotation.JsonView; + +public class JsonViewObject { + public static class View { + + public interface Public { + } + + public interface Protected { + } + + public interface Private { + } + } + + public static class Person { + + @JsonView({View.Public.class, View.Protected.class, View.Private.class}) + public String id; + + @JsonView({View.Protected.class, View.Private.class}) + public String firstName; + + @JsonView({View.Protected.class, View.Private.class}) + public String lastName; + + @JsonView(View.Private.class) + public String address; + + public String email; + } +}