diff --git a/java/src/org/openqa/selenium/json/InstanceCoercer.java b/java/src/org/openqa/selenium/json/InstanceCoercer.java index f0f43a2943c38..87aa260e81ac2 100644 --- a/java/src/org/openqa/selenium/json/InstanceCoercer.java +++ b/java/src/org/openqa/selenium/json/InstanceCoercer.java @@ -126,6 +126,10 @@ private Map getFieldWriters(Constructor constructor) { } }; return new TypeAndWriter(type, writer); + }, + (existing, replacement) -> { + throw new JsonException( + "Duplicate JSON field name detected while collecting field writers"); })); } diff --git a/java/test/org/openqa/selenium/json/JsonTest.java b/java/test/org/openqa/selenium/json/JsonTest.java index a8470b722f51b..762e50fefe303 100644 --- a/java/test/org/openqa/selenium/json/JsonTest.java +++ b/java/test/org/openqa/selenium/json/JsonTest.java @@ -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 map = Map.of("theName", "fishy"); @@ -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;