Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP - Fix import errors for types with similar names but different namespaces in request builders #3425

Merged
merged 5 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix null reference exception when a parameter is defined without a schema. (CLI).
- 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.

## [1.6.1] - 2023-09-11

Expand Down
10 changes: 3 additions & 7 deletions it/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
},
{
"Language": "php",
"Rationale": "https://github.com/microsoft/kiota/issues/2956"
"Rationale": "https://github.com/microsoft/kiota/issues/3029"
},
{
"Language": "python",
Expand Down Expand Up @@ -238,7 +238,7 @@
},
{
"Language": "php",
"Rationale": "https://github.com/microsoft/kiota/issues/2964"
"Rationale": "https://github.com/microsoft/kiota/issues/3029"
},
{
"Language": "java",
Expand All @@ -258,10 +258,6 @@
"Language": "go",
"Rationale": "https://github.com/microsoft/kiota/issues/2834"
},
{
"Language": "php",
"Rationale": "https://github.com/microsoft/kiota/issues/2964"
},
{
"Language": "java",
"Rationale": "https://github.com/microsoft/kiota/issues/2842"
Expand Down Expand Up @@ -322,4 +318,4 @@
}
]
}
}
}
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ private void WriteRequestExecutorBody(CodeMethod codeElement, CodeClass parentCl
joinedParams = string.Join(", ", callParams);
}

var returnTypeName = conventions.TranslateType(codeElement.ReturnType);
var returnTypeName = conventions.GetTypeString(codeElement.ReturnType, codeElement, false);
writer.WriteLine($"$requestInfo = $this->{generatorMethodName}({joinedParams});");
writer.WriteLine("try {");
writer.IncreaseIndent();
Expand Down
6 changes: 3 additions & 3 deletions src/Kiota.Builder/Writers/Php/CodePropertyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ public override void WriteCodeElement(CodeProperty codeElement, LanguageWriter w
ArgumentNullException.ThrowIfNull(codeElement);
ArgumentNullException.ThrowIfNull(writer);
if (codeElement.ExistsInExternalBaseType) return;
var returnType = conventions.GetTypeString(codeElement.Type, codeElement);
var propertyType = conventions.GetTypeString(codeElement.Type, codeElement);
var propertyName = codeElement.Name.ToFirstCharacterLowerCase();
var propertyAccess = conventions.GetAccessModifier(codeElement.Access);
switch (codeElement.Kind)
{
case CodePropertyKind.RequestBuilder:
WriteRequestBuilderBody(codeElement, writer, returnType, propertyAccess, propertyName);
WriteRequestBuilderBody(codeElement, writer, propertyType, propertyAccess, propertyName);
break;
default:
WritePropertyDocComment(codeElement, writer);
writer.WriteLine($"{propertyAccess} {(codeElement.Type.IsNullable ? "?" : string.Empty)}{returnType} ${propertyName}{(codeElement.Type.IsNullable ? " = null" : string.Empty)};");
writer.WriteLine($"{propertyAccess} {(codeElement.Type.IsNullable ? "?" : string.Empty)}{propertyType} ${propertyName}{(codeElement.Type.IsNullable ? " = null" : string.Empty)};");
break;
}
writer.WriteLine("");
Expand Down
6 changes: 5 additions & 1 deletion src/Kiota.Builder/Writers/Php/PhpConventionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ public override string GetTypeString(CodeTypeBase code, CodeElement targetElemen
throw new InvalidOperationException($"PHP does not support union types, the union type {code.Name} should have been filtered out by the refiner.");
if (code is CodeType currentType)
{
if (includeCollectionInformation && code.IsCollection)
{
return "array";
}
var typeName = TranslateType(currentType);
if (!currentType.IsExternal && IsSymbolDuplicated(typeName, targetElement) && currentType.TypeDefinition is not null)
{
return $"\\{currentType.TypeDefinition.GetImmediateParentOfType<CodeNamespace>().Name.ReplaceDotsWithSlashInNamespaces()}\\{typeName.ToFirstCharacterUpperCase()}";
}
}
return code is { IsCollection: true } ? "array" : TranslateType(code);
return TranslateType(code);
}

public override string TranslateType(CodeType type)
Expand Down
44 changes: 44 additions & 0 deletions tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2342,4 +2342,48 @@ public async void WritesQueryParameterConstructor()
Assert.Contains("$this->top = $top;", result);
}

[Fact]
public async Task WritesFullyQualifiedNameWhenSimilarTypeAlreadyExists()
{
setup();
var modelNamespace = root.AddNamespace("Models");
var nestedModelNamespace = modelNamespace.AddNamespace("Models\\Security");
var returnType1 = modelNamespace.AddClass(new CodeClass
{
Name = "ModelA"
}).First();
var returnType2 = nestedModelNamespace.AddClass(new CodeClass
{
Name = "ModelA"
}).First();
var returnType3 = root.AddClass(new CodeClass { Name = "Component" }).First();
parentClass.Kind = CodeClassKind.RequestBuilder;
parentClass.AddProperty(new CodeProperty
{
Name = "requestAdapter",
Kind = CodePropertyKind.RequestAdapter,
Type = new CodeType { Name = "RequestAdapter" }
}, new CodeProperty
{
Name = "pathParameters",
Kind = CodePropertyKind.PathParameters,
Type = new CodeType { Name = "array", CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Array }
});
var getMethod = new CodeMethod { Name = "getAsync", Kind = CodeMethodKind.RequestExecutor, HttpMethod = HttpMethod.Get, ReturnType = new CodeType { TypeDefinition = returnType1, CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Array } };
var deleteMethod = new CodeMethod { Name = "deleteAsync", Kind = CodeMethodKind.RequestExecutor, HttpMethod = HttpMethod.Delete, ReturnType = new CodeType { TypeDefinition = returnType2 } };
var testMethod = new CodeMethod { Name = "testMethod", Kind = CodeMethodKind.RequestExecutor, HttpMethod = HttpMethod.Post, ReturnType = new CodeType { TypeDefinition = returnType3, CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Array } };
parentClass.AddMethod(getMethod, deleteMethod, testMethod);

await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.PHP, UsesBackingStore = true }, root);
_codeMethodWriter = new CodeMethodWriter(new PhpConventionService(), true);
_codeMethodWriter.WriteCodeElement(getMethod, languageWriter);
_codeMethodWriter.WriteCodeElement(deleteMethod, languageWriter);
_codeMethodWriter.WriteCodeElement(testMethod, languageWriter);
var result = stringWriter.ToString();

Assert.Contains("return $this->requestAdapter->sendCollectionAsync($requestInfo, [\\Microsoft\\Graph\\Models\\ModelA::class, 'createFromDiscriminatorValue'], null);", result);
Assert.Contains("return $this->requestAdapter->sendAsync($requestInfo, [\\Microsoft\\Graph\\Models\\Security\\ModelA::class, 'createFromDiscriminatorValue'], null);", result);
Assert.Contains("return $this->requestAdapter->sendCollectionAsync($requestInfo, [Component::class, 'createFromDiscriminatorValue'], null);", result);
}

}
Loading