Skip to content

[Patch/32.2.2] feat: handle unknown enum values gracefully#1325

Merged
galesky-a merged 10 commits into32.2.Xfrom
patch/32.2.2
Feb 13, 2026
Merged

[Patch/32.2.2] feat: handle unknown enum values gracefully#1325
galesky-a merged 10 commits into32.2.Xfrom
patch/32.2.2

Conversation

@galesky-a
Copy link
Contributor

@galesky-a galesky-a commented Feb 11, 2026

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:

  • Generated code triggered by this action
  • SDK Automation to target this branch and skip unsupported fields (ex: oneOf, allOf). Note that the only change from 31.1.3 is that LEM was bumped from V3 to V4.
  • Tests run for Dotnet 8.0 and 6.0

Key Changes

  • Added SafeStringEnumConverter, which extends StringEnumConverter. It catches serialization exceptions for unknown enum members and returns null for nullable enum types.
  • Registered the new converter in JsonOperation.Deserialize() to apply it across all SDK operations.
  • Modified modelGeneric.mustache to ensure all generated enum properties and constructor arguments are nullable, regardless of whether they are "required" in the API specification.. This is the most impactful change, this should always have been the case, it's the behavior the API was modeled to have, hence this change is considered a bugfix

Testing Infrastructure:

  • Added test suite (SafeStringEnumConverterTest.cs) to verify that unknown enum values deserialize to null while known values continue to work as expected.

How to verify

  • Run the new test suite: Adyen.Test/SafeStringEnumConverterTest.cs.
  • Observe that providing an unknown string to a nullable enum field no longer crashes the JsonConvert process.

- 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
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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 null for nullable enum properties, preventing application crashes due to API evolution or unexpected data. This is achieved through a new custom JSON converter and an update to the C# model generation template to make all enum properties nullable by default.

Highlights

  • Graceful Enum Deserialization: A new SafeStringEnumConverter has been introduced to handle unknown enum values during JSON deserialization. Instead of throwing an exception, it will now deserialize unknown enum values to null for nullable enum properties, ensuring forward compatibility.
  • Template Update for Nullable Enums: The modelGeneric.mustache template has been modified to ensure that all enum properties generated in C# models are nullable (?). This change is crucial for the SafeStringEnumConverter to function correctly by allowing null assignment for unknown enum values.
  • New Test Cases: Two new test files (StringEnumConverterTest.cs and UnknownEnumDeserializationTest.cs) have been added to validate the new enum deserialization behavior, covering scenarios with known, unknown, and missing enum values, as well as serialization of null enum values.

🧠 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
  • Adyen.Test/StringEnumConverterTest.cs
    • Added a new test file to demonstrate the default behavior of StringEnumConverter (throwing an exception for unknown values) and to ensure null and known values are handled correctly.
  • Adyen.Test/UnknownEnumDeserializationTest.cs
    • Added a new test file to specifically verify the SafeStringEnumConverter's behavior, ensuring unknown enum values deserialize to null and known values deserialize correctly across various scenarios, including webhook data.
  • Adyen/Util/SafeStringEnumConverter.cs
    • Added a new custom JsonConverter that extends StringEnumConverter to catch JsonSerializationException for unknown enum values and return null if the target enum type is nullable.
  • templates/csharp/modelGeneric.mustache
    • Modified the template to append ? to all generated enum properties, making them nullable by default. This change ensures compatibility with the SafeStringEnumConverter.
Ignored Files
  • Ignored by pattern: .github/workflows/** (1)
    • .github/workflows/build-net-6.yml
Activity
  • The pull request title indicates a patch for version 32.2.2, suggesting this is a bug fix or feature addition for an existing release.
  • The description is minimal, indicating that the changes are self-explanatory or follow a known issue.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +17 to +25
var json = @"{
""channel"": ""UnknownChannelValue"",
""amount"": {
""value"": 1000,
""currency"": ""EUR""
},
""reference"": ""test-ref"",
""merchantAccount"": ""test-merchant""
}";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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\"}";

@galesky-a galesky-a marked this pull request as ready for review February 13, 2026 12:58
@galesky-a galesky-a requested review from a team as code owners February 13, 2026 12:58
@galesky-a galesky-a changed the base branch from main to 32.2.X February 13, 2026 13:00
@galesky-a galesky-a merged commit a57ab2b into 32.2.X Feb 13, 2026
@galesky-a galesky-a deleted the patch/32.2.2 branch February 13, 2026 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants