fix: required TypeEnum deserializer should reject null values#1798
fix: required TypeEnum deserializer should reject null values#1798TabishRiazBajwa wants to merge 2 commits into
Conversation
Fixes Adyen#1678 `Read()` in the generated `TypeEnum` JsonConverter silently accepts `null` during deserialization even when the field is marked as required/non-nullable. This violates the non-nullable contract — a required enum field should never deserialize to null without throwing. **Description** The generated `TypeEnumJsonConverter.Read()` returns `null` when the JSON value is `null`, regardless of whether the property is required. This means a required `TypeEnum` field can silently become `null` at runtime, bypassing the non-nullable contract and causing unexpected null reference exceptions downstream rather than at the deserialization boundary where the error actually is. Fixed by checking for null in the `Read()` method of the generated converter and throwing a `JsonException` when a null value is encountered for a required field. **Tested scenarios** - Required TypeEnum field throws `JsonException` when JSON contains null - Optional TypeEnum field continues to deserialize null correctly - All existing IEnum and TypeEnum deserialization tests continue to pass **Fixed issue**: Adyen#1678
There was a problem hiding this comment.
Code Review
This pull request updates the C# templates to prevent null values for required, non-nullable string-based enums during JSON deserialization, throwing a JsonException if encountered, and adds corresponding unit tests. The feedback recommends updating the signature of FromStringOrDefault to accept a nullable string to prevent compiler warnings when Nullable Reference Types are enabled. Additionally, it is suggested to extend this null-checking logic to required, non-nullable numeric enums for completeness.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Fixes #1678
Read()in the generatedTypeEnumJsonConverter silently acceptsnullduring deserialization even when the field is marked as required/non-nullable. This violates the non-nullable contract — a required enum field should never deserialize to null without throwing.Description
The generated
TypeEnumJsonConverter.Read()returnsnullwhen the JSON value isnull, regardless of whether the property is required. This means a requiredTypeEnumfield can silently becomenullat runtime, bypassing the non-nullable contract and causing unexpected null reference exceptions downstream rather than at the deserialization boundary where the error actually is.Fixed by checking for null in the
Read()method of the generated converter and throwing aJsonExceptionwhen a null value is encountered for a required field.Tested scenarios
JsonExceptionwhen JSON contains nullFixed issue: #1678