diff --git a/src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs b/src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs index 6b8f9aee1e..c9b1e4e2f0 100644 --- a/src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs +++ b/src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs @@ -479,7 +479,8 @@ private static CodeTypeBase ConvertComposedTypeToWrapper(CodeClass codeClass, Co if (!supportsInnerClasses) { var @namespace = codeClass.GetImmediateParentOfType(); - if (@namespace.FindChildByName(codeComposedType.Name, false) is not null) + var existingClass = @namespace.FindChildByName(codeComposedType.Name, false); + if (existingClass != null && existingClass.OriginalComposedType == null) codeComposedType.Name = $"{codeComposedType.Name}Wrapper"; newClass = @namespace.AddClass(new CodeClass { diff --git a/tests/Kiota.Builder.Tests/Refiners/PhpLanguageRefinerTests.cs b/tests/Kiota.Builder.Tests/Refiners/PhpLanguageRefinerTests.cs index 56e2d5a7f2..ddebaaac77 100644 --- a/tests/Kiota.Builder.Tests/Refiners/PhpLanguageRefinerTests.cs +++ b/tests/Kiota.Builder.Tests/Refiners/PhpLanguageRefinerTests.cs @@ -253,13 +253,41 @@ public async Task RenamesComposedTypeWrapperWhenSimilarClassExistsInNamespace() var composedType = new CodeUnionType { Name = "Union" }; composedType.AddType(new CodeType { Name = "string" }, new CodeType { Name = "int" }); - var composedProperty = parent.AddProperty(new CodeProperty + parent.AddProperty(new CodeProperty { Name = "property", Type = composedType - }).First(); + }); await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.PHP }, root); Assert.NotNull(root.FindChildByName("UnionWrapper", false)); } + + [Fact] + public async Task DoesNotCreateDuplicateComposedTypeWrapperIfOneAlreadyExists() + { + var composedType = new CodeUnionType { Name = "Union" }; + composedType.AddType(new CodeType { Name = "string" }, new CodeType { Name = "int" }); + + var parent = new CodeClass + { + Name = "Parent" + }; + parent.AddProperty(new CodeProperty + { + Name = "property", + Type = composedType + }); + parent.AddProperty(new CodeProperty + { + Name = "property2", + Type = composedType + }); + root.AddClass(parent); + + await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.PHP }, root); + Assert.True(root.FindChildByName("Union", false) is CodeClass unionTypeWrapper && unionTypeWrapper.OriginalComposedType != null); + Assert.True(root.FindChildByName("UnionWrapper", false) is null); + } + }