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

fix the nullable value type #3567

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public class CSharpType
/// <param name="isNullable">Optional flag to determine if the constructed type should be nullable. Defaults to <c>false</c>.</param>
public CSharpType(Type type, bool isNullable = false) : this(
type,
isNullable,
type.IsGenericType ? type.GetGenericArguments().Select(p => new CSharpType(p)).ToArray() : Array.Empty<CSharpType>())
type.IsGenericType ? type.GetGenericArguments().Select(p => new CSharpType(p)).ToArray() : Array.Empty<CSharpType>(),
isNullable)
{ }

/// <summary>
Expand Down Expand Up @@ -97,6 +97,16 @@ public CSharpType(Type type, IReadOnlyList<CSharpType> arguments, bool isNullabl
{
Debug.Assert(type.Namespace != null, "type.Namespace != null");

// handle nullable value types explicitly because they are implemented using generic type `System.Nullable<T>`
var underlyingValueType = Nullable.GetUnderlyingType(type);
if (underlyingValueType != null)
{
// in this block, we are converting input like `typeof(int?)` into the way as if they input: `typeof(int), isNullable: true`
type = underlyingValueType;
arguments = type.IsGenericType ? type.GetGenericArguments().Select(p => new CSharpType(p)).ToArray() : Array.Empty<CSharpType>();
isNullable = true;
}

_type = type.IsGenericType ? type.GetGenericTypeDefinition() : type;
ValidateArguments(_type, arguments);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System;
using NUnit.Framework;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Immutable;
using Moq;
using System.IO;
using System.Linq;
using System.Text;
using Moq;
using NUnit.Framework;

namespace Microsoft.Generator.CSharp.Tests
{
Expand Down Expand Up @@ -394,5 +394,71 @@ public void TestToString(Type type, string expectedString)

Assert.AreEqual(expected, actual);
}

[TestCaseSource(nameof(ValidateNullableTypesData))]
public void ValidateNullableTypes(Type type, IReadOnlyList<CSharpType> expectedArguments, bool expectedIsNullable)
{
var csharpType = new CSharpType(type);

CollectionAssert.AreEqual(expectedArguments, csharpType.Arguments);
Assert.AreEqual(expectedIsNullable, csharpType.IsNullable);
}

private static object[] ValidateNullableTypesData = [
new object[]
{
typeof(int), Array.Empty<CSharpType>(), false
},
new object[]
{
typeof(int?), Array.Empty<CSharpType>(), true
},
new object[]
{
typeof(Uri), Array.Empty<CSharpType>(), false
},
new object[]
{
typeof(Guid), Array.Empty<CSharpType>(), false
},
new object[]
{
typeof(Guid?), Array.Empty<CSharpType>(), true
},
new object[]
{
typeof(TestStruct<int>), new CSharpType[] { typeof(int) }, false
},
new object[]
{
typeof(TestStruct<int>?), new CSharpType[] { typeof(int) }, true
},
new object[]
{
typeof(TestStruct<int?>), new CSharpType[] { typeof(int?) }, false
},
new object[]
{
typeof(TestStruct<int?>?), new CSharpType[] { typeof(int?) }, true
},
new object[]
{
typeof(TestStruct<TestStruct<int>>), new CSharpType[] { typeof(TestStruct<int>) }, false
},
new object[]
{
typeof(TestStruct<TestStruct<int>>?), new CSharpType[] { typeof(TestStruct<int>) }, true
},
new object[]
{
typeof(TestStruct<TestStruct<int>?>), new CSharpType[] { typeof(TestStruct<int>?) }, false
},
new object[]
{
typeof(TestStruct<TestStruct<int>?>?), new CSharpType[] { typeof(TestStruct<int>?) }, true
},
];

internal struct TestStruct<T> { }
}
}
Loading