Skip to content

Throw exception when json-patch append adds primitive to collection o… - #2247

Open
andreivn2 wants to merge 2 commits into
spring-projects:mainfrom
andreivn2:json-patch-collection-primitive
Open

andreivn2 wants to merge 2 commits into
spring-projects:mainfrom
andreivn2:json-patch-collection-primitive

Conversation

@andreivn2

Copy link
Copy Markdown

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.

  • You have read the Spring Data contribution guidelines.
  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.
  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

if (value instanceof LateObjectEvaluator) {
return ((LateObjectEvaluator) value).evaluate(type);
}
if (value.getClass() != type) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants