Skip to content

Commit

Permalink
Rename composed type wrapper when class exists in namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Ndiritu committed Oct 3, 2023
1 parent beadfaa commit 3f4e121
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +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)
codeComposedType.Name = $"{codeComposedType.Name}Wrapper";
newClass = @namespace.AddClass(new CodeClass
{
Name = codeComposedType.Name,
Expand Down
27 changes: 27 additions & 0 deletions tests/Kiota.Builder.Tests/Refiners/PhpLanguageRefinerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,31 @@ public async Task ImportsClassForDiscriminatorReturns()
await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.PHP }, root);
Assert.Equal(2, modelClass.Usings.Count());
}

[Fact]
public async Task RenamesComposedTypeWrapperWhenSimilarClassExistsInNamespace()
{
var model = new CodeClass
{
Name = "Union",
Kind = CodeClassKind.Model
};
var parent = new CodeClass
{
Name = "Parent"
};
root.AddClass(model, parent);

var composedType = new CodeUnionType { Name = "Union" };
composedType.AddType(new CodeType { Name = "string" }, new CodeType { Name = "int"});

var composedProperty = 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));
}
}

0 comments on commit 3f4e121

Please sign in to comment.