[Patch/32.2.2] feat: handle unknown enum values gracefully#1325
[Patch/32.2.2] feat: handle unknown enum values gracefully#1325
Conversation
- Add SafeStringEnumConverter to return null for unknown enum values - Register converter globally in JsonOperation.Deserialize() - Update template to make all enums nullable (not just non-required) - Add comprehensive test suite for enum deserialization - Ensures forward compatibility when API adds new enum values
- Rename NewtonsoftEnumBehaviorTest to StringEnumConverterTest - Split test into focused Given/When/Then tests - Make TestEnum and TestModel private - Remove redundant SafeStringEnumConverter from JsonOperation.Deserialize - Use Nullable.GetUnderlyingType instead of custom IsNullableType helper
Summary of ChangesHello @galesky-a, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the robustness of JSON deserialization by introducing a mechanism to gracefully handle unknown enum values. Previously, encountering an unknown enum string during deserialization would lead to an exception. With this change, such values will now be deserialized to Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Ignored Files
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a SafeStringEnumConverter to gracefully handle unknown enum values during JSON deserialization by converting them to null instead of throwing an exception. This improves the forward compatibility of the API client. The changes include the converter itself, comprehensive unit tests demonstrating the new behavior, and updates to the C# model generation templates to make enum properties nullable. The implementation is solid and the tests are thorough. I have one suggestion to improve the formatting of JSON strings in the tests for better readability and robustness.
| var json = @"{ | ||
| ""channel"": ""UnknownChannelValue"", | ||
| ""amount"": { | ||
| ""value"": 1000, | ||
| ""currency"": ""EUR"" | ||
| }, | ||
| ""reference"": ""test-ref"", | ||
| ""merchantAccount"": ""test-merchant"" | ||
| }"; |
There was a problem hiding this comment.
Using multi-line raw string literals for JSON can introduce unintended whitespace (like newlines and indentation) which might cause issues with some JSON parsers. For better readability and to ensure the JSON is correctly formatted, it's recommended to define test JSON on a single line. This makes the test less prone to formatting errors. This feedback applies to other small JSON string definitions in this test file. For larger JSON payloads like in TestTransferWebhookWithUnknownReasonEnum, consider moving them to an embedded resource file to keep the test code clean.
var json = @"{\"channel\":\"UnknownChannelValue\",\"amount\":{\"value\":1000,\"currency\":\"EUR\"},\"reference\":\"test-ref\",\"merchantAccount\":\"test-merchant\"}";f976574 to
8040f27
Compare
Description
This PR introduces a "graceful fail" mechanism for enum deserialization. It ensures that when the Adyen API adds new enum values, the SDK remains functional by treating unknown values as null rather than throwing a JsonSerializationException.
Related PRs and runs:
oneOf,allOf). Note that the only change from31.1.3is that LEM was bumped from V3 to V4.Key Changes
Testing Infrastructure:
How to verify