-
Notifications
You must be signed in to change notification settings - Fork 158
fix: preserve nullable nested input presence in gRPC compiler #1447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Noroth
merged 4 commits into
wundergraph:master
from
fengyuwusong:codex/issue-2650-null-nested-input
Mar 18, 2026
+158
−4
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c045937
fix(grpc-datasource): preserve nullable nested input presence
fengyuwusong 8a818e7
fix: address PR review comments
fengyuwusong 34075dc
fix: add t.Parallel() to subtests to satisfy tparallel linter
fengyuwusong 8e94b0d
Merge branch 'master' into codex/issue-2650-null-nested-input
fengyuwusong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
141 changes: 141 additions & 0 deletions
141
v2/pkg/engine/datasource/grpc_datasource/compiler_input_presence_test.go
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| package grpcdatasource | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "github.com/tidwall/gjson" | ||
| "google.golang.org/protobuf/reflect/protoreflect" | ||
| ) | ||
|
|
||
| const protoSchemaWithOptionalNestedInputs = ` | ||
| syntax = "proto3"; | ||
| package rule.v1; | ||
|
|
||
| import "google/protobuf/wrappers.proto"; | ||
|
|
||
| service RuleService { | ||
| rpc UpdateRule(UpdateRuleRequest) returns (UpdateRuleResponse) {} | ||
| } | ||
|
|
||
| message UpdateRuleRequest { | ||
| ConditionsInput conditions = 1; | ||
| google.protobuf.StringValue params = 2; | ||
| } | ||
|
|
||
| message ConditionsInput { | ||
| string key = 1; | ||
| } | ||
|
|
||
| message UpdateRuleResponse {} | ||
| ` | ||
|
|
||
| func TestCompileOptionalNestedInputsTreatsNullAsAbsent(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| input string | ||
| expectConditions bool | ||
| expectConditionsKey bool | ||
| expectParams bool | ||
| }{ | ||
| { | ||
| name: "omitted optional fields stay absent", | ||
| input: `{}`, | ||
| expectConditions: false, | ||
| expectParams: false, | ||
| }, | ||
| { | ||
| name: "null optional fields stay absent", | ||
| input: `{"conditions":null,"params":null}`, | ||
| expectConditions: false, | ||
| expectParams: false, | ||
| }, | ||
| { | ||
| name: "explicit empty object keeps nested message", | ||
| input: `{"conditions":{}}`, | ||
| expectConditions: true, | ||
| expectConditionsKey: false, | ||
| expectParams: false, | ||
| }, | ||
| { | ||
| name: "explicit nested value is preserved", | ||
| input: `{"conditions":{"key":"status"},"params":"{\"value\":\"1212\"}"}`, | ||
| expectConditions: true, | ||
| expectConditionsKey: true, | ||
| expectParams: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| compiler, err := NewProtoCompiler(protoSchemaWithOptionalNestedInputs, nil) | ||
| require.NoError(t, err) | ||
|
|
||
| inputMessage, ok := compiler.doc.MessageByName("UpdateRuleRequest") | ||
| require.True(t, ok) | ||
|
|
||
| request, err := compiler.buildProtoMessage(inputMessage, optionalNestedInputsMessage(), gjson.Parse(tc.input)) | ||
| require.NoError(t, err) | ||
|
|
||
| conditionsDesc := request.Descriptor().Fields().ByName(protoreflect.Name("conditions")) | ||
| require.NotNil(t, conditionsDesc) | ||
| require.Equal(t, tc.expectConditions, request.Has(conditionsDesc)) | ||
|
|
||
| if tc.expectConditions { | ||
| conditions := request.Get(conditionsDesc).Message() | ||
| keyDesc := conditions.Descriptor().Fields().ByName(protoreflect.Name("key")) | ||
| require.NotNil(t, keyDesc) | ||
| require.Equal(t, tc.expectConditionsKey, conditions.Has(keyDesc)) | ||
| if tc.expectConditionsKey { | ||
| require.Equal(t, "status", conditions.Get(keyDesc).String()) | ||
| } | ||
| } | ||
|
|
||
| paramsDesc := request.Descriptor().Fields().ByName(protoreflect.Name("params")) | ||
| require.NotNil(t, paramsDesc) | ||
| require.Equal(t, tc.expectParams, request.Has(paramsDesc)) | ||
|
|
||
| if tc.expectParams { | ||
| params := request.Get(paramsDesc).Message() | ||
| valueDesc := params.Descriptor().Fields().ByName(protoreflect.Name("value")) | ||
| require.NotNil(t, valueDesc) | ||
| require.True(t, params.Has(valueDesc)) | ||
| require.Equal(t, `{"value":"1212"}`, params.Get(valueDesc).String()) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func optionalNestedInputsMessage() *RPCMessage { | ||
| return &RPCMessage{ | ||
| Name: "UpdateRuleRequest", | ||
| Fields: RPCFields{ | ||
| { | ||
| Name: "conditions", | ||
| ProtoTypeName: DataTypeMessage, | ||
| JSONPath: "conditions", | ||
| Optional: true, | ||
| Message: &RPCMessage{ | ||
| Name: "ConditionsInput", | ||
| Fields: RPCFields{ | ||
| { | ||
| Name: "key", | ||
| ProtoTypeName: DataTypeString, | ||
| JSONPath: "key", | ||
| Optional: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| Name: "params", | ||
| ProtoTypeName: DataTypeString, | ||
| JSONPath: "params", | ||
| Optional: true, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.