Repository navigation
Conversation
b048a81 to
fae77f2
Compare
| if (value instanceof LateObjectEvaluator) { | ||
| return ((LateObjectEvaluator) value).evaluate(type); | ||
| } | ||
| if (value.getClass() != type) { |
There was a problem hiding this comment.
Using != (reference equality on Class objects) is overly strict and will break valid, legitimate use cases:
Recommended fix:
if (!type.isAssignableFrom(value.getClass())) {
throw new PatchException(String.format("Could not read %s into %s", value, type));
}And for primitives, also handle autoboxing:
if (!ClassUtils.isAssignable(type, value.getClass())) {
throw new PatchException(String.format("Could not read %s into %s", value, type));
}Spring's ClassUtils.isAssignable(Class, Class) handles both primitive/wrapper coercion and subtype checks correctly.
Also, this change will result in NPE if value is null. JsonPatchPatchConverter#valueFromJsonNode explicitly returns null for null/missing nodes, and PatchOperation stores it directly. The original code value instanceof LateObjectEvaluator safely handles null (returns false), but the new value.getClass() call will throw a NullPointerException at runtime for any patch operation with a null value.
Recommended fix:
protected final Object evaluate(Class<?> type) {
if (value instanceof LateObjectEvaluator) {
return ((LateObjectEvaluator) value).evaluate(type);
}
if (value != null && !ClassUtils.isAssignable(type, value.getClass())) {
throw new PatchException(String.format("Could not read %s into %s", value, type));
}
return value;
}|
|
||
| @Data | ||
| @AllArgsConstructor | ||
| @NoArgsConstructor |
There was a problem hiding this comment.
The existing class already has explicit constructors and getters/setters. Adding @Data, @AllArgsConstructor, @NoArgsConstructor annotations will cause a compilation error due to duplicate constructors and methods generated by Lombok conflicting with the manually written ones.
The Lombok annotations should either replace the manual methods, or the manual methods should be removed. Additionally, @Data, @AllArgsConstructor, and @NoArgsConstructor are Lombok annotations that are not currently used anywhere else in this test file — it's unclear if Lombok is even on the test classpath for this module.
| @@ -155,6 +155,25 @@ void manipulatesNestedCollectionProperly() { | |||
| assertThat(outer.todoList.getTodos()).containsExactly(todos.get(0), todos.get(1), newTodo); | |||
| } | |||
|
|
|||
|
|
|||
| @Test | |||
There was a problem hiding this comment.
Add // GH-XXXX tracker reference. For example:
@Test // GH-XXXX (link to the GitHub issue)
void failPrimitiveInNestedObjectCollection() {If there is no GH issue, please create one
| return ((LateObjectEvaluator) value).evaluate(type); | ||
| } | ||
| if (value.getClass() != type) { | ||
| throw new PatchException(String.format("Could not read %s into %s", value, type)); |
There was a problem hiding this comment.
This error message will produce something like class org.springframework.data.rest.webmvc.json.patch.Todo (the full Class.toString() output). The existing JsonLateObjectEvaluator uses the same format string but the test asserts .withMessageContaining("into class") — this works because Class.toString() starts with "class ". This is fragile and relies on an implementation detail of Class#toString(). Consider using type.getName() or type.getSimpleName() for a cleaner message.
Throw exception when json-patch append adds primitive to collection of complex objects
When trying to do an json-patch append to a collection of complex objects, if we provide a primitive such as a string for the value there is no type checking. This causes a type mismatched item to be added to the collection.
Main cause is that org.springframework.data.rest.webmvc.json.patch.JsonPatchPatchConverter#valueFromJsonNode returns JsonLateObjectEvaluator only for complex types. If a primitive value is returned from this method, the value is not checked by the PatchOperation class.