Skip to content
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
4 changes: 4 additions & 0 deletions java/src/org/openqa/selenium/json/InstanceCoercer.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ private Map<String, TypeAndWriter> getFieldWriters(Constructor<?> constructor) {
}
};
return new TypeAndWriter(type, writer);
},
(existing, replacement) -> {
throw new JsonException(
"Duplicate JSON field name detected while collecting field writers");
}));
}

Expand Down
20 changes: 20 additions & 0 deletions java/test/org/openqa/selenium/json/JsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ void shouldAllowUserToPopulateFieldsDirectly() {
assertThat(seen.theName).isEqualTo("fishy");
}

@Test
void shouldThrowWhenDuplicateFieldNamesExistWithFieldSetting() {
String raw = "{\"value\": \"test\"}";

assertThatThrownBy(() -> new Json().toType(raw, ChildFieldBean.class, BY_FIELD))
.isInstanceOf(JsonException.class)
.hasMessageStartingWith("Unable to parse: " + raw)
.cause()
.isInstanceOf(JsonException.class)
.hasMessage("Duplicate JSON field name detected while collecting field writers");
}

@Test
void settingFinalFieldsShouldWork() {
Map<String, String> map = Map.of("theName", "fishy");
Expand Down Expand Up @@ -661,6 +673,14 @@ public void setBean(SimpleBean bean) {
}
}

public static class ParentFieldBean {
String value;
}

public static class ChildFieldBean extends ParentFieldBean {
String value;
}

public static class JsonAware {
private final String convertedValue;

Expand Down