From c885247dbd3d9a8f68710ec57cc16b48499aee7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Thu, 6 Jun 2024 17:09:02 +0200 Subject: [PATCH] Revert "Merge pull request #4751 from peterwurzinger/fix/4475" This reverts commit c05fb2535ac4d639b4141668a2996833a8ff9ab1. --- CHANGELOG.md | 1 - .../Writers/CSharp/CSharpConventionService.cs | 72 ++++++++- .../Writers/CSharp/CodeMethodWriter.cs | 16 +- .../CSharp/TypeDefinitionExtensions.cs | 44 ------ .../Writers/CLI/CliCodeMethodWriterTests.cs | 20 +-- .../Writers/CSharp/CodeMethodWriterTests.cs | 6 +- .../Writers/CSharp/CodePropertyWriterTests.cs | 8 +- .../CSharp/TypeDefinitionExtensionsTests.cs | 140 ------------------ 8 files changed, 91 insertions(+), 216 deletions(-) delete mode 100644 src/Kiota.Builder/Writers/CSharp/TypeDefinitionExtensions.cs delete mode 100644 tests/Kiota.Builder.Tests/Writers/CSharp/TypeDefinitionExtensionsTests.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index d354665a1f..fc743f4fdc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,7 +52,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixes a bug where indexers in include/exclude patters were not normalized if the indexer was the last segment without a slash at the end [#4715](https://github.com/microsoft/kiota/issues/4715) - Fixes a bug where CLI generation doesn't handle parameters of type string array. [#4707](https://github.com/microsoft/kiota/issues/4707) - Fixed a bug where models would not be created when a multipart content schema existed with no encoding [#4734](https://github.com/microsoft/kiota/issues/4734) -- Types generated by Kiota are now referenced with their full name to avoid namespace ambiguities [#4475](https://github.com/microsoft/kiota/issues/4475) - Fixes a bug where warnings about discriminator not being inherited were generated [#4761](https://github.com/microsoft/kiota/issues/4761) - Fix scalar member composed type serialization in PHP [#2827](https://github.com/microsoft/kiota/issues/2827) - Trims unused components from output openApi document when generating plugins [#4672](https://github.com/microsoft/kiota/issues/4672) diff --git a/src/Kiota.Builder/Writers/CSharp/CSharpConventionService.cs b/src/Kiota.Builder/Writers/CSharp/CSharpConventionService.cs index 9522fa678e..5a5c1309e8 100644 --- a/src/Kiota.Builder/Writers/CSharp/CSharpConventionService.cs +++ b/src/Kiota.Builder/Writers/CSharp/CSharpConventionService.cs @@ -177,7 +177,7 @@ public string GetTypeString(CodeTypeBase code, CodeElement targetElement, bool i throw new InvalidOperationException($"CSharp does not support union types, the union type {code.Name} should have been filtered out by the refiner"); if (code is CodeType currentType) { - var typeName = TranslateType(currentType); + var typeName = TranslateTypeAndAvoidUsingNamespaceSegmentNames(currentType, targetElement); var nullableSuffix = ShouldTypeHaveNullableMarker(code, typeName) && includeNullableInformation ? NullableMarkerAsString : string.Empty; var collectionPrefix = currentType.CollectionKind == CodeTypeCollectionKind.Complex && includeCollectionInformation ? "List<" : string.Empty; var collectionSuffix = currentType.CollectionKind switch @@ -196,14 +196,76 @@ public string GetTypeString(CodeTypeBase code, CodeElement targetElement, bool i throw new InvalidOperationException($"type of type {code?.GetType()} is unknown"); } + private string TranslateTypeAndAvoidUsingNamespaceSegmentNames(CodeType currentType, CodeElement targetElement) + { + var parentElementsHash = targetElement.Parent is CodeClass parentClass ? + parentClass.Methods.Select(static x => x.Name) + .Union(parentClass.Properties.Select(static x => x.Name)) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToHashSet(StringComparer.OrdinalIgnoreCase) : + new HashSet(0, StringComparer.OrdinalIgnoreCase); - public override string TranslateType(CodeType type) + var typeName = TranslateType(currentType); + var areElementsInSameNamesSpace = DoesTypeExistsInSameNamesSpaceAsTarget(currentType, targetElement); + if (currentType.TypeDefinition != null && + ( + GetNamesInUseByNamespaceSegments(targetElement).Contains(typeName) && !areElementsInSameNamesSpace // match if elements are not in the same namespace and the type name is used in the namespace segments + || parentElementsHash.Contains(typeName) // match if type name is used in the parent elements segments + || !areElementsInSameNamesSpace && DoesTypeExistsInTargetAncestorNamespace(currentType, targetElement) // match if elements are not in the same namespace and the type exists in target ancestor namespace + || !areElementsInSameNamesSpace && DoesTypeExistsInOtherImportedNamespaces(currentType, targetElement) // match if elements is not imported already by another namespace. + ) + ) + return $"{currentType.TypeDefinition.GetImmediateParentOfType().Name}.{typeName}"; + return typeName; + } + + private static bool DoesTypeExistsInSameNamesSpaceAsTarget(CodeType currentType, CodeElement targetElement) { - ArgumentNullException.ThrowIfNull(type); + return currentType?.TypeDefinition?.GetImmediateParentOfType()?.Name.Equals(targetElement?.GetImmediateParentOfType()?.Name, StringComparison.OrdinalIgnoreCase) ?? false; + } + + private static bool DoesTypeExistsInTargetAncestorNamespace(CodeType currentType, CodeElement targetElement) + { + // Avoid type ambiguity on similarly named classes. Currently, if we have namespaces A and A.B where both namespaces have type T, + // Trying to use type A.B.T in namespace A without using a qualified name will break the build. + // Similarly, if we have type A.B.C.D.T1 that needs to be used within type A.B.C.T2, but there's also a type + // A.B.T1, using T1 in T2 will resolve A.B.T1 even if you have a using statement with A.B.C.D. + var hasChildWithName = false; + if (currentType != null && currentType.TypeDefinition != null && !currentType.IsExternal && targetElement != null) + { + var typeName = currentType.TypeDefinition.Name; + var ns = targetElement.GetImmediateParentOfType(); + var rootNs = ns?.GetRootNamespace(); + while (ns is not null && ns != rootNs && !hasChildWithName) + { + hasChildWithName = ns.GetChildElements(true).OfType().Any(c => c.Name?.Equals(typeName, StringComparison.OrdinalIgnoreCase) == true); + ns = ns.Parent is CodeNamespace n ? n : (ns.GetImmediateParentOfType()); + } + } + return hasChildWithName; + } - if (type.TypeDefinition is ITypeDefinition typeDefinition) - return typeDefinition.GetFullName(); + private static bool DoesTypeExistsInOtherImportedNamespaces(CodeType currentType, CodeElement targetElement) + { + if (currentType.TypeDefinition is CodeClass { Parent: CodeNamespace currentTypeNamespace } codeClass) + { + var targetClass = targetElement.GetImmediateParentOfType(); + var importedNamespaces = targetClass.StartBlock.Usings + .Where(codeUsing => !codeUsing.IsExternal // 1. Are defined during generation(not external) + && codeUsing.Declaration?.TypeDefinition != null + && !codeUsing.Name.Equals(currentTypeNamespace.Name, StringComparison.OrdinalIgnoreCase)) // 2. Do not match the namespace of the current type + .Select(static codeUsing => codeUsing.Declaration!.TypeDefinition!.GetImmediateParentOfType()) + .DistinctBy(static declaredNamespace => declaredNamespace.Name); + return importedNamespaces.Any(importedNamespace => (importedNamespace.FindChildByName(codeClass.Name, false) != null) + || (importedNamespace.FindChildByName(codeClass.Name, false) != null)); + } + return false; + } + + public override string TranslateType(CodeType type) + { + ArgumentNullException.ThrowIfNull(type); return type.Name switch { "integer" => "int", diff --git a/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs index 796e6a91ac..66d29a759f 100644 --- a/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs @@ -105,9 +105,7 @@ private void WriteRawUrlBuilderBody(CodeClass parentClass, CodeMethod codeElemen { var rawUrlParameter = codeElement.Parameters.OfKind(CodeParameterKind.RawUrl) ?? throw new InvalidOperationException("RawUrlBuilder method should have a RawUrl parameter"); var requestAdapterProperty = parentClass.GetPropertyOfKind(CodePropertyKind.RequestAdapter) ?? throw new InvalidOperationException("RawUrlBuilder method should have a RequestAdapter property"); - - var fullName = parentClass.GetFullName(); - writer.WriteLine($"return new {fullName}({rawUrlParameter.Name.ToFirstCharacterLowerCase()}, {requestAdapterProperty.Name.ToFirstCharacterUpperCase()});"); + writer.WriteLine($"return new {parentClass.Name.ToFirstCharacterUpperCase()}({rawUrlParameter.Name.ToFirstCharacterLowerCase()}, {requestAdapterProperty.Name.ToFirstCharacterUpperCase()});"); } private static readonly CodePropertyTypeComparer CodePropertyTypeForwardComparer = new(); private static readonly CodePropertyTypeComparer CodePropertyTypeBackwardComparer = new(true); @@ -119,13 +117,13 @@ private void WriteFactoryMethodBodyForInheritedModel(CodeMethod codeElement, Cod { writer.WriteLine($"\"{mappedType.Key}\" => new {conventions.GetTypeString(mappedType.Value.AllTypes.First(), codeElement)}(),"); } - writer.WriteLine($"_ => new {parentClass.GetFullName()}(),"); + writer.WriteLine($"_ => new {parentClass.Name.ToFirstCharacterUpperCase()}(),"); writer.CloseBlock("};"); } private const string ResultVarName = "result"; private void WriteFactoryMethodBodyForUnionModel(CodeMethod codeElement, CodeClass parentClass, CodeParameter parseNodeParameter, LanguageWriter writer) { - writer.WriteLine($"var {ResultVarName} = new {parentClass.GetFullName()}();"); + writer.WriteLine($"var {ResultVarName} = new {parentClass.Name.ToFirstCharacterUpperCase()}();"); var includeElse = false; foreach (var property in parentClass.GetPropertiesOfKind(CodePropertyKind.Custom) .OrderBy(static x => x, CodePropertyTypeForwardComparer) @@ -152,7 +150,7 @@ private void WriteFactoryMethodBodyForUnionModel(CodeMethod codeElement, CodeCla } private void WriteFactoryMethodBodyForIntersectionModel(CodeMethod codeElement, CodeClass parentClass, CodeParameter parseNodeParameter, LanguageWriter writer) { - writer.WriteLine($"var {ResultVarName} = new {parentClass.GetFullName()}();"); + writer.WriteLine($"var {ResultVarName} = new {parentClass.Name.ToFirstCharacterUpperCase()}();"); var includeElse = false; foreach (var property in parentClass.GetPropertiesOfKind(CodePropertyKind.Custom) .Where(static x => x.Type is not CodeType propertyType || propertyType.IsCollection || propertyType.TypeDefinition is not CodeClass) @@ -204,7 +202,7 @@ private void WriteFactoryMethodBody(CodeMethod codeElement, CodeClass parentClas else if (parentClass.DiscriminatorInformation.ShouldWriteDiscriminatorForIntersectionType) WriteFactoryMethodBodyForIntersectionModel(codeElement, parentClass, parseNodeParameter, writer); else - writer.WriteLine($"return new {parentClass.GetFullName()}();"); + writer.WriteLine($"return new {parentClass.Name.ToFirstCharacterUpperCase()}();"); } private void WriteRequestBuilderBody(CodeClass parentClass, CodeMethod codeElement, LanguageWriter writer) { @@ -359,7 +357,7 @@ private string GetDeserializationMethodName(CodeTypeBase propType, CodeMethod me return $"GetCollectionOfObjectValues<{propertyType}>({propertyType}.CreateFromDiscriminatorValue){collectionMethod}"; } else if (currentType.TypeDefinition is CodeEnum enumType) - return $"GetEnumValue<{enumType.GetFullName()}>()"; + return $"GetEnumValue<{enumType.Name.ToFirstCharacterUpperCase()}>()"; } return propertyType switch { @@ -664,7 +662,7 @@ private string GetSerializationMethodName(CodeTypeBase propType, CodeMethod meth else return $"WriteCollectionOfObjectValues<{propertyType}>"; else if (currentType.TypeDefinition is CodeEnum enumType) - return $"WriteEnumValue<{enumType.GetFullName()}>"; + return $"WriteEnumValue<{enumType.Name.ToFirstCharacterUpperCase()}>"; } return propertyType switch diff --git a/src/Kiota.Builder/Writers/CSharp/TypeDefinitionExtensions.cs b/src/Kiota.Builder/Writers/CSharp/TypeDefinitionExtensions.cs deleted file mode 100644 index c6ef2fac85..0000000000 --- a/src/Kiota.Builder/Writers/CSharp/TypeDefinitionExtensions.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.Text; -using Kiota.Builder.CodeDOM; -using Kiota.Builder.Extensions; - -namespace Kiota.Builder.Writers.CSharp; - -internal static class TypeDefinitionExtensions -{ - public static string GetFullName(this ITypeDefinition typeDefinition) - { - ArgumentNullException.ThrowIfNull(typeDefinition); - - var fullNameBuilder = new StringBuilder(); - return AppendTypeName(typeDefinition, fullNameBuilder).ToString(); - } - - private static StringBuilder AppendTypeName(ITypeDefinition typeDefinition, StringBuilder fullNameBuilder) - { - if (string.IsNullOrEmpty(typeDefinition.Name)) - throw new ArgumentException("Cannot append a full name for a type without a name.", nameof(typeDefinition)); - - fullNameBuilder.Insert(0, typeDefinition.Name.ToFirstCharacterUpperCase()); - switch (typeDefinition.Parent) - { - case null: - return fullNameBuilder; - case ITypeDefinition parentTypeDefinition: - { - fullNameBuilder.Insert(0, '.'); - return AppendTypeName(parentTypeDefinition, fullNameBuilder); - } - case CodeNamespace codeNamespace: - { - if (!string.IsNullOrEmpty(codeNamespace.Name)) - fullNameBuilder.Insert(0, $"{codeNamespace.Name}."); - - return fullNameBuilder; - } - default: - throw new InvalidOperationException($"Type {typeDefinition.Name} contains an invalid parent of type {typeDefinition.Parent.GetType().FullName}."); - } - } -} diff --git a/tests/Kiota.Builder.Tests/Writers/CLI/CliCodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/CLI/CliCodeMethodWriterTests.cs index 1755ff0519..ab5d455c24 100644 --- a/tests/Kiota.Builder.Tests/Writers/CLI/CliCodeMethodWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/CLI/CliCodeMethodWriterTests.cs @@ -241,7 +241,7 @@ public void WritesIndexerCommands() writer.Write(method); var result = tw.ToString(); - Assert.Contains("var builder = new Test.Name.Sub.TestClass", result); + Assert.Contains("var builder = new TestClass", result); Assert.Contains("var commands = new List();", result); Assert.Contains("commands.Add(builder.BuildTestMethod1());", result); Assert.Contains("commands.AddRange(builder.BuildTestMethod2());", result); @@ -325,7 +325,7 @@ public void WritesMatchingIndexerCommandsIntoExecutableCommand() writer.Write(method); var result = tw.ToString(); - Assert.Contains("var testItemIdx = new Test.Name.Sub.TestItemRequestBuilder();", result); + Assert.Contains("var testItemIdx = new TestItemRequestBuilder();", result); Assert.Contains("var command = testItemIdx.BuildTestMethod1();", result); Assert.Contains("var cmds = testItemIdx.BuildTestMethod2();", result); Assert.DoesNotContain("execCommands.AddRange(cmds.Item1)", result); @@ -418,12 +418,12 @@ public void WritesMatchingIndexerCommandsIntoContainerCommand() writer.Write(method); var result = tw.ToString(); - Assert.Contains("var testItemIndexer = new Test.Name.Sub.TestIndexItemRequestBuilder();", result); + Assert.Contains("var testItemIndexer = new TestIndexItemRequestBuilder();", result); Assert.Contains("var command = testItemIndexer.BuildTestMethod1();", result); Assert.Contains("var cmds = testItemIndexer.BuildTestMethod2();", result); Assert.DoesNotContain("execCommands.AddRange(cmds.Item1);", result); Assert.Contains("nonExecCommands.AddRange(cmds.Item2);", result); - Assert.Contains("var builder = new Test.TestNavItemRequestBuilder", result); + Assert.Contains("var builder = new TestNavItemRequestBuilder", result); Assert.Contains("nonExecCommands.Add(builder.BuildTestMethod11());", result); Assert.Contains("return command;", result); Assert.DoesNotContain("nonExecCommands.Add(builder.BuildTestMethod3());", result); @@ -553,7 +553,7 @@ public void WritesNavCommandThatSkipsReusedNavCommandInstance() var result = tw.ToString(); Assert.Contains("var command = new Command(\"user\");", result); - Assert.Contains("var builder = new Test.TestNavItemRequestBuilder();", result); + Assert.Contains("var builder = new TestNavItemRequestBuilder();", result); Assert.Contains("execCommands.Add(builder.BuildExecutableTestMethod());", result); Assert.Contains("return command;", result); Assert.DoesNotContain("BuildNavTestMethod", result); @@ -596,7 +596,7 @@ public void WritesContainerCommands() var result = tw.ToString(); Assert.Contains("var command = new Command(\"user\");", result); - Assert.Contains("var builder = new Test.Name.Sub1.Sub2.TestClass1", result); + Assert.Contains("var builder = new TestClass1", result); Assert.Contains("nonExecCommands.Add(builder.BuildTestMethod1());", result); Assert.Contains("nonExecCommands.Add(builder.BuildTestMethod2());", result); Assert.Contains("return command;", result); @@ -637,7 +637,7 @@ public void WritesRequestBuilderWithParametersCommands() var result = tw.ToString(); Assert.Contains("var command = new Command(\"user\");", result); - Assert.Contains("var builder = new Test.Name.Sub1.Sub2.TestClass1", result); + Assert.Contains("var builder = new TestClass1", result); Assert.Contains("nonExecCommands.Add(builder.BuildTestMethod1());", result); Assert.Contains("nonExecCommands.Add(builder.BuildTestMethod2());", result); Assert.Contains("return command;", result); @@ -1085,7 +1085,7 @@ public void WritesExecutableCommandForPostRequestWithModelBody() Assert.Contains("command.AddOption(bodyOption);", result); Assert.Contains("var body = invocationContext.ParseResult.GetValueForOption(bodyOption) ?? string.Empty;", result); Assert.Contains("using var stream = new MemoryStream(Encoding.UTF8.GetBytes(body));", result); - Assert.Contains("var model = parseNode.GetObjectValue(Test.Content.CreateFromDiscriminatorValue);", result); + Assert.Contains("var model = parseNode.GetObjectValue(Content.CreateFromDiscriminatorValue);", result); Assert.Contains("if (model is null)", result); Assert.Contains("Console.Error.WriteLine(\"No model data to send.\")", result); Assert.Contains("var requestInfo = CreatePostRequestInformation", result); @@ -1171,7 +1171,7 @@ public void WritesExecutableCommandForPostRequestWithModelBodyAndContentType() Assert.Contains("var body = invocationContext.ParseResult.GetValueForOption(bodyOption) ?? string.Empty;", result); Assert.Contains("var contentType = invocationContext.ParseResult.GetValueForOption(contentTypeOption);", result); Assert.Contains("using var stream = new MemoryStream(Encoding.UTF8.GetBytes(body));", result); - Assert.Contains("var model = parseNode.GetObjectValue(Test.Content.CreateFromDiscriminatorValue);", result); + Assert.Contains("var model = parseNode.GetObjectValue(Content.CreateFromDiscriminatorValue);", result); Assert.Contains("if (model is null)", result); Assert.Contains("Console.Error.WriteLine(\"No model data to send.\")", result); Assert.Contains("var requestInfo = CreatePostRequestInformation(model, contentType", result); @@ -1242,7 +1242,7 @@ public void WritesExecutableCommandForPostRequestWithCollectionModel() Assert.Contains("command.AddOption(bodyOption);", result); Assert.Contains("var body = invocationContext.ParseResult.GetValueForOption(bodyOption) ?? string.Empty;", result); Assert.Contains("using var stream = new MemoryStream(Encoding.UTF8.GetBytes(body));", result); - Assert.Contains("var model = parseNode.GetCollectionOfObjectValues(Test.Content.CreateFromDiscriminatorValue)?.ToList();", result); + Assert.Contains("var model = parseNode.GetCollectionOfObjectValues(Content.CreateFromDiscriminatorValue)?.ToList();", result); Assert.Contains("if (model is null)", result); Assert.Contains("Console.Error.WriteLine(\"No model data to send.\")", result); Assert.Contains("var requestInfo = CreatePostRequestInformation", result); diff --git a/tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs index 2fe8182ed3..ae87bd46ba 100644 --- a/tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs @@ -738,7 +738,7 @@ public void WritesModelFactoryBodyAndDisambiguateAmbiguousImportedTypes() Assert.Contains("var mappingValue = parseNode.GetChildNode(\"@odata.type\")?.GetStringValue()", result); Assert.Contains("return mappingValue switch", result); Assert.Contains("\"namespaceLevelOne.ConflictingModel\" => new namespaceLevelOne.ConflictingModel(),", result); //Assert the disambiguation happens due to the enum imported - Assert.Contains("_ => new models.ConflictingModelBaseClass()", result); + Assert.Contains("_ => new ConflictingModelBaseClass()", result); AssertExtensions.CurlyBracesAreClosed(result); } [Fact] @@ -1532,7 +1532,7 @@ public void WritesConstructorWithEnumValue() writer.Write(method); var result = tw.ToString(); Assert.Contains(parentClass.Name.ToFirstCharacterUpperCase(), result); - Assert.Contains($"{propName.ToFirstCharacterUpperCase()} = {modelsNamespace.Name}.{codeEnum.Name.ToFirstCharacterUpperCase()}.{defaultValue.CleanupSymbolName()}", result);//ensure symbol is cleaned up + Assert.Contains($"{propName.ToFirstCharacterUpperCase()} = {codeEnum.Name.ToFirstCharacterUpperCase()}.{defaultValue.CleanupSymbolName()}", result);//ensure symbol is cleaned up } [Fact] public void WritesConstructorAndIncludesSanitizedEnumValue() @@ -1557,7 +1557,7 @@ public void WritesConstructorAndIncludesSanitizedEnumValue() var result = tw.ToString(); Assert.Contains(parentClass.Name.ToFirstCharacterUpperCase(), result); Assert.Contains("PictureSize.Slash", result);//ensure symbol is cleaned up - Assert.Contains($"{propName.ToFirstCharacterUpperCase()} = {modelsNamespace.Name}.{codeEnum.Name.ToFirstCharacterUpperCase()}.{defaultValue.CleanupSymbolName()}", result);//ensure symbol is cleaned up + Assert.Contains($"{propName.ToFirstCharacterUpperCase()} = {codeEnum.Name.ToFirstCharacterUpperCase()}.{defaultValue.CleanupSymbolName()}", result);//ensure symbol is cleaned up } [Fact] public void WritesConstructorAndDisambiguatesEnumType() diff --git a/tests/Kiota.Builder.Tests/Writers/CSharp/CodePropertyWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/CSharp/CodePropertyWriterTests.cs index 96524a9e7f..a12cd43520 100644 --- a/tests/Kiota.Builder.Tests/Writers/CSharp/CodePropertyWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/CSharp/CodePropertyWriterTests.cs @@ -74,7 +74,7 @@ public void WritesRequestBuilder() writer.Write(property); var result = tw.ToString(); Assert.Contains("get =>", result); - Assert.Contains($"new {rootNamespace.Name}.{TypeName}", result); + Assert.Contains($"new {TypeName}", result); Assert.Contains("RequestAdapter", result); Assert.Contains("PathParameters", result); } @@ -103,7 +103,7 @@ public void MapsCustomPropertiesToBackingStore() property.Kind = CodePropertyKind.Custom; writer.Write(property); var result = tw.ToString(); - Assert.Contains("get { return BackingStore?.Get<" + rootNamespace.Name + ".SomeCustomClass>(\"propertyName\"); }", result); + Assert.Contains("get { return BackingStore?.Get(\"propertyName\"); }", result); Assert.Contains("set { BackingStore?.Set(\"propertyName\", value);", result); } [Fact] @@ -113,7 +113,7 @@ public void MapsAdditionalDataPropertiesToBackingStore() property.Kind = CodePropertyKind.AdditionalData; writer.Write(property); var result = tw.ToString(); - Assert.Contains("get { return BackingStore.Get<" + rootNamespace.Name + ".SomeCustomClass>(\"propertyName\") ?? new Dictionary(); }", result); + Assert.Contains("get { return BackingStore.Get(\"propertyName\") ?? new Dictionary(); }", result); Assert.Contains("set { BackingStore.Set(\"propertyName\", value);", result); } [Fact] @@ -207,7 +207,7 @@ public void DisambiguateAmbiguousImportedTypes() // Assert: properties types are disambiguated. Assert.Contains("namespaceLevelOne.SomeCustomClass", result); - Assert.Contains($"{rootNamespace.Name}.SomeCustomClass", result); + Assert.Contains("defaultNamespace.SomeCustomClass", result); } [Fact] public void WritesDeprecationInformation() diff --git a/tests/Kiota.Builder.Tests/Writers/CSharp/TypeDefinitionExtensionsTests.cs b/tests/Kiota.Builder.Tests/Writers/CSharp/TypeDefinitionExtensionsTests.cs deleted file mode 100644 index 41c397b34f..0000000000 --- a/tests/Kiota.Builder.Tests/Writers/CSharp/TypeDefinitionExtensionsTests.cs +++ /dev/null @@ -1,140 +0,0 @@ -using System; -using Kiota.Builder.CodeDOM; -using Kiota.Builder.Writers.CSharp; - -using Xunit; - -namespace Kiota.Builder.Tests.Writers.CSharp; - -public sealed class TypeDefinitionExtensionsTests -{ - [Fact] - public void ReturnsFullNameForTypeWithoutNamespace() - { - var rootNamespace = CodeNamespace.InitRootNamespace(); - var myClass = new CodeClass - { - Name = "myClass" - }; - rootNamespace.AddClass(myClass); - - var fullName = TypeDefinitionExtensions.GetFullName(myClass); - - Assert.Equal("MyClass", fullName); - } - - [Fact] - public void ReturnsFullNameForTypeInNamespace() - { - var rootNamespace = CodeNamespace.InitRootNamespace(); - - var myNamespace = rootNamespace.AddNamespace("MyNamespace"); - var myClass = new CodeClass - { - Name = "myClass", - }; - myNamespace.AddClass(myClass); - - var fullName = TypeDefinitionExtensions.GetFullName(myClass); - - Assert.Equal("MyNamespace.MyClass", fullName); - } - - [Fact] - public void ReturnsFullNameForNestedTypes() - { - var rootNamespace = CodeNamespace.InitRootNamespace(); - - var myNamespace = rootNamespace.AddNamespace("MyNamespace"); - - var myParentClass = new CodeClass - { - Name = "myParentClass" - }; - myNamespace.AddClass(myParentClass); - - var myNestedClass = new CodeClass - { - Name = "myNestedClass", - }; - myParentClass.AddInnerClass(myNestedClass); - - var parentClassFullName = TypeDefinitionExtensions.GetFullName(myParentClass); - var nestedClassFullName = TypeDefinitionExtensions.GetFullName(myNestedClass); - - Assert.Equal("MyNamespace.MyParentClass", parentClassFullName); - Assert.Equal("MyNamespace.MyParentClass.MyNestedClass", nestedClassFullName); - } - - [Fact] - public void ThrowsIfTypeIsNull() - { - Assert.Throws("typeDefinition", () => TypeDefinitionExtensions.GetFullName(null!)); - } - - [Theory] - [InlineData("")] - [InlineData(null)] - public void ThrowsIfTypeDoesNotHaveAName(string typeName) - { - var myClass = new CodeClass - { - Name = typeName - }; - - Assert.Throws("typeDefinition", () => TypeDefinitionExtensions.GetFullName(myClass)); - } - - [Fact] - public void ThrowsIfTypesParentIsInvalid() - { - var myClass = new CodeClass - { - Name = "myClass", - Parent = new CodeConstant() - }; - - Assert.Throws(() => TypeDefinitionExtensions.GetFullName(myClass)); - } - - [Fact] - public void CapitalizesTypeNamesInTypeHierarchyButNotTheNamespace() - { - var rootNamespace = CodeNamespace.InitRootNamespace(); - var myNamespace = rootNamespace.AddNamespace("myNamespace"); - - var myParentClass = new CodeClass - { - Name = "myParentClass" - }; - myNamespace.AddClass(myParentClass); - - var myNestedClass = new CodeClass - { - Name = "myNestedClass", - }; - myParentClass.AddInnerClass(myNestedClass); - - var nestedClassFullName = TypeDefinitionExtensions.GetFullName(myNestedClass); - - Assert.Equal("myNamespace.MyParentClass.MyNestedClass", nestedClassFullName); - } - - [Fact] - public void DoesNotAppendNamespaceSegmentIfNamespaceNameIsEmpty() - { - var rootNamespace = CodeNamespace.InitRootNamespace(); - var myNamespace = rootNamespace.AddNamespace("ThisWillBeEmpty"); - myNamespace.Name = ""; - - var myClass = new CodeClass - { - Name = "myClass" - }; - myNamespace.AddClass(myClass); - - var fullName = TypeDefinitionExtensions.GetFullName(myClass); - - Assert.Equal("MyClass", fullName); - } -}