Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python/bugfix missing path parameters #3353

Merged
merged 10 commits into from
Sep 25, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 16 additions & 1 deletion src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;

using Kiota.Builder.CodeDOM;
using Kiota.Builder.Extensions;

Expand Down Expand Up @@ -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)");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down