Skip to content

Commit

Permalink
Merge pull request #3420 from microsoft/fix/composed-wrapper-name
Browse files Browse the repository at this point in the history
Rename composed type wrapper when another class with similar name exists in namespace
  • Loading branch information
baywet authored Oct 5, 2023
2 parents a5c6afe + f39f2e5 commit d17bab2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Log a message to stderr if a request is skipped due to missing data. (CLI) [#2210](https://github.com/microsoft/kiota/issues/2210)
- Fixes code file generation in typescript [#3419](https://github.com/microsoft/kiota/issues/3419)
- Writes fully qualified name of custom types when a type with a similar name exists in the class in PHP.
- Rename composed type wrapper when class already exists in the namespace. [#2964](https://github.com/microsoft/kiota/issues/2964)

## [1.6.1] - 2023-09-11

Expand Down
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 CodeClass { OriginalComposedType: null })
codeComposedType.Name = $"{codeComposedType.Name}Wrapper";
newClass = @namespace.AddClass(new CodeClass
{
Name = codeComposedType.Name,
Expand Down
53 changes: 53 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,57 @@ 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" });

parent.AddProperty(new CodeProperty
{
Name = "property",
Type = composedType
});
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 d17bab2

Please sign in to comment.