Skip to content

Commit

Permalink
Rename wrapper class only if existing class is not a composed type wr…
Browse files Browse the repository at this point in the history
…apper
  • Loading branch information
Ndiritu committed Oct 4, 2023
1 parent 3e9f47e commit a492afd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,8 @@ private static CodeTypeBase ConvertComposedTypeToWrapper(CodeClass codeClass, Co
if (!supportsInnerClasses)
{
var @namespace = codeClass.GetImmediateParentOfType<CodeNamespace>();
if (@namespace.FindChildByName<CodeClass>(codeComposedType.Name, false) is not null)
var existingClass = @namespace.FindChildByName<CodeClass>(codeComposedType.Name, false);
if (existingClass != null && existingClass.OriginalComposedType == null)
codeComposedType.Name = $"{codeComposedType.Name}Wrapper";
newClass = @namespace.AddClass(new CodeClass
{
Expand Down
32 changes: 30 additions & 2 deletions tests/Kiota.Builder.Tests/Refiners/PhpLanguageRefinerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CodeClass>("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<CodeClass>("Union", false) is CodeClass unionTypeWrapper && unionTypeWrapper.OriginalComposedType != null);
Assert.True(root.FindChildByName<CodeClass>("UnionWrapper", false) is null);
}

}

0 comments on commit a492afd

Please sign in to comment.