From d74ee8e35dea9233a796fa6a974a098cbaebf232 Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Tue, 31 Jan 2023 19:46:10 +0300 Subject: [PATCH 01/16] Add support for raw urls. --- .../Writers/Php/CodeEnumWriter.cs | 2 +- .../Writers/Php/CodeMethodWriter.cs | 60 +++++++++++++------ .../Writers/Php/CodePropertyWriter.cs | 2 +- .../Writers/Php/PhpConventionService.cs | 5 +- .../Writers/Php/CodeMethodWriterTests.cs | 4 +- 5 files changed, 49 insertions(+), 24 deletions(-) diff --git a/src/Kiota.Builder/Writers/Php/CodeEnumWriter.cs b/src/Kiota.Builder/Writers/Php/CodeEnumWriter.cs index 69f2dc4cde..144d796809 100644 --- a/src/Kiota.Builder/Writers/Php/CodeEnumWriter.cs +++ b/src/Kiota.Builder/Writers/Php/CodeEnumWriter.cs @@ -27,7 +27,7 @@ public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter write !x.Declaration.Name.Equals(codeElement.Name, StringComparison.OrdinalIgnoreCase))) .Select(x => x.Declaration is { IsExternal: true } ? $"use {x.Declaration.Name.ReplaceDotsWithSlashInNamespaces()}\\{x.Name.ReplaceDotsWithSlashInNamespaces()};" - : $"use {x.Name.ReplaceDotsWithSlashInNamespaces()}\\{x.Declaration.Name.ReplaceDotsWithSlashInNamespaces()};") + : $"use {x.Name.ReplaceDotsWithSlashInNamespaces()}\\{x.Declaration?.Name.ReplaceDotsWithSlashInNamespaces()};") .Distinct() .OrderBy(x => x) .ToList() diff --git a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs index 2f9470a532..9b2e5a9c1b 100644 --- a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs @@ -71,7 +71,8 @@ public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter wri writer.WriteLine(); } - private static void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMethod, LanguageWriter writer, bool inherits) + private const string UrlTemplateTempVarName = "$urlTplParams"; + private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMethod, LanguageWriter writer, bool inherits) { if (inherits) writer.WriteLine("parent::__construct();"); @@ -101,24 +102,45 @@ private static void WriteConstructorBody(CodeClass parentClass, CodeMethod curre if (currentMethod.IsOfKind(CodeMethodKind.Constructor, CodeMethodKind.ClientConstructor)) { AssignPropertyFromParameter(parentClass, currentMethod, CodeParameterKind.RequestAdapter, CodePropertyKind.RequestAdapter, writer); - AssignPropertyFromParameter(parentClass, currentMethod, CodeParameterKind.PathParameters, CodePropertyKind.PathParameters, writer); - AssignPropertyFromParameter(parentClass, currentMethod, CodeParameterKind.RawUrl, CodePropertyKind.UrlTemplate, writer); - } - var urlTemplateTempVarName = "$urlTplParams"; - if (currentMethod.IsOfKind(CodeMethodKind.Constructor, CodeMethodKind.ClientConstructor) && - parentClass.IsOfKind(CodeClassKind.RequestBuilder) && - currentMethod.Parameters.Any(x => x.IsOfKind(CodeParameterKind.Path))) - { - if (currentMethod.Parameters.OfKind(CodeParameterKind.PathParameters) is CodeParameter pathParametersParameter) - writer.WriteLine($"{urlTemplateTempVarName} = ${pathParametersParameter.Name};"); - currentMethod.Parameters.Where(parameter => parameter.IsOfKind(CodeParameterKind.Path)).ToList() - .ForEach(parameter => - { - writer.WriteLine($"{urlTemplateTempVarName}['{parameter.Name}'] = ${parameter.Name.ToFirstCharacterLowerCase()};"); - }); - if (parentClass.GetPropertyOfKind(CodePropertyKind.PathParameters) is CodeProperty pathParametersProperty) - writer.WriteLine($"{GetPropertyCall(pathParametersProperty, "[]")} = array_merge({GetPropertyCall(pathParametersProperty, "[]")}, {urlTemplateTempVarName});"); } + var pathParametersParameter = currentMethod.Parameters.OfKind(CodeParameterKind.PathParameters); + var pathParametersProperty = + parentClass.Properties.FirstOrDefault(x => x.IsOfKind(CodePropertyKind.PathParameters)); + const string rawUrlParameterKey = "request-raw-url"; + if (parentClass.IsOfKind(CodeClassKind.RequestBuilder) && + currentMethod.IsOfKind(CodeMethodKind.Constructor, CodeMethodKind.ClientConstructor) && + pathParametersParameter != null + ) + { + var pathParametersParameterName = conventions.GetParameterName(pathParametersParameter); + writer.WriteLine($"if (is_array({pathParametersParameterName})) {{"); + writer.IncreaseIndent(); + WritePathParametersOptions(currentMethod, parentClass, pathParametersParameter, writer); + writer.DecreaseIndent(); + writer.WriteLine("} else {"); + writer.IncreaseIndent(); + writer.WriteLine($"{GetPropertyCall(pathParametersProperty, "[]")} = ['{rawUrlParameterKey}' => {conventions.GetParameterName(pathParametersParameter)}];"); + writer.CloseBlock(); + } + } + private void WritePathParametersOptions(CodeMethod currentMethod, CodeClass parentClass, CodeParameter pathParameter, LanguageWriter writer) + { + var pathParametersProperty = parentClass.GetPropertyOfKind(CodePropertyKind.PathParameters); + + if (!currentMethod.Parameters.Any(x => x.IsOfKind(CodeParameterKind.Path))) + { + writer.WriteLine($"{GetPropertyCall(pathParametersProperty, "[]")} = {conventions.GetParameterName(pathParameter)};"); + return; + } + + writer.WriteLine($"{UrlTemplateTempVarName} = {conventions.GetParameterName(pathParameter)};"); + currentMethod.Parameters.Where(parameter => parameter.IsOfKind(CodeParameterKind.Path)).ToList() + .ForEach(parameter => + { + writer.WriteLine($"{UrlTemplateTempVarName}['{parameter.Name}'] = ${parameter.Name.ToFirstCharacterLowerCase()};"); + }); + writer.WriteLine( + $"{GetPropertyCall(pathParametersProperty, "[]")} = {UrlTemplateTempVarName};"); } private static void AssignPropertyFromParameter(CodeClass parentClass, CodeMethod currentMethod, CodeParameterKind parameterKind, CodePropertyKind propertyKind, LanguageWriter writer) { @@ -415,7 +437,7 @@ private void WriteRequestBuilderWithParametersBody(string returnType, LanguageWr conventions.AddRequestBuilderBody(returnType, writer, default, element); } - private static string GetPropertyCall(CodeProperty property, string defaultValue) => property == null ? defaultValue : $"$this->{property.Name}"; + private static string GetPropertyCall(CodeProperty? property, string defaultValue) => property == null ? defaultValue : $"$this->{property.Name}"; private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams requestParams, CodeClass currentClass, LanguageWriter writer) { if (codeElement.HttpMethod == null) throw new InvalidOperationException("http method cannot be null"); diff --git a/src/Kiota.Builder/Writers/Php/CodePropertyWriter.cs b/src/Kiota.Builder/Writers/Php/CodePropertyWriter.cs index d81abcb566..a5faad2f2a 100644 --- a/src/Kiota.Builder/Writers/Php/CodePropertyWriter.cs +++ b/src/Kiota.Builder/Writers/Php/CodePropertyWriter.cs @@ -28,7 +28,7 @@ public override void WriteCodeElement(CodeProperty codeElement, LanguageWriter w private void WritePropertyDocComment(CodeProperty codeProperty, LanguageWriter writer) { var propertyDescription = codeProperty.Documentation.Description; - var hasDescription = !string.IsNullOrEmpty(propertyDescription); + var hasDescription = codeProperty.Documentation.DescriptionAvailable; var collectionKind = codeProperty.Type.IsArray || codeProperty.Type.IsCollection; var typeString = (collectionKind diff --git a/src/Kiota.Builder/Writers/Php/PhpConventionService.cs b/src/Kiota.Builder/Writers/Php/PhpConventionService.cs index d2961c38a3..5c3a55765b 100644 --- a/src/Kiota.Builder/Writers/Php/PhpConventionService.cs +++ b/src/Kiota.Builder/Writers/Php/PhpConventionService.cs @@ -76,7 +76,7 @@ public string GetParameterName(CodeParameter parameter) { CodeParameterKind.RequestConfiguration => "$requestConfiguration", CodeParameterKind.BackingStore => "$backingStore", - CodeParameterKind.PathParameters => "$pathParameters", + CodeParameterKind.PathParameters => "$pathParametersOrRawUrl", CodeParameterKind.RequestAdapter => RequestAdapterPropertyName, CodeParameterKind.RequestBody => "$body", CodeParameterKind.RawUrl => "$rawUrl", @@ -93,6 +93,7 @@ public override string GetParameterSignature(CodeParameter parameter, CodeElemen { CodeParameterKind.RequestAdapter => $"RequestAdapter {GetParameterName(parameter)}", CodeParameterKind.ResponseHandler => $"ResponseHandler {GetParameterName(parameter)}", + CodeParameterKind.PathParameters => GetParameterName(parameter), CodeParameterKind.RequestConfiguration => $"{parameter.Type.Name.ToFirstCharacterUpperCase()} {GetParameterName(parameter)}", CodeParameterKind.Serializer => $"SerializationWriter {GetParameterName(parameter)}", CodeParameterKind.BackingStore => $"{parameter.Type.Name.ToFirstCharacterUpperCase()} {GetParameterName(parameter)}", @@ -108,7 +109,7 @@ public string GetParameterDocNullable(CodeParameter parameter, CodeElement codeE var parameterSignature = GetParameterSignature(parameter, codeElement).Trim().Split(' '); if (parameter.IsOfKind(CodeParameterKind.PathParameters)) { - return $"array{(parameter.Optional ? "|null" : string.Empty)} {parameterSignature[1]}"; + return $"array|string{(parameter.Optional ? "|null" : string.Empty)} {parameterSignature[0]}"; } var isCollection = parameter.Type.IsCollection; diff --git a/tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs index 27146a5faf..0bcef68f8a 100644 --- a/tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs @@ -1160,7 +1160,9 @@ public void WriteRequestBuilderConstructor() var result = stringWriter.ToString(); Assert.Contains("__construct", result); Assert.Contains($"$this->{propName} = {defaultValue};", result); - Assert.Contains("$this->pathParameters = array_merge($this->pathParameters, $urlTplParams);", result); + Assert.Contains("if (is_array($pathParametersOrRawUrl)) {", result); + Assert.Contains("$this->pathParameters = ['request-raw-url' => $pathParametersOrRawUrl];", result); + Assert.Contains("$this->pathParameters = $urlTplParams;", result); } [Fact] From a07fb703be1bcb2e29e16aa3d800e312e82db858 Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Wed, 1 Feb 2023 11:09:08 +0300 Subject: [PATCH 02/16] Change parameter description. --- src/Kiota.Builder/Refiners/PhpRefiner.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Kiota.Builder/Refiners/PhpRefiner.cs b/src/Kiota.Builder/Refiners/PhpRefiner.cs index 3911d0ffa8..5d9c845aa4 100644 --- a/src/Kiota.Builder/Refiners/PhpRefiner.cs +++ b/src/Kiota.Builder/Refiners/PhpRefiner.cs @@ -226,12 +226,15 @@ private static void CorrectParameterType(CodeElement codeElement) { if (codeElement is CodeMethod currentMethod) { - currentMethod.Parameters.Where(x => x.IsOfKind(CodeParameterKind.ParseNode)).ToList().ForEach(static x => + currentMethod.Parameters.Where(x => x.IsOfKind(CodeParameterKind.ParseNode, CodeParameterKind.PathParameters)).ToList().ForEach(static x => { - x.Type.Name = "ParseNode"; + if (x.IsOfKind(CodeParameterKind.ParseNode)) + x.Type.Name = "ParseNode"; + else + x.Documentation.Description += " or a String representing the raw URL."; }); currentMethod.Parameters.Where(x => x.IsOfKind(CodeParameterKind.BackingStore) - && currentMethod.IsOfKind(CodeMethodKind.ClientConstructor)).ToList().ForEach(static x => + && currentMethod.IsOfKind(CodeMethodKind.ClientConstructor)).ToList().ForEach(static x => { x.Type.Name = "BackingStoreFactory"; x.DefaultValue = "null"; From eb35f4b4345f9718f5411df0fe3ef737a9bb8387 Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Wed, 1 Feb 2023 12:52:57 +0300 Subject: [PATCH 03/16] Add changelog entry. --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea719b0c8a..0454150554 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Added support for external documentation links on request execution methods (PHP Generation). [2038](https://github.com/microsoft/kiota/issues/2138) +- Added support for Raw Url in Request Builders for PHP Generation. [2205](https://github.com/microsoft/kiota/issues/2205) +- Added support for external documentation links on request execution methods (PHP Generation). [2138](https://github.com/microsoft/kiota/issues/2138) - Added support for nullable reference types in dotnet for projects running Netstandard 2.1/Net 6.0 and above [2073](https://github.com/microsoft/kiota/issues/2073) - Added support for multi-value headers to CLI generation. (Shell) - Added support for multi-value headers for PHP Generation. [#2052](https://github.com/microsoft/kiota/issues/2052) From 59b57ac6a6962bf7408c6018c34eee8adc876733 Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Thu, 2 Feb 2023 00:13:31 -0800 Subject: [PATCH 04/16] Update src/Kiota.Builder/Refiners/PhpRefiner.cs Co-authored-by: Vincent Biret --- src/Kiota.Builder/Refiners/PhpRefiner.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Kiota.Builder/Refiners/PhpRefiner.cs b/src/Kiota.Builder/Refiners/PhpRefiner.cs index 5d9c845aa4..bd694161d3 100644 --- a/src/Kiota.Builder/Refiners/PhpRefiner.cs +++ b/src/Kiota.Builder/Refiners/PhpRefiner.cs @@ -226,7 +226,7 @@ private static void CorrectParameterType(CodeElement codeElement) { if (codeElement is CodeMethod currentMethod) { - currentMethod.Parameters.Where(x => x.IsOfKind(CodeParameterKind.ParseNode, CodeParameterKind.PathParameters)).ToList().ForEach(static x => + currentMethod.Parameters.Where(static x => x.IsOfKind(CodeParameterKind.ParseNode, CodeParameterKind.PathParameters)).ToList().ForEach(static x => { if (x.IsOfKind(CodeParameterKind.ParseNode)) x.Type.Name = "ParseNode"; From 73d4c6acc601a8e14e1a039dd2258387bd11461b Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Thu, 2 Feb 2023 00:13:47 -0800 Subject: [PATCH 05/16] Update src/Kiota.Builder/Writers/Php/CodeEnumWriter.cs Co-authored-by: Vincent Biret --- src/Kiota.Builder/Writers/Php/CodeEnumWriter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Kiota.Builder/Writers/Php/CodeEnumWriter.cs b/src/Kiota.Builder/Writers/Php/CodeEnumWriter.cs index 144d796809..b7189464f9 100644 --- a/src/Kiota.Builder/Writers/Php/CodeEnumWriter.cs +++ b/src/Kiota.Builder/Writers/Php/CodeEnumWriter.cs @@ -27,7 +27,7 @@ public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter write !x.Declaration.Name.Equals(codeElement.Name, StringComparison.OrdinalIgnoreCase))) .Select(x => x.Declaration is { IsExternal: true } ? $"use {x.Declaration.Name.ReplaceDotsWithSlashInNamespaces()}\\{x.Name.ReplaceDotsWithSlashInNamespaces()};" - : $"use {x.Name.ReplaceDotsWithSlashInNamespaces()}\\{x.Declaration?.Name.ReplaceDotsWithSlashInNamespaces()};") + : $"use {x.Name.ReplaceDotsWithSlashInNamespaces()}\\{x.Declaration!.Name.ReplaceDotsWithSlashInNamespaces()};") .Distinct() .OrderBy(x => x) .ToList() From c2f8be5516f1de97f0752f2d32fa3cecaf4dc724 Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Thu, 2 Feb 2023 00:14:05 -0800 Subject: [PATCH 06/16] Update src/Kiota.Builder/Writers/Php/CodeEnumWriter.cs Co-authored-by: Vincent Biret --- src/Kiota.Builder/Writers/Php/CodeEnumWriter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Kiota.Builder/Writers/Php/CodeEnumWriter.cs b/src/Kiota.Builder/Writers/Php/CodeEnumWriter.cs index b7189464f9..8c49a0215f 100644 --- a/src/Kiota.Builder/Writers/Php/CodeEnumWriter.cs +++ b/src/Kiota.Builder/Writers/Php/CodeEnumWriter.cs @@ -29,7 +29,7 @@ public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter write ? $"use {x.Declaration.Name.ReplaceDotsWithSlashInNamespaces()}\\{x.Name.ReplaceDotsWithSlashInNamespaces()};" : $"use {x.Name.ReplaceDotsWithSlashInNamespaces()}\\{x.Declaration!.Name.ReplaceDotsWithSlashInNamespaces()};") .Distinct() - .OrderBy(x => x) + .Order() .ToList() .ForEach(x => { From d7f4516888564fd4ebb3642320fbe699ea5b1a56 Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Thu, 2 Feb 2023 00:15:09 -0800 Subject: [PATCH 07/16] Update src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs Co-authored-by: Vincent Biret --- src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs index 9b2e5a9c1b..df78ef9e8b 100644 --- a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs @@ -113,8 +113,7 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho ) { var pathParametersParameterName = conventions.GetParameterName(pathParametersParameter); - writer.WriteLine($"if (is_array({pathParametersParameterName})) {{"); - writer.IncreaseIndent(); + writer.StartBlock($"if (is_array({pathParametersParameterName})) {{"); WritePathParametersOptions(currentMethod, parentClass, pathParametersParameter, writer); writer.DecreaseIndent(); writer.WriteLine("} else {"); From 1066590ea7c6e42db1c463255562bdd9d007663d Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Thu, 2 Feb 2023 00:15:29 -0800 Subject: [PATCH 08/16] Update src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs Co-authored-by: Vincent Biret --- src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs index df78ef9e8b..f81e9c9201 100644 --- a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs @@ -115,8 +115,7 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho var pathParametersParameterName = conventions.GetParameterName(pathParametersParameter); writer.StartBlock($"if (is_array({pathParametersParameterName})) {{"); WritePathParametersOptions(currentMethod, parentClass, pathParametersParameter, writer); - writer.DecreaseIndent(); - writer.WriteLine("} else {"); + writer.CloseBlock("} else {"); writer.IncreaseIndent(); writer.WriteLine($"{GetPropertyCall(pathParametersProperty, "[]")} = ['{rawUrlParameterKey}' => {conventions.GetParameterName(pathParametersParameter)}];"); writer.CloseBlock(); From d27973cd935a2a53c711a9767b63d7a0ca05990a Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Thu, 2 Feb 2023 00:16:06 -0800 Subject: [PATCH 09/16] Update src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs Co-authored-by: Vincent Biret --- src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs index f81e9c9201..e6763cae10 100644 --- a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs @@ -109,7 +109,8 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho const string rawUrlParameterKey = "request-raw-url"; if (parentClass.IsOfKind(CodeClassKind.RequestBuilder) && currentMethod.IsOfKind(CodeMethodKind.Constructor, CodeMethodKind.ClientConstructor) && - pathParametersParameter != null + currentMethod.Parameters.OfKind(CodeParameterKind.PathParameters) is CodeParameter pathParametersParameter && + parentClass.GetPropertyOfKind(CodePropertyKind.PathParameters) is CodeProperty pathParametersProperty ) { var pathParametersParameterName = conventions.GetParameterName(pathParametersParameter); From ba829a888212c3e3f7f30fd377425bc98e972e66 Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Thu, 2 Feb 2023 00:16:24 -0800 Subject: [PATCH 10/16] Update src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs Co-authored-by: Vincent Biret --- src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs index e6763cae10..fd14480616 100644 --- a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs @@ -126,7 +126,7 @@ private void WritePathParametersOptions(CodeMethod currentMethod, CodeClass pare { var pathParametersProperty = parentClass.GetPropertyOfKind(CodePropertyKind.PathParameters); - if (!currentMethod.Parameters.Any(x => x.IsOfKind(CodeParameterKind.Path))) + if (!currentMethod.Parameters.Any(static x => x.IsOfKind(CodeParameterKind.Path))) { writer.WriteLine($"{GetPropertyCall(pathParametersProperty, "[]")} = {conventions.GetParameterName(pathParameter)};"); return; From 4b04b7043a7fc77de3518ef5b34220dbca62a867 Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Thu, 2 Feb 2023 00:16:34 -0800 Subject: [PATCH 11/16] Update src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs Co-authored-by: Vincent Biret --- src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs index fd14480616..c1cfb4472e 100644 --- a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs @@ -133,7 +133,7 @@ private void WritePathParametersOptions(CodeMethod currentMethod, CodeClass pare } writer.WriteLine($"{UrlTemplateTempVarName} = {conventions.GetParameterName(pathParameter)};"); - currentMethod.Parameters.Where(parameter => parameter.IsOfKind(CodeParameterKind.Path)).ToList() + currentMethod.Parameters.Where(static parameter => parameter.IsOfKind(CodeParameterKind.Path)).ToList() .ForEach(parameter => { writer.WriteLine($"{UrlTemplateTempVarName}['{parameter.Name}'] = ${parameter.Name.ToFirstCharacterLowerCase()};"); From b26cf1ad3c19ff027ddb03bc50edcee64a86b767 Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Thu, 2 Feb 2023 00:16:49 -0800 Subject: [PATCH 12/16] Update src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs Co-authored-by: Vincent Biret --- src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs index c1cfb4472e..e1c1b467c5 100644 --- a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs @@ -103,9 +103,6 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho { AssignPropertyFromParameter(parentClass, currentMethod, CodeParameterKind.RequestAdapter, CodePropertyKind.RequestAdapter, writer); } - var pathParametersParameter = currentMethod.Parameters.OfKind(CodeParameterKind.PathParameters); - var pathParametersProperty = - parentClass.Properties.FirstOrDefault(x => x.IsOfKind(CodePropertyKind.PathParameters)); const string rawUrlParameterKey = "request-raw-url"; if (parentClass.IsOfKind(CodeClassKind.RequestBuilder) && currentMethod.IsOfKind(CodeMethodKind.Constructor, CodeMethodKind.ClientConstructor) && From e5ec54f47c50b1ef0d43cfcf7ac0ab91cec39ee9 Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Thu, 2 Feb 2023 11:24:53 +0300 Subject: [PATCH 13/16] Add null checks --- src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs index e1c1b467c5..8d8796ae4c 100644 --- a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs @@ -72,6 +72,7 @@ public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter wri } private const string UrlTemplateTempVarName = "$urlTplParams"; + private const string RawUrlParameterKey = "request-raw-url"; private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMethod, LanguageWriter writer, bool inherits) { if (inherits) @@ -103,7 +104,6 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho { AssignPropertyFromParameter(parentClass, currentMethod, CodeParameterKind.RequestAdapter, CodePropertyKind.RequestAdapter, writer); } - const string rawUrlParameterKey = "request-raw-url"; if (parentClass.IsOfKind(CodeClassKind.RequestBuilder) && currentMethod.IsOfKind(CodeMethodKind.Constructor, CodeMethodKind.ClientConstructor) && currentMethod.Parameters.OfKind(CodeParameterKind.PathParameters) is CodeParameter pathParametersParameter && @@ -115,7 +115,7 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho WritePathParametersOptions(currentMethod, parentClass, pathParametersParameter, writer); writer.CloseBlock("} else {"); writer.IncreaseIndent(); - writer.WriteLine($"{GetPropertyCall(pathParametersProperty, "[]")} = ['{rawUrlParameterKey}' => {conventions.GetParameterName(pathParametersParameter)}];"); + writer.WriteLine($"{GetPropertyCall(pathParametersProperty, "[]")} = ['{RawUrlParameterKey}' => {conventions.GetParameterName(pathParametersParameter)}];"); writer.CloseBlock(); } } @@ -123,7 +123,7 @@ private void WritePathParametersOptions(CodeMethod currentMethod, CodeClass pare { var pathParametersProperty = parentClass.GetPropertyOfKind(CodePropertyKind.PathParameters); - if (!currentMethod.Parameters.Any(static x => x.IsOfKind(CodeParameterKind.Path))) + if (pathParametersProperty != null && !currentMethod.Parameters.Any(static x => x.IsOfKind(CodeParameterKind.Path))) { writer.WriteLine($"{GetPropertyCall(pathParametersProperty, "[]")} = {conventions.GetParameterName(pathParameter)};"); return; @@ -135,8 +135,9 @@ private void WritePathParametersOptions(CodeMethod currentMethod, CodeClass pare { writer.WriteLine($"{UrlTemplateTempVarName}['{parameter.Name}'] = ${parameter.Name.ToFirstCharacterLowerCase()};"); }); - writer.WriteLine( - $"{GetPropertyCall(pathParametersProperty, "[]")} = {UrlTemplateTempVarName};"); + if (pathParametersProperty != null) + writer.WriteLine( + $"{GetPropertyCall(pathParametersProperty, "[]")} = {UrlTemplateTempVarName};"); } private static void AssignPropertyFromParameter(CodeClass parentClass, CodeMethod currentMethod, CodeParameterKind parameterKind, CodePropertyKind propertyKind, LanguageWriter writer) { @@ -433,7 +434,7 @@ private void WriteRequestBuilderWithParametersBody(string returnType, LanguageWr conventions.AddRequestBuilderBody(returnType, writer, default, element); } - private static string GetPropertyCall(CodeProperty? property, string defaultValue) => property == null ? defaultValue : $"$this->{property.Name}"; + private static string GetPropertyCall(CodeProperty property, string defaultValue) => property == null ? defaultValue : $"$this->{property.Name}"; private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams requestParams, CodeClass currentClass, LanguageWriter writer) { if (codeElement.HttpMethod == null) throw new InvalidOperationException("http method cannot be null"); From d8834e2f66abd89dfc8d81daaf04b97037c25e06 Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Thu, 2 Feb 2023 13:36:53 +0300 Subject: [PATCH 14/16] Fix minor issue with request builder constructors. --- src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs | 13 ++++++++----- .../Writers/Php/PhpConventionService.cs | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs index 8d8796ae4c..a36596a8ec 100644 --- a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs @@ -133,7 +133,10 @@ private void WritePathParametersOptions(CodeMethod currentMethod, CodeClass pare currentMethod.Parameters.Where(static parameter => parameter.IsOfKind(CodeParameterKind.Path)).ToList() .ForEach(parameter => { - writer.WriteLine($"{UrlTemplateTempVarName}['{parameter.Name}'] = ${parameter.Name.ToFirstCharacterLowerCase()};"); + var key = String.IsNullOrEmpty(parameter.SerializationName) + ? parameter.Name + : parameter.SerializationName; + writer.WriteLine($"{UrlTemplateTempVarName}['{key}'] = ${parameter.Name.ToFirstCharacterLowerCase()};"); }); if (pathParametersProperty != null) writer.WriteLine( @@ -165,8 +168,7 @@ private void WriteMethodPhpDocs(CodeMethod codeMethod, LanguageWriter writer, IR var isSetterForAdditionalData = codeMethod.IsOfKind(CodeMethodKind.Setter) && (accessedProperty?.IsOfKind(CodePropertyKind.AdditionalData) ?? false); - var parametersWithDescription = codeMethod.Parameters - .Where(static x => x.Documentation.DescriptionAvailable) + var parametersWithOrWithoutDescription = codeMethod.Parameters .Select(x => GetParameterDocString(codeMethod, x, isSetterForAdditionalData)) .ToList(); var returnDocString = GetDocCommentReturnType(codeMethod, accessedProperty); @@ -177,7 +179,7 @@ private void WriteMethodPhpDocs(CodeMethod codeMethod, LanguageWriter writer, IR else returnDocString = String.Empty; conventions.WriteLongDescription(codeMethod.Documentation, writer, - parametersWithDescription.Union(new[] { returnDocString }) + parametersWithOrWithoutDescription.Union(new[] { returnDocString }) ); } @@ -586,11 +588,12 @@ private static void WriteDeserializerBodyForUnionModel(CodeMethod method, CodeCl private void WriteIndexerBody(CodeMethod codeElement, CodeClass parentClass, string returnType, LanguageWriter writer) { + var pathParameters = codeElement.Parameters.Where(x => x.IsOfKind(CodeParameterKind.Path, CodeParameterKind.Custom)); if (parentClass.GetPropertyOfKind(CodePropertyKind.PathParameters) is CodeProperty pathParametersProperty && codeElement.OriginalIndexer != null) conventions.AddParametersAssignment(writer, pathParametersProperty.Type, $"$this->{pathParametersProperty.Name}", (codeElement.OriginalIndexer.IndexType, codeElement.OriginalIndexer.SerializationName, "$id")); - conventions.AddRequestBuilderBody(parentClass, returnType, writer, conventions.TempDictionaryVarName); + conventions.AddRequestBuilderBody(parentClass, returnType, writer, conventions.TempDictionaryVarName, pathParameters); } private void WriteRequestExecutorBody(CodeMethod codeElement, CodeClass parentClass, RequestParams requestParams, LanguageWriter writer) diff --git a/src/Kiota.Builder/Writers/Php/PhpConventionService.cs b/src/Kiota.Builder/Writers/Php/PhpConventionService.cs index 5c3a55765b..bda07d9570 100644 --- a/src/Kiota.Builder/Writers/Php/PhpConventionService.cs +++ b/src/Kiota.Builder/Writers/Php/PhpConventionService.cs @@ -225,7 +225,7 @@ public void WriteNamespaceAndImports(ClassDeclaration codeElement, LanguageWrite internal void AddRequestBuilderBody(CodeClass parentClass, string returnType, LanguageWriter writer, string? urlTemplateVarName = default, IEnumerable? pathParameters = default) { var codeParameters = pathParameters as CodeParameter[] ?? pathParameters?.ToArray(); - var codePathParametersSuffix = !(codeParameters?.Any() ?? false) ? string.Empty : $", {string.Join(", ", codeParameters.Select(x => $"{x.Name.ToFirstCharacterLowerCase()}"))}"; + var codePathParametersSuffix = !(codeParameters?.Any() ?? false) ? string.Empty : $", {string.Join(", ", codeParameters.Select(x => $"${x.Name.ToFirstCharacterLowerCase()}"))}"; var urlTemplateParams = string.IsNullOrEmpty(urlTemplateVarName) && parentClass.GetPropertyOfKind(CodePropertyKind.PathParameters) is CodeProperty pathParametersProperty ? $"$this->{pathParametersProperty.Name}" : urlTemplateVarName; From 0c05de99d1f6a2bedcceacb20988f7ab211cc90b Mon Sep 17 00:00:00 2001 From: silaskenneth Date: Thu, 2 Feb 2023 13:44:36 +0300 Subject: [PATCH 15/16] Fix tests. --- tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs index 0bcef68f8a..c8b8c6da00 100644 --- a/tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs @@ -661,7 +661,7 @@ public async Task WriteIndexerBody() Assert.Contains("$urlTplParams['message_id'] = $id;", result); Assert.Contains("public function messageById(string $id): MessageRequestBuilder {", result); - Assert.Contains("return new MessageRequestBuilder($urlTplParams, $this->requestAdapter);", result); + Assert.Contains("return new MessageRequestBuilder($urlTplParams, $this->requestAdapter, $id);", result); } From eb636d70c93210289850f9e461f35255b5c696d8 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Thu, 2 Feb 2023 08:43:26 -0500 Subject: [PATCH 16/16] Update src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs --- src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs index a36596a8ec..34d66661bf 100644 --- a/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs @@ -588,7 +588,7 @@ private static void WriteDeserializerBodyForUnionModel(CodeMethod method, CodeCl private void WriteIndexerBody(CodeMethod codeElement, CodeClass parentClass, string returnType, LanguageWriter writer) { - var pathParameters = codeElement.Parameters.Where(x => x.IsOfKind(CodeParameterKind.Path, CodeParameterKind.Custom)); + var pathParameters = codeElement.Parameters.Where(static x => x.IsOfKind(CodeParameterKind.Path, CodeParameterKind.Custom)); if (parentClass.GetPropertyOfKind(CodePropertyKind.PathParameters) is CodeProperty pathParametersProperty && codeElement.OriginalIndexer != null) conventions.AddParametersAssignment(writer, pathParametersProperty.Type, $"$this->{pathParametersProperty.Name}",