Skip to content
Open
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 @@ -76,6 +76,8 @@ public class TypeResolver {
private boolean exposed = false;
private boolean readOnly = false;
private boolean writeOnly = false;
private MethodInfo ignoredReadMethod;
private MethodInfo ignoredWriteMethod;
private final List<AnnotationTarget> constraintTargets = new ArrayList<>();
private String propertyNamePrefix;
private String propertyNameSuffix;
Expand Down Expand Up @@ -372,7 +374,17 @@ public Type resolveType() {

public boolean isIgnored() {
return ignored || (readOnly && readMethod == null && field == null)
|| (writeOnly && writeMethod == null && field == null);
|| (writeOnly && writeMethod == null && field == null)
|| (ignoredReadMethod != null && readMethod == ignoredReadMethod && writeMethod == null
&& !isFieldExplicitlyVisible())
|| (ignoredWriteMethod != null && writeMethod == ignoredWriteMethod && readMethod == null
&& !isFieldExplicitlyVisible());
}

private boolean isFieldExplicitlyVisible() {
return field != null && (context.annotations().getAnnotation(field, JacksonConstants.JSON_VIEW) != null
|| isUnhidden(field)
|| context.annotations().getAnnotation(field, JacksonConstants.JSON_PROPERTY) != null);
}

public boolean isReadOnly() {
Expand Down Expand Up @@ -692,8 +704,10 @@ private void processVisibility(AnnotationScannerContext context, AnnotationTarge
if (target.kind() == Kind.METHOD) {
if (isAccessor(context, target.asMethod())) {
this.writeOnly = true;
this.ignoredReadMethod = target.asMethod();
} else {
this.readOnly = true;
this.ignoredWriteMethod = target.asMethod();
}

if (this.readOnly && this.writeOnly) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,46 @@ public void setField3(String field3) {
Stream.of("field0", "field1", "field2", "field3").forEach(f -> assertFalse(p3.get(f).isIgnored()));
}

@Test
/*
* Issue: https://github.com/smallrye/smallrye-open-api/issues/2412
*/
void testJsonViewExcludesGetterOnlyProperty() {
class Views {
class Public {
}

class Private {
}
}

@SuppressWarnings("unused")
class Bean {
private String field0;
private String field1;

public String getField0() {
return field0;
}

public void setField0(String field0) {
this.field0 = field0;
}

@JsonView(Views.Private.class)
public String getField1() {
return field1;
}
}

AnnotationScannerContext context = buildContext(emptyConfig(), Bean.class, Views.Private.class, Views.Public.class);
context.getJsonViews().put(Type.create(DotName.createSimple(Views.Public.class), Type.Kind.CLASS), true);

Map<String, TypeResolver> properties = getProperties(context, Bean.class);
assertFalse(properties.get("field0").isIgnored());
assertTrue(properties.get("field1").isIgnored());
}

/*
* Issue: https://github.com/smallrye/smallrye-open-api/issues/1817
*/
Expand Down