Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Python client: Populate `APIError.message` while deserializing a property marked with `x-ms-primary-error-message`. [#7542](https://github.com/microsoft/kiota/issues/7542)
- C#, Java, Go, PHP, Dart, TypeScript, Python and Ruby client: default value initialization in model classes for DateTime/Date/Time/UUID properties did not compile [#7404](https://github.com/microsoft/kiota/issues/7404)
- All languages: default value initialization in model classes for numeric/boolean properties was missing [#7404](https://github.com/microsoft/kiota/issues/7404)
- Fixed a bug where required query parameters from one HTTP operation were leaking into the path-item-level URL template, making them appear required for sibling operations on the same path. [#7292](https://github.com/microsoft/kiota/issues/7292)
Expand Down
25 changes: 20 additions & 5 deletions src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,13 +568,28 @@ private void WriteDeserializerBodyForIntersectionModel(CodeClass parentClass, La
private void WriteDeserializerBodyForInheritedModel(bool inherits, CodeMethod codeElement, CodeClass parentClass, LanguageWriter writer)
{
_codeUsingWriter.WriteInternalImports(parentClass, writer);
var customProperties = parentClass
.GetPropertiesOfKind(CodePropertyKind.Custom)
.Where(static x => !x.ExistsInBaseType)
.OrderBy(static x => x.Name)
.ToArray();
if (parentClass.IsErrorDefinition)
{
foreach (var primaryErrorMessageProperty in customProperties.Where(static x => x.IsPrimaryErrorMessage))
{
writer.StartBlock($"def deserialize_{primaryErrorMessageProperty.Name}(n: ParseNode) -> None:");
writer.WriteLine($"self.{primaryErrorMessageProperty.Name} = n.{GetDeserializationMethodName(primaryErrorMessageProperty.Type, codeElement, parentClass)}");
writer.WriteLine($"self.message = '' if self.{primaryErrorMessageProperty.Name} is None else str(self.{primaryErrorMessageProperty.Name})");
Comment on lines +578 to +582
writer.CloseBlock(string.Empty);
}
}
writer.StartBlock($"fields: dict[str, Callable[[Any], {NoneKeyword}]] = {{");
foreach (var otherProp in parentClass
.GetPropertiesOfKind(CodePropertyKind.Custom)
.Where(static x => !x.ExistsInBaseType)
.OrderBy(static x => x.Name))
foreach (var otherProp in customProperties)
{
writer.WriteLine($"\"{otherProp.WireName.SanitizeDoubleQuote()}\": lambda n : setattr(self, '{otherProp.Name}', n.{GetDeserializationMethodName(otherProp.Type, codeElement, parentClass)}),");
var deserializer = parentClass.IsErrorDefinition && otherProp.IsPrimaryErrorMessage ?
$"deserialize_{otherProp.Name}" :
$"lambda n : setattr(self, '{otherProp.Name.SanitizeSingleQuote()}', n.{GetDeserializationMethodName(otherProp.Type, codeElement, parentClass)})";
writer.WriteLine($"\"{otherProp.WireName.SanitizeDoubleQuote()}\": {deserializer},");
}
writer.CloseBlock();
if (inherits)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,32 @@ public void WritesDeSerializerBody()
Assert.Contains("defined_in_parent", result, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void WritesPrimaryErrorMessageDeserializer()
{
setup();
parentClass.IsErrorDefinition = true;
parentClass.AddProperty(new CodeProperty
{
Name = "detail",
Kind = CodePropertyKind.Custom,
IsPrimaryErrorMessage = true,
Type = new CodeType
{
Name = "string"
}
});
method.Kind = CodeMethodKind.Deserializer;
method.IsAsync = false;

writer.Write(method);
var result = tw.ToString();

Assert.Contains("def deserialize_detail(n: ParseNode) -> None:", result);
Assert.Contains("self.detail = n.get_str_value()", result);
Assert.Contains("self.message = '' if self.detail is None else str(self.detail)", result);
Assert.Contains("\"detail\": deserialize_detail,", result);
}
[Fact]
public void WritesInheritedSerializerBody()
{
setup(true);
Expand Down Expand Up @@ -1031,6 +1057,28 @@ public void EscapesWireNamesInSerializerAndDeserializerBody()
Assert.Contains("\"line1\\\"\\nline2\": lambda n : setattr(self, 'dummy_string', n.get_str_value())", deserializerResult);
}
[Fact]
public void EscapesPropertyNamesInDeserializerBody()
{
setup();
parentClass.AddProperty(new CodeProperty
{
Name = "line1'\nline2",
SerializationName = "value",
Kind = CodePropertyKind.Custom,
Type = new CodeType
{
Name = "string"
}
});
method.Kind = CodeMethodKind.Deserializer;
method.IsAsync = false;

writer.Write(method);
var result = tw.ToString();

Assert.Contains("setattr(self, 'line1\\'\\nline2', n.get_str_value())", result);
}
[Fact]
public void WritesMethodAsyncDescription()
{
setup();
Expand Down