Skip to content

Commit

Permalink
Fix request executor bug for non-duplicate collection types
Browse files Browse the repository at this point in the history
  • Loading branch information
Ndiritu committed Oct 4, 2023
1 parent 2cb185d commit be296bd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
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.GetTypeString(codeElement.ReturnType, codeElement);
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
10 changes: 7 additions & 3 deletions tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}

}

0 comments on commit be296bd

Please sign in to comment.