Skip to content

Commit

Permalink
add support of visibility of properties not annotated with jsonview b…
Browse files Browse the repository at this point in the history
…y setting DEFAULT_VIEW_INCLUSION
  • Loading branch information
uc4w6c authored and frantuma committed Jul 18, 2023
1 parent 9fd0bc0 commit f050d0a
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
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"));
}
}
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;
}
}

0 comments on commit f050d0a

Please sign in to comment.