Skip to content

Commit 94b9858

Browse files
authored
fix: exclude non-persisted inherited properties from Schema [DHIS2-21374] (#24467) (#24472)
* fix: exclude non-persisted inherited properties from Schema [DHIS2-21374] * fix: refine detection * fix: handle properties without getter
1 parent 145483c commit 94b9858

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Property.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public class Property implements Ordered, Klass {
9292
*/
9393
@Setter private boolean persisted;
9494

95+
@Getter @Setter private boolean isTransient;
96+
9597
/** Name of collection wrapper. */
9698
@Setter private String collectionName;
9799

dhis-2/dhis-services/dhis-service-schema/src/main/java/org/hisp/dhis/schema/introspection/JacksonPropertyIntrospector.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.lang.annotation.Annotation;
5050
import java.lang.reflect.Field;
5151
import java.lang.reflect.Method;
52+
import java.lang.reflect.Modifier;
5253
import java.lang.reflect.ParameterizedType;
5354
import java.lang.reflect.Type;
5455
import java.util.ArrayList;
@@ -71,6 +72,7 @@
7172
import org.hisp.dhis.schema.Property;
7273
import org.hisp.dhis.schema.SchemaService;
7374
import org.hisp.dhis.system.util.ReflectionUtils;
75+
import org.hisp.dhis.translation.Translatable;
7476
import org.springframework.util.ClassUtils;
7577

7678
/**
@@ -117,6 +119,20 @@ public void introspect(Class<?> type, Map<String, Property> properties) {
117119

118120
if (persistedProperties.containsKey(fieldName)) {
119121
initFromPersistedProperty(property, persistedProperties.get(fieldName));
122+
} else {
123+
Method getter = property.getGetterMethod();
124+
if (getter != null) {
125+
Class<?> declaringClass = getter.getDeclaringClass();
126+
if (declaringClass.getName().startsWith("Base")
127+
&& !property.isTransient()
128+
&& (!getter.isAnnotationPresent(Translatable.class)
129+
|| !persistedProperties.containsKey(
130+
getter.getAnnotation(Translatable.class).propertyName()))) {
131+
// this is assumed to be an inherited property that is not persisted,
132+
// so we skip it and do not add it
133+
continue;
134+
}
135+
}
120136
}
121137

122138
initFromDescription(property);
@@ -224,6 +240,7 @@ private static void initFromEnumConstants(Property property) {
224240
private static void initFromPersistedProperty(Property property, Property persisted) {
225241
property.setPersisted(true);
226242
property.setWritable(true);
243+
property.setTransient(false);
227244
property.setFieldName(persisted.getFieldName());
228245
property.setUnique(persisted.isUnique());
229246
property.setRequired(persisted.isRequired());
@@ -331,6 +348,7 @@ private static List<Property> collectProperties(Class<?> type) {
331348

332349
property.setName(name);
333350
property.setFieldName(fieldName);
351+
property.setTransient(Modifier.isTransient(field.getModifiers()));
334352
property.setSetterMethod(findSetterMethod(fieldName, type, field.getType()));
335353
property.setGetterMethod(findGetterMethod(fieldName, type));
336354
property.setNamespace(trimToNull(jsonProperty.namespace()));

0 commit comments

Comments
 (0)