[java] Fix duplicate field name handling in InstanceCoercer#getFieldWriters - #17187
Conversation
Review Summary by QodoFix duplicate field name handling in InstanceCoercer#getFieldWriters
WalkthroughsDescription• Fixes duplicate field name handling in InstanceCoercer#getFieldWriters • Adds merge function to Collectors.toMap() to retain first entry • Uses LinkedHashMap to preserve field processing order • Prevents runtime IllegalStateException on duplicate keys File Changes1. java/src/org/openqa/selenium/json/InstanceCoercer.java
|
Code Review by Qodo
1.
|
|
Hi maintainers, could someone please assign a reviewer for this PR? Thank you. |
asolntsev
left a comment
There was a problem hiding this comment.
It's slightly better than previously, so we can merge this PR. Thank you.
Before:
java.lang.IllegalStateException: Duplicate key value (attempted merging values org.openqa.selenium.json.InstanceCoercer$TypeAndWriter@4e70a728 and org.openqa.selenium.json.InstanceCoercer$TypeAndWriter@b7838a9)After:
org.openqa.selenium.json.JsonException: Duplicate JSON field name detected while collecting field writersP.S. But the name of the field is not still shown in the error message. Ideally, we should show the name as well. But I can it in a separate PR if you wish.
This PR extracts few anonymous lambdas in InstanceCoercer.java to named classes which now have "toString" method. Thus, they look readable in exception messages. continuation of SeleniumHQ#17187
This PR extracts few anonymous lambdas in InstanceCoercer.java to named classes which now have "toString" method. Thus, they look readable in exception messages. continuation of SeleniumHQ#17187
…riters (SeleniumHQ#17187) * Fix duplicate field name handling in InstanceCoercer#getFieldWriters * added unit testcase
SeleniumHQ#17225) Add field names to JsonException "Duplicate JSON field name..." This PR extracts few anonymous lambdas in InstanceCoercer.java to named classes which now have "toString" method. Thus, they look readable in exception messages. continuation of SeleniumHQ#17187


💥 What does this PR do?
This PR fixes duplicate field name handling in
InstanceCoercer#getFieldWriters.Previously, field writers were collected using
Collectors.toMap(...)without an explicit merge strategy. When duplicate field names were encountered (for example through field hiding in inheritance), the collector would fail with an implicitIllegalStateExceptioncaused by duplicate keys.This change makes duplicate handling explicit by detecting duplicates during map collection and throwing a
JsonExceptionwith a clear error message.This prevents ambiguous field mappings and replaces the implicit collector failure with a controlled and descriptive exception.
🔧 Implementation Notes
The change modifies the
Collectors.toMap(...)call ingetFieldWriters.Previous behavior
Collectors.toMapwas used without a merge function.IllegalStateExceptionfrom the collector.New behavior
JsonExceptionwhen duplicate field names are encountered.Example scenario that previously caused an implicit collector failure: