-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support of visibility of properties not annotated with jsonview b…
…y setting DEFAULT_VIEW_INCLUSION
- Loading branch information
Showing
4 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/JsonViewTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<? extends Annotation> annotationType() { | ||
return JsonView.class; | ||
} | ||
public Class<?>[] value() { | ||
return new Class[] {JsonViewObject.View.Protected.class}; | ||
} | ||
}) | ||
.ctxAnnotations( | ||
this.getClass() | ||
.getMethod("includePropertiesToWhichJsonviewIsNotAnnotated") | ||
.getAnnotations())); | ||
|
||
Map<String, Schema> 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<? extends Annotation> annotationType() { | ||
return JsonView.class; | ||
} | ||
public Class<?>[] value() { | ||
return new Class[] {JsonViewObject.View.Protected.class}; | ||
} | ||
}) | ||
.includePropertiesWithoutJSONView(false) | ||
.ctxAnnotations( | ||
this.getClass() | ||
.getMethod("includePropertiesToWhichJsonviewIsNotAnnotated") | ||
.getAnnotations())); | ||
|
||
Map<String, Schema> properties = model.getProperties(); | ||
Assert.assertEquals(properties.size(), 3); | ||
Assert.assertNotNull(properties.get("id")); | ||
Assert.assertNotNull(properties.get("firstName")); | ||
Assert.assertNotNull(properties.get("lastName")); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...les/swagger-core/src/test/java/io/swagger/v3/core/resolving/resources/JsonViewObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |