Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ protected Object evaluateValueFromTarget(Object targetObject, Class<?> entityTyp
}

protected final Object evaluate(Class<?> type) {
return value instanceof LateObjectEvaluator ? ((LateObjectEvaluator) value).evaluate(type) : value;
if (value instanceof LateObjectEvaluator) {
return ((LateObjectEvaluator) value).evaluate(type);
}
if (value.getClass() != type) {
throw new PatchException(String.format("Could not read %s into %s", value, type));
}
return value;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,25 @@ void manipulatesNestedCollectionProperly() {
assertThat(outer.todoList.getTodos()).containsExactly(todos.get(0), todos.get(1), newTodo);
}


@Test
void failPrimitiveInNestedObjectCollection() {
List<Todo> todos = new ArrayList<>();
todos.add(new Todo(1L, "A", false));
todos.add(new Todo(2L, "B", false));

TodoList todoList = new TodoList();
todoList.setTodos(todos);

assertThatExceptionOfType(PatchException.class)
.isThrownBy(() -> AddOperation.of("/todos/-", "Primitive").perform(todoList, TodoList.class, TestPropertyPathContext.INSTANCE))
.withMessageContaining("Could not read")
.withMessageContaining("into class");
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public static class TodoListWrapper {
public TodoList todoList;

Expand Down