diff --git a/CHANGELOG.md b/CHANGELOG.md index c61e9e09e7..aa74d27e10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,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 the hints would miss quotes for paths and always use the API manifest. [#3342](https://github.com/microsoft/kiota/issues/3342) - Fixed a bug where inline composed types for components schemas would have the wrong name. [#3067](https://github.com/microsoft/kiota/issues/3067) diff --git a/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs index 9c04fbd605..33267408b6 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 Kiota.Builder.CodeDOM; using Kiota.Builder.Extensions; @@ -331,7 +330,23 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho parentClass.Properties.FirstOrDefaultOfKind(CodePropertyKind.UrlTemplate) is CodeProperty urlTemplateProperty) { if (currentMethod.Parameters.OfKind(CodeParameterKind.PathParameters) is CodeParameter pathParametersParameter) + { + 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 (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()})"); + } else writer.WriteLine($"super().__init__({requestAdapterParameter.Name.ToSnakeCase()}, {urlTemplateProperty.DefaultValue ?? ""}, None)"); } 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);