Skip to content

Fix null-string conversions to nullable C# extensible enums - #12576

Draft
Jorge Rangel (jorgerangel-msft) with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-nullable-string-operator
Draft

Jorge Rangel (jorgerangel-msft) with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-nullable-string-operator

Conversation

Copilot AI commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Converting a null string to a nullable extensible enum throws because the conditional’s untyped null resolves through the non-nullable implicit operator. Existing null-literal tests bypassed the nullable operator and missed the failure.

  • Generation: Explicitly cast the null branch to the nullable enum type; update affected baselines and generated output.
    public static implicit operator StringExtensibleEnum?(string value) =>
        value == null ? (StringExtensibleEnum?)null : new StringExtensibleEnum(value);
  • Regression coverage: Exercise conversions from string variables containing null, known, unknown, and empty values. Verify null-string conversion to a non-nullable enum still throws.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

…num conversions

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label Nov 13, 2026
Copilot AI changed the title [WIP] Fix ArgumentNullException for nullable string extensible enum Fix null-string conversions to nullable C# extensible enums Nov 13, 2026
@pkg-pr-new

pkg-pr-new Bot commented Nov 13, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-csharp@12576

commit: ecd393b

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the nullable extensible-enum conversion fix.

No open issues 🎉

What changed in this PR

Fixes #12572 in the C# client generator so converting a null string to a nullable extensible enum returns null instead of throwing.

Changes:

  • Explicitly casts the conditional’s null branch to the nullable enum type.
  • Adds regression tests using string variables with null, known, unknown, and empty values, while preserving exceptions for non-nullable conversions.
  • Updates affected generation baselines and sample output.
File Description
packages/​http-client-csharp/​generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​ThingRequiredNullableLiteralString1.cs Applies nullable cast in generated fixture.
packages/​http-client-csharp/​generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​ThingOptionalLiteralString.cs Applies nullable cast in generated fixture.
packages/​http-client-csharp/​generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​StringExtensibleEnum.cs Updates enum exercised by regression tests.
packages/​http-client-csharp/​generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​PreviewExtensibleChoice.cs Applies nullable cast in generated fixture.
packages/​http-client-csharp/​generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​LegacyExtensibleChoice.cs Applies nullable cast in generated fixture.
packages/​http-client-csharp/​generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​DaysOfWeekExtensibleEnum.cs Applies nullable cast in generated fixture.
packages/​http-client-csharp/​generator/​TestProjects/​Local.Tests/​ExtensibleEnumTests.cs Tests nullable conversions and non-nullable rejection.
packages/​http-client-csharp/​generator/​Microsoft.TypeSpec.Generator/​test/​Providers/​EnumProviders/​TestData/​EnumProviderTests/​BackCompat_ExtensibleEnumRemovedValueReadded.cs Updates restored-value baseline.
packages/​http-client-csharp/​generator/​Microsoft.TypeSpec.Generator/​test/​Providers/​EnumProviders/​TestData/​EnumProviderTests/​BackCompat_ExtensibleEnumRemovedValueNotReaddedWhenProvidedByCustomCode.cs Updates custom-provided-value baseline.
packages/​http-client-csharp/​generator/​Microsoft.TypeSpec.Generator/​test/​Providers/​EnumProviders/​TestData/​EnumProviderTests/​BackCompat_ExtensibleEnumRemovedValueNotReaddedWhenBaselineAccepts.cs Updates accepted-removal baseline.
packages/​http-client-csharp/​generator/​Microsoft.TypeSpec.Generator/​test/​Providers/​EnumProviders/​TestData/​EnumProviderTests/​BackCompat_ExtensibleEnumNonPublicValueNotReadded.cs Updates non-public-value baseline.
packages/​http-client-csharp/​generator/​Microsoft.TypeSpec.Generator/​test/​Providers/​EnumProviders/​TestData/​EnumProviderTests/​BackCompat_ExtensibleEnumCustomCodeTakesPrecedenceOverPreservedUnderscores.cs Updates customization-precedence baseline.
packages/​http-client-csharp/​generator/​Microsoft.TypeSpec.Generator/​src/​Providers/​ExtensibleEnumProvider.cs Generates explicitly typed nullable null branch.
docs/​samples/​client/​csharp/​SampleService/​SampleClient/​src/​Generated/​Models/​ThingRequiredNullableLiteralString1.cs Synchronizes sample with corrected conversion.
docs/​samples/​client/​csharp/​SampleService/​SampleClient/​src/​Generated/​Models/​ThingOptionalLiteralString.cs Synchronizes sample with corrected conversion.
docs/​samples/​client/​csharp/​SampleService/​SampleClient/​src/​Generated/​Models/​StringExtensibleEnum.cs Synchronizes sample with corrected conversion.
docs/​samples/​client/​csharp/​SampleService/​SampleClient/​src/​Generated/​Models/​DaysOfWeekExtensibleEnum.cs Synchronizes sample with corrected conversion.

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Nullable implicit operator on string extensible enums still throws for a null string

3 participants