aws / aws/aws-cdk

constructs: `Node#tryGetContext` is annotated with `@NotNull` while it can return null

Open
#18,942 3 comments 5 reactions 0 assignees View on GitHub
bug docs/inline effort/small p2 package/tools
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### What is the problem?

The `Node#tryGetContext` documentation says

```
Retrieves a value from tree context.

Context is usually initialized at the root, but can be overridden at any point in the tree.

@return The context value or `undefined` if there is no context value for thie key.
```

But the Java bindings are also annotated with `@NotNull`, which is wrong and causes at least Kotlin to issue a warning when still handling null values.

I tried with `software.constructs:constructs:10.0.59` and `tryGetContext` returns `null` for a key for which no context is set.

### Reproduction Steps

https://github.com/felixscheinost/aws-cdk-tryGetContext-NotNull

https://github.com/felixscheinost/aws-cdk-tryGetContext-NotNull/blob/ff9a53cb35f1a4492859fc210736abe10f2e61e1/src/main/java/com/myorg/TestApp.java#L9

```java
System.out.println("tryGetContext is null" + (app.getNode().tryGetContext("notExisting") == null));
```

Running `$ npx cdk synth` prints `tryGetContext is nulltrue`

### What did you expect to happen?

As this method is annotated with `@NotNull` it should not return null.

### What actually happened?

It returned null, which seems correct. Therefore it should be annoated with `@Nullable`

### CDK CLI Version

2.12.0 (build c9786db)

### Framework Version

_No response_

### Node.js Version

v14.18.1

### OS

macOS 12.2

### Language

Java

### Language Version

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by locating the Java binding for Node#tryGetContext and inspect its nullability annotation. Use the linked TestApp.java reproduction to verify behavior when the context key is absent, then confirm the binding exposes that result as nullable for Java and Kotlin callers.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.