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

add support of visibility of properties not annotated with jsonview b… #4391

Merged
merged 1 commit into from
Jul 18, 2023
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 @@ -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;
}
}