Repository navigation
Fix nullable implicit cast operator for string extensible enums - #12575
Open
Vishal Raj (vishalrajofficial) wants to merge 1 commit into
Open
Vishal Raj (vishalrajofficial) wants to merge 1 commit into
Vishal Raj (vishalrajofficial) wants to merge 1 commit into
Conversation
Cast the null branch of the ternary conditional expression to the nullable extensible enum type (Type.WithNullable(true)) so C# does not select the non-nullable implicit operator. Fixes microsoft#12572 Signed-off-by: Vishal Raj <9031842908.vk@gmail.com>
|
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. |
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block the fix for null-string conversions in C# extensible enums.
No open issues 🎉
What changed in this PR
Fixes #12572 in the C# client generator by explicitly casting the null branch of string extensible enum conversions to the nullable enum type. This corrects the behavior introduced in #7967 so converting a null string returns null instead of throwing ArgumentNullException.
Changes:
- Emit an explicitly typed nullable null in the conversion operator.
- Add regression coverage for null-string assignment, method arguments, and the generated cast expression.
- Update six generated sample models to reflect the fix.
| File | Description |
|---|---|
packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/Models/ThingRequiredNullableLiteralString1.cs |
Updates the generated nullable conversion. |
packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/Models/ThingOptionalLiteralString.cs |
Updates the generated nullable conversion. |
packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/Models/StringExtensibleEnum.cs |
Applies the fix to the enum used by regression tests. |
packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/Models/PreviewExtensibleChoice.cs |
Updates the generated nullable conversion. |
packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/Models/LegacyExtensibleChoice.cs |
Updates the generated nullable conversion. |
packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/Models/DaysOfWeekExtensibleEnum.cs |
Updates the generated nullable conversion. |
packages/http-client-csharp/generator/TestProjects/Local.Tests/ExtensibleEnumTests.cs |
Tests null-string assignment and method arguments. |
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/EnumProviders/EnumProviderTests.cs |
Verifies the null branch casts to the nullable enum type. |
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ExtensibleEnumProvider.cs |
Emits the explicit nullable cast in the null branch. |
Review effort: Balanced
Author
|
@microsoft-github-policy-service agree |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #12572.
In PR #7967, a nullable implicit cast operator was added for string extensible enums:
When evaluated, the conditional ternary operator's natural type resolves to
MyEnum(struct) rather thanMyEnum?. Consequently, the untypednullliteral converts via the non-nullableimplicit operator MyEnum(string value)withnull, invoking the constructor withnulland throwing anArgumentNullException.Fix
In
ExtensibleEnumProvider.cs, cast the null branch to the nullable extensible enum type (Null.CastTo(Type.WithNullable(true))), emitting:This forces the ternary expression type to
MyEnum?, evaluating directly tonullwithout invoking the constructor.Tests
ConvertingNullStringToNullableExtensibleEnumDoesNotThrowinExtensibleEnumTests.cscovering both variable assignment and method invocation with a null string.EnumProviderTests.csverifying the consequent expression is cast toType.WithNullable(true).TestProjects/Local.