From be296bdd258e1cdc70454fa6eeb1216f2e7ef086 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Wed, 4 Oct 2023 21:17:19 +0300 Subject: [PATCH] Fix request executor bug for non-duplicate collection types --- src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs | 2 +- src/Kiota.Builder/Writers/Php/CodePropertyWriter.cs | 6 +++--- src/Kiota.Builder/Writers/Php/PhpConventionService.cs | 6 +++++- .../Writers/Php/CodeMethodWriterTests.cs | 10 +++++++--- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs index 7996827707..aaf53cc7e7 100644 --- a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs @@ -705,7 +705,7 @@ private void WriteRequestExecutorBody(CodeMethod codeElement, CodeClass parentCl joinedParams = string.Join(", ", callParams); } - var returnTypeName = conventions.GetTypeString(codeElement.ReturnType, codeElement); + var returnTypeName = conventions.GetTypeString(codeElement.ReturnType, codeElement, false); writer.WriteLine($"$requestInfo = $this->{generatorMethodName}({joinedParams});"); writer.WriteLine("try {"); writer.IncreaseIndent(); diff --git a/src/Kiota.Builder/Writers/Php/CodePropertyWriter.cs b/src/Kiota.Builder/Writers/Php/CodePropertyWriter.cs index c7792bb90f..fa0ef3d459 100644 --- a/src/Kiota.Builder/Writers/Php/CodePropertyWriter.cs +++ b/src/Kiota.Builder/Writers/Php/CodePropertyWriter.cs @@ -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(""); diff --git a/src/Kiota.Builder/Writers/Php/PhpConventionService.cs b/src/Kiota.Builder/Writers/Php/PhpConventionService.cs index 001a786ab9..edfb8f6246 100644 --- a/src/Kiota.Builder/Writers/Php/PhpConventionService.cs +++ b/src/Kiota.Builder/Writers/Php/PhpConventionService.cs @@ -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().Name.ReplaceDotsWithSlashInNamespaces()}\\{typeName.ToFirstCharacterUpperCase()}"; } } - return code is { IsCollection: true } ? "array" : TranslateType(code); + return TranslateType(code); } public override string TranslateType(CodeType type) diff --git a/tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs index c0ace27a8e..bee0dd083a 100644 --- a/tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs @@ -2356,6 +2356,7 @@ public async Task WritesFullyQualifiedNameWhenSimilarTypeAlreadyExists() { Name = "ModelA" }).First(); + var returnType3 = root.AddClass(new CodeClass { Name = "Component" }).First(); parentClass.Kind = CodeClassKind.RequestBuilder; parentClass.AddProperty(new CodeProperty { @@ -2368,18 +2369,21 @@ public async Task WritesFullyQualifiedNameWhenSimilarTypeAlreadyExists() 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 } }; + 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 } }; - parentClass.AddMethod(getMethod, deleteMethod); + 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->sendAsync($requestInfo, [\\Microsoft\\Graph\\Models\\ModelA::class, 'createFromDiscriminatorValue'], null);", result); + 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); } }