Skip to content

Commit bd6420e

Browse files
authored
fix: save parent field information to type resolver's target queue (#2159)
* fix: save parent field information to type resolver's target queue Adjust tests that no longer make sense Signed-off-by: Michael Edgar <[email protected]> * Remove unnecessary conditions Signed-off-by: Michael Edgar <[email protected]> --------- Signed-off-by: Michael Edgar <[email protected]>
1 parent ec4d6f6 commit bd6420e

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

core/src/main/java/io/smallrye/openapi/runtime/scanner/dataobject/TypeResolver.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ public FieldInfo getField() {
285285

286286
private void setField(FieldInfo field) {
287287
this.field = field;
288+
targets.add(field);
288289
}
289290

290291
/**
@@ -824,7 +825,9 @@ private static void scanField(AnnotationScannerContext context, Map<String, Type
824825
if (properties.containsKey(propertyName)) {
825826
resolver = properties.get(propertyName);
826827

827-
if (resolver.getField() == null && (Modifier.isPublic(field.flags()) || Modifier.isProtected(field.flags()))) {
828+
if (resolver.getField() == null && (Modifier.isPublic(field.flags())
829+
|| Modifier.isProtected(field.flags())
830+
|| context.getConfig().privatePropertiesEnable())) {
828831
/*
829832
* Field is declared in parent class and the getter/setter was
830833
* declared/overridden in child class

core/src/test/java/io/smallrye/openapi/runtime/scanner/StandaloneSchemaScanTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,4 +901,47 @@ class Bean {
901901

902902
assertJsonEquals("components.schemas.multi-array-generic.json", Bean.class);
903903
}
904+
905+
static class ParentClassFieldSchemaAnnotationTestClasses {
906+
static Class<?>[] CLASSES = {
907+
FieldInterface.class,
908+
ParentClass.class,
909+
ChildClass.class
910+
};
911+
912+
interface FieldInterface {
913+
public String getRequiredField();
914+
915+
public void setRequiredField(String requiredField);
916+
}
917+
918+
class ParentClass {
919+
@jakarta.validation.constraints.Size(min = 1, max = 32)
920+
@jakarta.validation.constraints.NotNull
921+
@Schema(readOnly = true, description = "Required field", examples = "Required field data")
922+
private String requiredField;
923+
924+
public void setRequiredField(String requiredField) {
925+
this.requiredField = requiredField;
926+
}
927+
928+
public String getRequiredField() {
929+
return this.requiredField;
930+
}
931+
}
932+
933+
@Schema(description = "Child class description")
934+
class ChildClass extends ParentClass implements FieldInterface {
935+
@jakarta.validation.constraints.Size(min = 1, max = 32)
936+
@jakarta.validation.constraints.NotNull
937+
@Schema(readOnly = true, description = "Other field", examples = "other field data")
938+
private String otherField;
939+
}
940+
941+
}
942+
943+
@Test
944+
void testParentClassFieldSchemaAnnotation() throws IOException, JSONException {
945+
assertJsonEquals("components.schemas.annotated-parent-field.json", ParentClassFieldSchemaAnnotationTestClasses.CLASSES);
946+
}
904947
}

core/src/test/java/io/smallrye/openapi/runtime/scanner/dataobject/TypeResolverTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ void testAnnotatedInterfaceMethodOverridesImplMethod(AnnotationScannerContext co
171171
assertEquals("c_name", schema.value("name").asString());
172172
assertEquals(50, schema.value("maxLength").asInt());
173173
assertEquals("The name of the canine", schema.value("description").asString());
174-
assertArrayEquals(new String[] { "age", "type", "c_name", "bark", "speciesName", "extinct" },
174+
assertArrayEquals(new String[] { "age", "pet_type", "c_name", "bark", "speciesName", "extinct" },
175175
properties.values().stream().map(TypeResolver::getPropertyName).toArray());
176176
}
177177

core/src/test/java/test/io/smallrye/openapi/runtime/scanner/dataobject/AbstractAnimal.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
@com.fasterxml.jackson.annotation.JsonPropertyOrder(value = { "age", "type" })
77
public abstract class AbstractAnimal implements Animal {
88

9-
@Schema
109
private String type;
1110
protected Integer age;
1211
private boolean extinct;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"openapi" : "3.1.0",
3+
"components" : {
4+
"schemas" : {
5+
"ChildClass" : {
6+
"description" : "Child class description",
7+
"type" : "object",
8+
"required" : [ "requiredField", "otherField" ],
9+
"properties" : {
10+
"requiredField" : {
11+
"type" : "string",
12+
"description" : "Required field",
13+
"readOnly" : true,
14+
"examples" : [ "Required field data" ],
15+
"minLength" : 1,
16+
"maxLength" : 32
17+
},
18+
"otherField" : {
19+
"type" : "string",
20+
"description" : "Other field",
21+
"readOnly" : true,
22+
"examples" : [ "other field data" ],
23+
"minLength" : 1,
24+
"maxLength" : 32
25+
}
26+
}
27+
}
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)