[csharp][generichost] Deserialize present-but-null nullable enums#23912
Open
daberni wants to merge 1 commit into
Open
[csharp][generichost] Deserialize present-but-null nullable enums#23912daberni wants to merge 1 commit into
daberni wants to merge 1 commit into
Conversation
1eb8d6e to
c1ac499
Compare
For a property that is both `required` and `nullable` with an enum type, the generated JsonConverter only assigned the backing Option when the raw JSON string was non-null. An explicit `null` therefore left the Option unset, and the required-property check then threw `ArgumentException: Property is required` — even though the property was present (just null). Assign the Option unconditionally for nullable enums, mapping a null raw value to a null enum value. Non-nullable enums keep the existing guard, so their generated output is unchanged. Regenerated all generichost samples. Adds CSharpClientCodegenTest#testGenericHostNullableEnumDeserializesPresentNull, which generates the generichost client and asserts the RequiredClass converter uses the null-tolerant read for the required+nullable enum while the non-nullable enum keeps the guard.
c1ac499 to
1458996
Compare
Member
|
thanks for the PR cc @devhl-labs |
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.
For a property that is both
requiredandnullablewith an enum type, thegenerichostJsonConverter.Read(...)only assigned the backingOptionwhen the raw JSON string was non-null. An explicitnulltherefore left theOptionunset, and the required-property check then threwArgumentException: Property is required ...— even though the property was present (justnull).Non-enum nullable properties (string, int, DateTime, …) already assign the
Optionunconditionally; only the enum branch had the null-skipping guard.Reproduce
Deserialize a model with a
required+nullableenum whose JSON value isnull, e.g.RequiredClasswith"required_nullable_enum_string": null:Expected: deserializes with the value
null.Fix
csharp/libraries/generichost/JsonConverter.mustache: for the string-enum read, assign theOptionunconditionally when the property is nullable, mapping anullraw value to anullenum value. Non-nullable enums keep the existing guard, so their generated output is unchanged (scoped via{{#isNullable}}).Test
Adds
RequiredClassNullableEnumTeststo the net8NullReferenceTypessample, deserializingRequiredClasswithrequired_nullable_enum_stringset tonull. It throws before the fix and passes after (verified locally, red→green).PR checklist
./bin/generate-samples.sh ./bin/configs/csharp-generichost-*.yaml); committed all changes. The change is scoped to the generichost C# template, so only generichost samples are affected.master.Summary by cubic
Fixes C#
generichostdeserialization for required nullable enums so a present-but-null value no longer triggers a “Property is required” error. Non-nullable enums are unchanged.csharp/libraries/generichost/JsonConverter.mustacheto always set the backingOptionfor nullable enums, mapping a null raw value to a null enum.CSharpClientCodegenTest#testGenericHostNullableEnumDeserializesPresentNullto verify nullable enums accept null and non-nullable enums keep the guard.generichostsamples.Written for commit 1458996. Summary will update on new commits.