Skip to content

Make ComponentContext containsKey consistent with get - #1394

Open
sharanggupta wants to merge 1 commit into
spring-projects:mainfrom
sharanggupta:gh-685-component-context-contains-key
Open

sharanggupta wants to merge 1 commit into
spring-projects:mainfrom
sharanggupta:gh-685-component-context-contains-key

Conversation

@sharanggupta

Copy link
Copy Markdown

BaseComponentContext extends LinkedHashMap and overrides get to throw NoSuchElementException when a key maps to a null value, but it inherits the map's containsKey, which reports such a key as present. Guarding get with containsKey, as the API invites, therefore still raised the exception, for example when a ConfirmationInput received input other than y/n and its null result was stored in the context. The maintainer confirmed in the issue that a key reported by containsKey should always be retrievable via get.

This PR overrides containsKey in BaseComponentContext to apply the same rule as get, so a key is only reported as contained when get can return its value, and documents that contract on ComponentContext#containsKey.

Note that current main already null-guards the confirmation result inside ComponentFlow, so the original end-to-end path no longer triggers it, but the inconsistency remains reachable through any put(key, null) (user pre/post handlers, context copying between components).

Tests: two cases added to ComponentContextTests (positive case, and put(key, null) makes containsKey false while get throws). The spring-shell-core and spring-shell-jline suites and spring-javaformat:validate pass.

Fixes #685

BaseComponentContext extends LinkedHashMap and overrides get to throw
NoSuchElementException when a key maps to a null value, but it inherited
the map's containsKey, which reports such a key as present. Guarding get
with containsKey, as the API invites, therefore still raised the
exception, for example when a ConfirmationInput received input other
than y/n and its null result was stored in the context.

This commit overrides containsKey to apply the same rule as get, so a
key is only reported as contained when get can return its value.

Fixes spring-projects#685

Signed-off-by: Sharang Gupta <sharang@sharanggupta.dev>

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BaseComponentContext containsKey is not accurate with get

1 participant