Skip to content

Commit 370e609

Browse files
author
Saif Eddine Jerbi
committed
Inherited Form Fields #78
expected behavior : The form definition contains the inherited fields fix #78
1 parent e300f8c commit 370e609

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/main/java/io/asfjava/ui/core/schema/UiFormSchemaGenerator.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.Serializable;
44
import java.lang.annotation.Annotation;
55
import java.lang.reflect.Field;
6+
import java.lang.reflect.Modifier;
67
import java.util.ArrayList;
78
import java.util.Arrays;
89
import java.util.Comparator;
@@ -12,9 +13,12 @@
1213
import java.util.Map;
1314
import java.util.Map.Entry;
1415
import java.util.Optional;
16+
import java.util.Set;
1517
import java.util.function.Predicate;
1618
import java.util.stream.Collectors;
1719

20+
import org.reflections.ReflectionUtils;
21+
1822
import com.fasterxml.jackson.databind.JsonMappingException;
1923
import com.fasterxml.jackson.databind.JsonNode;
2024
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -43,7 +47,8 @@ public final class UiFormSchemaGenerator {
4347
private static UiFormSchemaGenerator instance;
4448

4549
public UiForm generate(Class<? extends Serializable> formDto) throws JsonMappingException {
46-
Field[] declaredFields = formDto.getDeclaredFields();
50+
Set<Field> declaredFields = ReflectionUtils.getAllFields(formDto,
51+
field -> !Modifier.isStatic(field.getModifiers()));
4752
ObjectMapper mapper = new ObjectMapper();
4853

4954
JsonSchemaGenerator schemaGen = initSchemaGen(mapper);
@@ -105,13 +110,13 @@ private ObjectNode buildActionNode(ObjectMapper mapper, Action action) {
105110
return node;
106111
}
107112

108-
private ObjectNode handlerGroupedFields(ObjectMapper mapper, Field[] declaredFields,
113+
private ObjectNode handlerGroupedFields(ObjectMapper mapper, Set<Field> declaredFields,
109114
Map<Field, JsonNode> sortedNodes) {
110115
Predicate<? super Field> checkFieldSetAnnotation = field -> field.isAnnotationPresent(FieldSet.class);
111116

112117
Map<String, List<JsonNode>> groupedFields = new LinkedHashMap<>();
113118

114-
Arrays.stream(declaredFields).filter(checkFieldSetAnnotation)
119+
declaredFields.stream().filter(checkFieldSetAnnotation)
115120
.forEach(field -> groupFieldsByTab(sortedNodes, field, groupedFields));
116121

117122
ArrayNode groups = mapper.createArrayNode();
@@ -123,7 +128,7 @@ private ObjectNode handlerGroupedFields(ObjectMapper mapper, Field[] declaredFie
123128

124129
}
125130

126-
private ObjectNode handleTabbedFields(ObjectMapper mapper, Field[] declaredFields, Map<Field, JsonNode> nodes) {
131+
private ObjectNode handleTabbedFields(ObjectMapper mapper, Set<Field> declaredFields, Map<Field, JsonNode> nodes) {
127132
Predicate<? super Field> checkTabAnnotation = field -> field.isAnnotationPresent(Tab.class);
128133

129134
Comparator<? super Field> tabIndexComparator = (field1, field2) -> Integer
@@ -138,7 +143,7 @@ private ObjectNode handleTabbedFields(ObjectMapper mapper, Field[] declaredField
138143

139144
Map<String, List<JsonNode>> groupedFieldsByTab = new LinkedHashMap<>();
140145

141-
Arrays.stream(declaredFields).filter(checkTabAnnotation).sorted(fieldIndexComparator).sorted(tabIndexComparator)
146+
declaredFields.stream().filter(checkTabAnnotation).sorted(fieldIndexComparator).sorted(tabIndexComparator)
142147
.forEach(field -> groupFieldsByTab(nodes, field, groupedFieldsByTab));
143148

144149
ArrayNode tabs = mapper.createArrayNode();
@@ -161,10 +166,10 @@ private ObjectNode handleTabbedFields(ObjectMapper mapper, Field[] declaredField
161166

162167
}
163168

164-
private Map<Field, JsonNode> initFieldsFormDefinition(ObjectMapper mapper, Field[] declaredFields) {
169+
private Map<Field, JsonNode> initFieldsFormDefinition(ObjectMapper mapper, Set<Field> declaredFields) {
165170
Map<Field, JsonNode> nodes = new HashMap<>();
166171

167-
Arrays.stream(declaredFields).forEach(field -> buildFormDefinition(nodes, mapper, field));
172+
declaredFields.forEach(field -> buildFormDefinition(nodes, mapper, field));
168173

169174
return nodes;
170175
}

0 commit comments

Comments
 (0)