From 1aac1ac6ee35aea625be66197b792a00b1895fcc Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Fri, 22 Sep 2023 16:15:43 +0300 Subject: [PATCH 1/7] Set passed path parameters in request builder constructor --- .../Writers/Python/CodeMethodWriter.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs index 9c04fbd605..8d54a81580 100644 --- a/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs @@ -4,6 +4,7 @@ using Kiota.Builder.CodeDOM; using Kiota.Builder.Extensions; +using Microsoft.OpenApi.Models; namespace Kiota.Builder.Writers.Python; public class CodeMethodWriter : BaseElementWriter @@ -331,7 +332,20 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho parentClass.Properties.FirstOrDefaultOfKind(CodePropertyKind.UrlTemplate) is CodeProperty urlTemplateProperty) { if (currentMethod.Parameters.OfKind(CodeParameterKind.PathParameters) is CodeParameter pathParametersParameter) + { + writer.StartBlock($"if isinstance({pathParametersParameter.Name.ToSnakeCase()}, dict):"); + currentMethod.Parameters.Where(static parameter => parameter.IsOfKind(CodeParameterKind.Path)).ToList() + .ForEach(parameter => + { + var key = String.IsNullOrEmpty(parameter.SerializationName) + ? parameter.Name + : parameter.SerializationName; + writer.WriteLine($"{pathParametersParameter.Name.ToSnakeCase()}['{key}'] = str({parameter.Name.ToSnakeCase()})"); + }); + writer.DecreaseIndent(); writer.WriteLine($"super().__init__({requestAdapterParameter.Name.ToSnakeCase()}, {urlTemplateProperty.DefaultValue ?? ""}, {pathParametersParameter.Name.ToSnakeCase()})"); + } + else writer.WriteLine($"super().__init__({requestAdapterParameter.Name.ToSnakeCase()}, {urlTemplateProperty.DefaultValue ?? ""}, None)"); } From 12cafb8db035ddd9880f4a779ba7edee353c6346 Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Fri, 22 Sep 2023 16:15:58 +0300 Subject: [PATCH 2/7] Add tests --- .../Writers/Python/CodeMethodWriterTests.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/Kiota.Builder.Tests/Writers/Python/CodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/Python/CodeMethodWriterTests.cs index cb284b1c02..7e22bfab7b 100644 --- a/tests/Kiota.Builder.Tests/Writers/Python/CodeMethodWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/Python/CodeMethodWriterTests.cs @@ -1627,10 +1627,24 @@ public void WritesConstructorForRequestBuilderWithRequestAdapterAndPathParameter IsNullable = true, }, }); + method.AddParameter(new CodeParameter + { + Kind = CodeParameterKind.Path, + Name = "username", + Optional = true, + Type = new CodeType + { + Name = "string", + IsNullable = true + } + }); writer.Write(method); var result = tw.ToString(); Assert.DoesNotContain("super().__init__(self)", result); - Assert.Contains("def __init__(self,request_adapter: Optional[RequestAdapter] = None, path_parameters: Optional[Union[Dict[str, Any], str]] = None)", result); + Assert.Contains("def __init__(self,request_adapter: Optional[RequestAdapter] = None, path_parameters: Optional[Union[Dict[str, Any], str]] = None,", result); + Assert.Contains("username: Optional[str] = None", result); + Assert.Contains("if isinstance(path_parameters, dict):", result); + Assert.Contains("path_parameters['username'] = str(username)", result); Assert.DoesNotContain("This property has a description", result); Assert.DoesNotContain($"self.{propName}: Optional[str] = {defaultValue}", result); Assert.DoesNotContain("get_path_parameters(", result); From eaf183c61c604decb783e7a6102b30a115fe81e6 Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Fri, 22 Sep 2023 16:29:13 +0300 Subject: [PATCH 3/7] Add CHANGELOG entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2732e6fce3..acbac77413 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Updated constructor for request builders in Python to set passed path parameters. [#3352](https://github.com/microsoft/kiota/issues/3352) - Localhost based descriptions are not cached anymore to facilitate development workflows. [#3316](https://github.com/microsoft/kiota/issues/3316) - Fixed a bug where inline composed types for components schemas would have the wrong name. [#3067](https://github.com/microsoft/kiota/issues/3067) - Changed parameter order in with_url method body to match the signature of RequestBuilder constructor in Python. [#3328](https://github.com/microsoft/kiota/issues/3328) From d5f07443d55b32b46e3d23d7dab9197d7d0d4254 Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Fri, 22 Sep 2023 18:19:26 +0300 Subject: [PATCH 4/7] Fix indentation errors --- .../Writers/Python/CodeMethodWriter.cs | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs index 8d54a81580..d28d21d5d7 100644 --- a/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; - +using System.Reflection.Metadata; using Kiota.Builder.CodeDOM; using Kiota.Builder.Extensions; using Microsoft.OpenApi.Models; @@ -333,15 +333,19 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho { if (currentMethod.Parameters.OfKind(CodeParameterKind.PathParameters) is CodeParameter pathParametersParameter) { - writer.StartBlock($"if isinstance({pathParametersParameter.Name.ToSnakeCase()}, dict):"); - currentMethod.Parameters.Where(static parameter => parameter.IsOfKind(CodeParameterKind.Path)).ToList() - .ForEach(parameter => + var pathParameters = currentMethod.Parameters + .Where(static x => x.IsOfKind(CodeParameterKind.Path)) + .Select(static x => (string.IsNullOrEmpty(x.SerializationName) ? x.Name : x.SerializationName, x.Name.ToSnakeCase())) + .ToArray(); + if (pathParameters.Any()) + { + writer.StartBlock($"if isinstance({pathParametersParameter.Name.ToSnakeCase()}, dict):"); + foreach (var parameter in pathParameters) { - var key = String.IsNullOrEmpty(parameter.SerializationName) - ? parameter.Name - : parameter.SerializationName; - writer.WriteLine($"{pathParametersParameter.Name.ToSnakeCase()}['{key}'] = str({parameter.Name.ToSnakeCase()})"); - }); + var (name, identName) = parameter; + writer.WriteLine($"{pathParametersParameter.Name.ToSnakeCase()}['{name}'] = str({identName})"); + } + } writer.DecreaseIndent(); writer.WriteLine($"super().__init__({requestAdapterParameter.Name.ToSnakeCase()}, {urlTemplateProperty.DefaultValue ?? ""}, {pathParametersParameter.Name.ToSnakeCase()})"); } From 56c60ffc1557f166276ed099a5596a096c893e4e Mon Sep 17 00:00:00 2001 From: samwelkanda Date: Fri, 22 Sep 2023 18:49:20 +0300 Subject: [PATCH 5/7] Fix wrong indentation --- src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs index d28d21d5d7..ba4db1de1c 100644 --- a/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs @@ -345,11 +345,10 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho var (name, identName) = parameter; writer.WriteLine($"{pathParametersParameter.Name.ToSnakeCase()}['{name}'] = str({identName})"); } + writer.DecreaseIndent(); } - writer.DecreaseIndent(); writer.WriteLine($"super().__init__({requestAdapterParameter.Name.ToSnakeCase()}, {urlTemplateProperty.DefaultValue ?? ""}, {pathParametersParameter.Name.ToSnakeCase()})"); } - else writer.WriteLine($"super().__init__({requestAdapterParameter.Name.ToSnakeCase()}, {urlTemplateProperty.DefaultValue ?? ""}, None)"); } From 3e9d668f9bf96dc453cd0035c50ce405fc03958b Mon Sep 17 00:00:00 2001 From: Samwel K <40166690+samwelkanda@users.noreply.github.com> Date: Sat, 23 Sep 2023 08:41:20 +0300 Subject: [PATCH 6/7] Update src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs Co-authored-by: Vincent Biret --- src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs index ba4db1de1c..a1266de245 100644 --- a/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Reflection.Metadata; using Kiota.Builder.CodeDOM; using Kiota.Builder.Extensions; using Microsoft.OpenApi.Models; From 4818963c1e3335c9f88a8890125417441f4ca44e Mon Sep 17 00:00:00 2001 From: Samwel K <40166690+samwelkanda@users.noreply.github.com> Date: Sat, 23 Sep 2023 08:41:31 +0300 Subject: [PATCH 7/7] Update src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs Co-authored-by: Vincent Biret --- src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs index a1266de245..33267408b6 100644 --- a/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs @@ -3,7 +3,6 @@ using System.Linq; using Kiota.Builder.CodeDOM; using Kiota.Builder.Extensions; -using Microsoft.OpenApi.Models; namespace Kiota.Builder.Writers.Python; public class CodeMethodWriter : BaseElementWriter