Repository navigation
Reject Java record inputs that omit a primitive component with a clear input error - #2363
Open
unknownhackerworld wants to merge 1 commit into
Open
unknownhackerworld wants to merge 1 commit into
unknownhackerworld wants to merge 1 commit into
Conversation
…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.
unknownhackerworld
requested review from
asibross,
iparadiso,
iuliiasobolevska,
jjacobs44,
kzwang,
paulbakker and
srinivasankavitha
as code owners
October 20, 2026 12:15
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull request checklist
first
Pull Request type
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 aDgsInvalidInputArgumentExceptionthat names the missing component. Before this change it failed with an uncaughtIllegalArgumentException, 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 anargsarray witharrayOfNulls(recordComponents.size)and only fills in components that are present in the input map. Missing components staynull. It then calls the canonical constructor withctor.newInstance(*args)and only catchesReflectiveOperationException.For a primitive component,
Constructor.newInstancerejects anullargument by throwingIllegalArgumentException. That is not aReflectiveOperationException, so it escapesmapToJavaObjectunwrapped.DefaultDataFetcherExceptionHandleronly mapsDgsExceptionsubclasses to their own error type, so the client gets anINTERNALerror 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 torecord Filter(int limit), and a request that leaveslimitout.The other mapping paths already handle this as an input error:
mapToKotlinObjectthrowsDgsInvalidInputArgumentException("No value specified for required parameter ...")for a missing non-null parameter without a default, and wraps any constructor exception.DgsInvalidInputArgumentException.nullfor a primitive component: Spring's conversion service throws aConversionFailedException, whichmaybeConvertalready 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
DgsInvalidInputArgumentExceptionbefore calling the constructor, with a message like: No value specified for primitive record componentbarof classcom.example.RecordInput. Missing boxed or reference components still getnull, as before.Tests
Added to
InputObjectMapperTest, using the existingRecordInput(String foo, boolean bar, Integer baz)test record:mapping to a Java record with a missing primitive component throws DgsInvalidInputArgumentException: omitsbar, expectsDgsInvalidInputArgumentExceptionmentioningbar. Onmasterthis test fails because anIllegalArgumentExceptionis thrown instead.mapping to a Java record with a missing boxed component passes null: omitsbaz, expectsRecordInput("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.java.lang.IllegalArgumentExceptioninstead ofDgsInvalidInputArgumentException../gradlew :graphql-dgs:ktlintCheckpasses.Alternatives considered
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.IllegalArgumentExceptionaroundctor.newInstanceand 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.