[csharp] Map unknown enum values to the unknown default member

ND
10h ago
+62-0

With enumUnknownDefaultCase=true, the C# generator adds an UnknownDefaultOpenApi member to each enum. But FromString throws, and FromStringOrDefault returns null, for any value the enum doesn't list. The generichost JSON converters turn that null into a JsonException, so a response containing an enum value added to the API after the client was generated still fails to deserialize, which is what the option is meant to prevent.

This PR makes both methods return the unknown default member when the option is on, for inner enums (modelInnerEnum.mustache) and top-level enums (modelEnum.mustache). Java's fromValue already behaves this way. Without the option the output is unchanged.

Spec

openapi: 3.0.3
info: { title: repro, version: 1.0.0 }
paths: {}
components:
  schemas:
    Color:
      type: string
      enum: [red, green]
    Pet:
      type: object
      required: [status]
      properties:
        status: { type: string, enum: [available, sold] }
        mode: { type: string, nullable: true, enum: [auto, manual] }
        color: { $ref: '#/components/schemas/Color' }

-g csharp --library generichost --additional-properties=enumUnknownDefaultCase=true

Before

public static StatusEnum? StatusEnumFromStringOrDefault(string value)
{
    if (value.Equals("available"))
        return StatusEnum.Available;

    if (value.Equals("sold"))
        return StatusEnum.Sold;

    if (value.Equals("unknown_default_open_api"))
        return StatusEnum.UnknownDefaultOpenApi;

    return null;
}

{"status":"pending"}, {"status":"sold","mode":"scheduled"} and {"status":"sold","color":"blue"} all throw JsonException: The JSON value could not be converted to ....

After

    if (value.Equals("unknown_default_open_api"))
        return StatusEnum.UnknownDefaultOpenApi;

    // Unknown values map to the unknown default member
    return StatusEnum.UnknownDefaultOpenApi;
}

The same three inputs deserialize with Status, Mode and Color set to UnknownDefaultOpenApi. Known values and "mode": null behave as before.

Testing

  • ./mvnw -DskipTests -pl modules/openapi-generator-cli -am package
  • ./bin/generate-samples.sh bin/configs/csharp-*.yaml (80 configs): no changes, because no C# sample config sets enumUnknownDefaultCase.
  • Added CSharpClientCodegenTest#testGenericHostEnumUnknownDefaultCaseMapsUnknownValues, covering an inner and a top-level enum. It fails without the template change and passes with it. CSharpClientCodegenTest: 24 tests, 0 failures.
  • Generated a net8.0 client from the spec above and deserialized the inputs above with the .NET 9 SDK, before and after.

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
    (Ran the build and regenerated all csharp-* configs; nothing changed. Not all configs.)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@mandrean @shibayan @Blackclaws @lucamazzanti @devhl-labs


Summary by cubic

Fixes C# enum deserialization when enumUnknownDefaultCase=true so unknown values map to the generated UnknownDefaultOpenApi member instead of causing a JsonException.

FromString and FromStringOrDefault now return the unknown default member for any unrecognized value on both inner and top-level enums, matching Java's fromValue. Behavior is unchanged when the option is off.