Skip to content

Commit

Permalink
Merge pull request #3353 from microsoft/python/bugfix-missing-pathpar…
Browse files Browse the repository at this point in the history
…ameters

Python/bugfix missing path parameters
  • Loading branch information
samwelkanda authored Sep 25, 2023
2 parents bff7fc1 + 6d0630f commit 8032c4e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
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

0 comments on commit 8032c4e

Please sign in to comment.