Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Inconsistency happens when CSharpType is created from System.Nullable<T> #4678

Closed
ArcturusZhang opened this issue May 6, 2024 · 0 comments · Fixed by microsoft/typespec#3567 or #4816
Assignees
Labels
MGC v3 Version 3 of AutoRest C# generator.

Comments

@ArcturusZhang
Copy link
Member

ArcturusZhang commented May 6, 2024

Describe the issue or request

We decide to hide the System.Nullable<T> layer in our type system, for instance, we will not generate model a nullable int type as CSharpType { IsFrameworkType: true, FrameworkType: typeof(Nullable<>), Arguments: [ typeof(int) ]
Instead, we do this:

CSharpType { IsFrameworkType: true, FrameworkType: typeof(int), IsNullable: true }

But when we create an instance of CSharpType from typeof(int?), we still get an instance as a generic type of System.Nullable<T>.
The issue exists in this implementation is that if we do this:

CSharpType type = typeof(int?);
Assert.IsTrue(type.IsNullable); // this will fail.

This creates an inconsistency, and when we are creating types, we have to notice that we should not do the above, instead, we should do:

CSharpType type = new CSharpType(typeof(int), true); // this creates a nullable int
Assert.IsTrue(type.IsNullable); // this succeeds.

Unfortunately this is not covered by any test cases. We should add more to validate such an important infrastructure is working as expected.

@ArcturusZhang ArcturusZhang added the v3 Version 3 of AutoRest C# generator. label May 6, 2024
@ArcturusZhang ArcturusZhang self-assigned this Jun 12, 2024
github-merge-queue bot pushed a commit to microsoft/typespec that referenced this issue Jun 12, 2024
Fixes Azure/autorest.csharp#4678

We are only checking if the input type is a generic type, and then get
its generic argument if it is.
We are not checking if it is this special generic type
`System.Nullable<T>` which appears to be a generic type but we actually
want it to be non-generic but nullable.

This PR changes the final constructor that would be invoke for all cases
that a ctor of CSharpType from a `System.Type` to add a check if it is
`System.Nullable<T>`, we will use `T` as `_type`, and the arguments of
`T` as the real arguments.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment