diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Primitives/CSharpType.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Primitives/CSharpType.cs index 05e97810af1..9ce217039d5 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Primitives/CSharpType.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Primitives/CSharpType.cs @@ -105,7 +105,6 @@ public CSharpType(Type type, IReadOnlyList arguments, bool isNullabl Name = type.IsGenericType ? type.Name.Substring(0, type.Name.IndexOf('`')) : type.Name; IsValueType = type.IsValueType; Namespace = type.Namespace ?? string.Empty; - FullyQualifiedName = $"{Namespace}.{Name}"; IsPublic = type.IsPublic && arguments.All(t => t.IsPublic); // open generic parameter such as the `T` in `List` is considered as declared inside the `List` type as well, but we just want this to be the pure nested type, therefore here we exclude the open generic parameter scenario // for a closed generic parameter such as the `string` in `List`, it is just an ordinary type without a `DeclaringType`. @@ -132,27 +131,6 @@ private static void ValidateArguments(Type type, IReadOnlyList argum } } - internal CSharpType( - TypeProvider implementation, - string providerNamespace, - IReadOnlyList arguments, - CSharpType? baseType) - : this( - implementation.Name, - providerNamespace, - implementation is EnumProvider || - implementation.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Struct) || - implementation.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Enum), - false, - implementation.DeclaringTypeProvider?.Type, - arguments, - implementation.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Public) && arguments.All(t => t.IsPublic), - implementation.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Struct), - baseType, - implementation.IsEnum? implementation.EnumUnderlyingType.FrameworkType : null) - { - } - internal CSharpType( string name, string ns, @@ -174,7 +152,6 @@ internal CSharpType( IsValueType = isValueType; IsNullable = isNullable; Namespace = ns; - FullyQualifiedName = $"{ns}.{name}"; DeclaringType = declaringType; IsPublic = isPublic; IsStruct = isStruct; @@ -182,9 +159,13 @@ internal CSharpType( _underlyingType = underlyingEnumType; } - public string Namespace { get; set; } - public string Name { get; private init; } - internal string FullyQualifiedName { get; private init; } + public string Namespace { get; private set; } + + /// + /// Gets or sets the name of the type. + /// + public string Name { get; private set; } + internal string FullyQualifiedName => $"{Namespace}.{Name}"; public CSharpType? DeclaringType { get; private init; } public bool IsValueType { get; private init; } public bool IsEnum => _underlyingType is not null; @@ -666,5 +647,22 @@ private CSharpType GetRootType() return returnType; } + + /// + /// Update the instance with given parameters. + /// + /// Name of the + /// Namespace of the + public void Update(string? name = null, string? @namespace = null) + { + if (name != null) + { + Name = name; + } + if (@namespace != null) + { + Namespace = @namespace; + } + } } } diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/TypeProvider.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/TypeProvider.cs index f68b476d4d1..d4f19ffca0b 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/TypeProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/TypeProvider.cs @@ -86,9 +86,7 @@ private IReadOnlyList BuildAllCustomFields() private string? _relativeFilePath; - public string Name => _name ??= CustomCodeView?.Name ?? BuildName(); - - private string? _name; + public string Name => Type.Name; protected virtual FormattableString Description { get; } = FormattableStringHelpers.Empty; @@ -106,12 +104,23 @@ public string? Deprecated private set => _deprecated = value; } + private string? _name; private CSharpType? _type; - public CSharpType Type => _type ??= new( - this, - CustomCodeView?.BuildNamespace() ?? BuildNamespace(), - GetTypeArguments(), - GetBaseType()); + private CSharpType[]? _arguments; + public CSharpType Type => _type ??= + new( + _name ??= CustomCodeView?.Name ?? BuildName(), + CustomCodeView?.BuildNamespace() ?? BuildNamespace(), + this is EnumProvider || + DeclarationModifiers.HasFlag(TypeSignatureModifiers.Struct) || + DeclarationModifiers.HasFlag(TypeSignatureModifiers.Enum), + false, + DeclaringTypeProvider?.Type, + _arguments ??= GetTypeArguments(), + DeclarationModifiers.HasFlag(TypeSignatureModifiers.Public) && _arguments.All(t => t.IsPublic), + DeclarationModifiers.HasFlag(TypeSignatureModifiers.Struct), + GetBaseType(), + IsEnum ? EnumUnderlyingType.FrameworkType : null); protected virtual bool GetIsEnum() => false; public bool IsEnum => GetIsEnum(); diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Primitives/CSharpTypeTests.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Primitives/CSharpTypeTests.cs index 0456323dfc3..cdac3801f04 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Primitives/CSharpTypeTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Primitives/CSharpTypeTests.cs @@ -49,8 +49,7 @@ public void FullyQualifiedNamePopulatedForFrameworkTypes(Type type) public void FullyQualifiedNamePopulatedForTypeProviders() { var provider = new TestTypeProvider(); - var type = new CSharpType(provider, "TestNamespace", [], null); - Assert.AreEqual("TestNamespace.TestName", type.FullyQualifiedName); + Assert.AreEqual("Test.TestName", provider.Type.FullyQualifiedName); } [TestCase(typeof(int))] diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelProviders/ClientCustomizationTests.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelProviders/ClientCustomizationTests.cs index c5ca0ce57b7..4f8e57feb9a 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelProviders/ClientCustomizationTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelProviders/ClientCustomizationTests.cs @@ -188,13 +188,13 @@ public async Task CanRemoveConstructors() { // Parameter type doesn't match new ConstructorProvider(new ConstructorSignature( - new CSharpType(client, "Samples", [typeof(int)], null), + client.Type, $"", MethodSignatureModifiers.Public, [new ParameterProvider("param1", $"", typeof(bool))]), Snippet.ThrowExpression(Snippet.Null), client), new ConstructorProvider(new ConstructorSignature( - new CSharpType(client, "Samples", [typeof(int)], null), + client.Type, $"", MethodSignatureModifiers.Public, [ @@ -203,7 +203,7 @@ [new ParameterProvider("param1", $"", typeof(bool))]), ]), Snippet.ThrowExpression(Snippet.Null), client), new ConstructorProvider(new ConstructorSignature( - new CSharpType(client, "Samples", [typeof(int)], null), + client.Type, $"", MethodSignatureModifiers.Public, [ @@ -211,7 +211,7 @@ [new ParameterProvider("param1", $"", typeof(bool))]), ]), Snippet.ThrowExpression(Snippet.Null), client), new ConstructorProvider(new ConstructorSignature( - new CSharpType(client, "Samples", [typeof(int)], null), + client.Type, $"", MethodSignatureModifiers.Public, [ @@ -240,19 +240,19 @@ public async Task DoesNotRemoveConstructorsThatDoNotMatch() var constructors = new[] { new ConstructorProvider(new ConstructorSignature( - new CSharpType(client, "Samples", [typeof(int)], null), + client.Type, $"", MethodSignatureModifiers.Public, []), Snippet.ThrowExpression(Snippet.Null), client), new ConstructorProvider(new ConstructorSignature( - new CSharpType(client, "Samples", [typeof(int)], null), + client.Type, $"", MethodSignatureModifiers.Public, [new ParameterProvider("param1", $"", typeof(bool))]), Snippet.ThrowExpression(Snippet.Null), client), new ConstructorProvider(new ConstructorSignature( - new CSharpType(client, "Samples", [typeof(int)], null), + client.Type, $"", MethodSignatureModifiers.Public, [ @@ -261,7 +261,7 @@ [new ParameterProvider("param1", $"", typeof(bool))]), ]), Snippet.ThrowExpression(Snippet.Null), client), new ConstructorProvider(new ConstructorSignature( - new CSharpType(client, "Samples", [typeof(int)], null), + client.Type, $"", MethodSignatureModifiers.Public, [