Skip to content
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
13 changes: 2 additions & 11 deletions src/main/java/org/hisp/dhis/jsontree/JsonObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,14 @@ record Property(
* access is a property of the accessed {@link Property#jsonName()} with the same {@link
* Property#javaName()}.
*
* @return a model of this object in form its properties in no particular order
* @return a model of this object in form its properties in no particular order, including {@link
* Collapsed} properties for {@link Record}s
* @since 1.4
*/
static List<Property> properties(Class<?> of) {
return JsonVirtualTree.properties(of);
}

/**
* @param of an object type
* @return a list of the properties in the given object type including those collapsed down from
* {@link Collapsed} inner {@link Record}s
* @since 1.9
*/
static List<Property> collapsedProperties(Class<? extends Record> of) {
return JsonVirtualTree.collapsedProperties(of);
}

/**
* Access to object fields by name.
*
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/org/hisp/dhis/jsontree/JsonVirtualTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ final class JsonVirtualTree implements JsonMixed, Serializable {
new JsonVirtualTree(JsonNode.NULL, JsonPath.SELF, JsonAccess.GLOBAL);

private static final Map<Class<?>, List<Property>> PROPERTIES = new ConcurrentHashMap<>();
private static final Map<Class<?>, List<Property>> COLLAPSED_PROPERTIES = new ConcurrentHashMap<>();

static List<Property> properties(Class<?> of) {
if (JsonObject.class.isAssignableFrom(of)) {
Expand All @@ -90,16 +89,12 @@ static List<Property> properties(Class<?> of) {
if (Record.class.isAssignableFrom(of)) {
@SuppressWarnings("unchecked")
Class<? extends Record> type = (Class<? extends Record>) of;
return PROPERTIES.computeIfAbsent(type, t -> componentProperties(type));
return PROPERTIES.computeIfAbsent(type, t -> collapsedComponentProperties(type));
}
throw new UnsupportedOperationException(
"Must be a subtype of JsonObject or Record but was: " + of);
}

static List<Property> collapsedProperties(Class<? extends Record> of) {
return COLLAPSED_PROPERTIES.computeIfAbsent(of, type -> collapsedComponentProperties(of));
}

static JsonMixed lift(JsonNode node, JsonAccessors accessors) {
return new JsonVirtualTree(node.getRoot(), node.path(), node, accessors);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/hisp/dhis/jsontree/JsonValueToTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void testSingle() {
}

private static void assertParamsEquals(OuterParams expected, Map<String, List<String>> actual) {
List<JsonObject.Property> properties = JsonObject.collapsedProperties(OuterParams.class);
List<JsonObject.Property> properties = JsonObject.properties(OuterParams.class);
JsonNode object =
JsonBuilder.createObject(
obj -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import java.util.function.Consumer;

import org.hisp.dhis.jsontree.Collapsed;
import org.hisp.dhis.jsontree.JsonMixed;
import org.hisp.dhis.jsontree.Validation;
import org.hisp.dhis.jsontree.Validator;
Expand All @@ -18,7 +20,11 @@
class JsonValidationRecordTest {

record SomeBean(
@Validation(minimum = 0) int age, @Validator(CustomValidator.class) String pattern) {}
@Validation(minimum = 0) int age,
@Validator(CustomValidator.class) String pattern,
@Collapsed InnerBean inner) {}

record InnerBean(@Validation(minLength = 10, required = Validation.YesNo.NO) String name) {}

record CustomValidator() implements Validation.Validator {

Expand All @@ -32,7 +38,7 @@ public void validate(JsonMixed value, Consumer<Validation.Error> addError) {
}

@Test
void testMinimum_OK() {
void testRecord_Minimum_OK() {
assertDoesNotThrow(
() ->
JsonMixed.of(
Expand All @@ -42,17 +48,27 @@ void testMinimum_OK() {
}

@Test
void testMinimum_Required() {
void testRecord_Minimum_Required() {
assertValidationError("{}", SomeBean.class, Validation.Rule.REQUIRED, "age");
}

@Test
void testCustom_Validator() {
void testRecord_Custom_Validator() {
assertValidationError(
"""
{"age": 10, "pattern": "bar"}""",
SomeBean.class,
Validation.Rule.CUSTOM,
"bar");
}

@Test
void testRecord_CollapsedValidation() {
assertValidationError(
"""
{"age": 10, "pattern": "foo", "name": "tooShort"}""",
SomeBean.class,
Validation.Rule.MIN_LENGTH,
10, 8);
}
}
Loading