Skip to content

Reject Java record inputs that omit a primitive component with a clear input error - #2363

Open
unknownhackerworld wants to merge 1 commit into
Netflix:masterfrom
unknownhackerworld:fix/record-missing-primitive-component
Open

unknownhackerworld wants to merge 1 commit into
Netflix:masterfrom
unknownhackerworld:fix/record-missing-primitive-component

Conversation

@unknownhackerworld

Copy link
Copy Markdown

Pull request checklist

  • Please read our contributor guide
  • Consider creating a discussion on the discussion forum
    first
  • Make sure the PR doesn't introduce backward compatibility issues
  • Make sure to have sufficient test cases

Pull Request type

  • Bugfix
  • Feature
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Other (please describe):

Changes in this PR

When an input argument is mapped to a Java record and the input map does not contain a value for a primitive record component (int, boolean, long, ...), the request now fails with a DgsInvalidInputArgumentException that names the missing component. Before this change it failed with an uncaught IllegalArgumentException, which the default exception handler reports as an internal error.

Root cause

DefaultInputObjectMapper.handleRecordClass (graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/internal/DefaultInputObjectMapper.kt) builds an args array with arrayOfNulls(recordComponents.size) and only fills in components that are present in the input map. Missing components stay null. It then calls the canonical constructor with ctor.newInstance(*args) and only catches ReflectiveOperationException.

For a primitive component, Constructor.newInstance rejects a null argument by throwing IllegalArgumentException. That is not a ReflectiveOperationException, so it escapes mapToJavaObject unwrapped. DefaultDataFetcherExceptionHandler only maps DgsException subclasses to their own error type, so the client gets an INTERNAL error and the server logs it at ERROR level, even though the problem is in the client's input.

A simple way to hit this is a nullable schema field backed by a primitive record component, for example input Filter { limit: Int } mapped to record Filter(int limit), and a request that leaves limit out.

The other mapping paths already handle this as an input error:

  • Kotlin classes: mapToKotlinObject throws DgsInvalidInputArgumentException("No value specified for required parameter ...") for a missing non-null parameter without a default, and wraps any constructor exception.
  • Java beans: any exception while setting a property is wrapped in DgsInvalidInputArgumentException.
  • Records with an explicit null for a primitive component: Spring's conversion service throws a ConversionFailedException, which maybeConvert already wraps. Only the "key missing" case slipped through.

Fix

In the loop over record components, if a component is missing from the input map and its type is primitive, throw a DgsInvalidInputArgumentException before calling the constructor, with a message like: No value specified for primitive record component bar of class com.example.RecordInput. Missing boxed or reference components still get null, as before.

Tests

Added to InputObjectMapperTest, using the existing RecordInput(String foo, boolean bar, Integer baz) test record:

  • mapping to a Java record with a missing primitive component throws DgsInvalidInputArgumentException: omits bar, expects DgsInvalidInputArgumentException mentioning bar. On master this test fails because an IllegalArgumentException is thrown instead.
  • mapping to a Java record with a missing boxed component passes null: omits baz, expects RecordInput("foo-value", true, null). This locks in the existing behavior for non-primitive components.

Results (JDK 17 toolchain, run in a GitHub Codespace):

  • ./gradlew :graphql-dgs:test --tests '*InputObjectMapperTest*' passes with the fix.
  • With only the test change applied (fix reverted), the same run reports 27 tests completed, 1 failed: the new missing-primitive test, which got java.lang.IllegalArgumentException instead of DgsInvalidInputArgumentException.
  • ./gradlew :graphql-dgs:ktlintCheck passes.
  • I did not run the full build across all modules.

Alternatives considered

  • Default missing primitives to 0 / false, like the Java bean path does (an unset primitive field keeps its default value). That would silently make up a value the client never sent, and a record has no "unset" state to fall back to, so an explicit error seemed safer. It also matches how Kotlin data classes are handled. If you would rather have the default-value behavior, it is a small change and I am happy to switch.
  • Also catch IllegalArgumentException around ctor.newInstance and wrap it. That fixes the error type too, but the message would be the generic "Failed to construct record" with a JVM "argument type mismatch" cause, which does not tell the client which field is missing. The up-front check gives a clearer message.

Backward compatibility: valid inputs map exactly as before. The only change is the exception type and message for an input that already failed.

This change was prepared with help from an AI assistant. The bug was traced in the code and the tests above were run before opening the PR.

…r input error

DefaultInputObjectMapper.handleRecordClass leaves the constructor
argument null for any record component missing from the input map. For
a primitive component (int, boolean, ...) Constructor.newInstance then
throws IllegalArgumentException, which is not caught (only
ReflectiveOperationException is), so the request fails with an
internal error instead of a DgsInvalidInputArgumentException
(BAD_REQUEST). Check for a missing primitive component up front and
throw DgsInvalidInputArgumentException naming the component.
Copilot AI balanced review requested due to automatic review settings October 20, 2026 12:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

2 participants