From 165951d02d2bafcf0fd8f750ad94a89426d1c7ba Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Tue, 5 Nov 2024 12:59:18 +0300 Subject: [PATCH 1/8] fixes python intergration test --- CHANGELOG.md | 3 ++- it/config.json | 6 +---- .../Writers/Python/PythonConventionService.cs | 2 +- .../Writers/Python/CodeMethodWriterTests.cs | 24 +++++++++++++++++++ 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 171110e1f..db46e5a9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,9 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added ### Changed + - Fixed python generation client serailization failure str being quoted as "str" - Fixed Issue with primitive values being stringified in python python. [#5417](https://github.com/microsoft/kiota/issues/5417) - - Fixed an issue where multipart request content would be ignored if other unstructured content was present in the description. [#5638](https://github.com/microsoft/kiota/issues/5638) - Fixed an issue where when generating Go code the deserializer for unions was using `CodeClass` as a filter and not `CodeInterface`. [#4844](https://github.com/microsoft/kiota/issues/4844) - Fixes mapping of `int16` format to the `integer` type rather than `double` when the type is `integer` or `number` [#5611](https://github.com/microsoft/kiota/issues/5611) @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed a bug where the type name for inherited inline models would be incorrect. [#5610](https://github.com/microsoft/kiota/issues/5610) - Fixes typing inconsistencies in generated code and libraries in Python [kiota-python#333](https://github.com/microsoft/kiota-python/issues/333) - Fixes generation of superfluous fields for Models with discriminator due to refiners adding the same properties to the same model [#4178](https://github.com/microsoft/kiota/issues/4178). +- Fixed python generation in scenarios with opening/closing tags for code comments. [#5636](https://github.com/microsoft/kiota/issues/5636) ## [1.19.1] - 2024-10-11 diff --git a/it/config.json b/it/config.json index 1e7120f80..854051e98 100644 --- a/it/config.json +++ b/it/config.json @@ -31,10 +31,6 @@ { "Language": "ruby", "Rationale": "https://github.com/microsoft/kiota/issues/1816" - }, - { - "Language": "python", - "Rationale": "https://github.com/microsoft/kiota/issues/5636" } ], "ExcludePatterns": [ @@ -262,4 +258,4 @@ "Suppressions": [], "IdempotencySuppressions": [] } -} +} \ No newline at end of file diff --git a/src/Kiota.Builder/Writers/Python/PythonConventionService.cs b/src/Kiota.Builder/Writers/Python/PythonConventionService.cs index 0daa153fd..6e335f94b 100644 --- a/src/Kiota.Builder/Writers/Python/PythonConventionService.cs +++ b/src/Kiota.Builder/Writers/Python/PythonConventionService.cs @@ -95,7 +95,7 @@ public class PythonConventionService : CommonLanguageConventionService throw new InvalidOperationException($"type of type {code.GetType()} is unknown"); } #pragma warning restore CA1822 // Method should be static - internal static string RemoveInvalidDescriptionCharacters(string originalDescription) => originalDescription.Replace("\\", "/", StringComparison.OrdinalIgnoreCase); + internal static string RemoveInvalidDescriptionCharacters(string originalDescription) => originalDescription.Replace("\\", "/", StringComparison.OrdinalIgnoreCase).Replace("\"\"\"", "\\\"\\\"\\\"", StringComparison.OrdinalIgnoreCase); public override string TranslateType(CodeType type) { ArgumentNullException.ThrowIfNull(type); diff --git a/tests/Kiota.Builder.Tests/Writers/Python/CodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/Python/CodeMethodWriterTests.cs index ae6d75a3b..eaf1873c7 100644 --- a/tests/Kiota.Builder.Tests/Writers/Python/CodeMethodWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/Python/CodeMethodWriterTests.cs @@ -1592,6 +1592,30 @@ public sealed class CodeMethodWriterTests : IDisposable Assert.DoesNotContain("get_path_parameters(", result); } [Fact] + public void EscapesCommentCharactersInDescription() + { + setup(); + method.Kind = CodeMethodKind.Constructor; + method.IsAsync = false; + parentClass.Kind = CodeClassKind.Custom; + parentClass.AddProperty(new CodeProperty + { + Name = "prop_without_default_value", + Kind = CodePropertyKind.Custom, + Documentation = new() + { + DescriptionTemplate = "This property has a description with comments \"\"\".", + }, + Type = new CodeType + { + Name = "string" + } + }); + writer.Write(method); + var result = tw.ToString(); + Assert.Contains("This property has a description with comments \\\"\\\"\\\".", result); + } + [Fact] public void WritesWithUrl() { setup(); From 87e25f1949cf7df3759aa236a768ea020ca904ed Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Thu, 7 Nov 2024 15:30:15 +0300 Subject: [PATCH 2/8] fix: default values in costructors of type boolean are set as strings --- .../Writers/Python/CodeMethodWriter.cs | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs index 3b391cdb8..6f68ffbed 100644 --- a/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs @@ -384,10 +384,15 @@ public class CodeMethodWriter : BaseElementWriter x.Name)) { var defaultValue = propWithDefault.DefaultValue; - if (propWithDefault.Type is CodeType propertyType && propertyType.TypeDefinition is CodeEnum enumDefinition) + switch (propWithDefault.Type) { - _codeUsingWriter.WriteDeferredImport(parentClass, enumDefinition.Name, writer); - defaultValue = $"{enumDefinition.Name}({defaultValue})"; + case CodeType { TypeDefinition: CodeEnum enumDefinition }: + _codeUsingWriter.WriteDeferredImport(parentClass, enumDefinition.Name, writer); + defaultValue = $"{enumDefinition.Name}({defaultValue})"; + break; + case CodeType propType when propType.Name.Equals("boolean", StringComparison.OrdinalIgnoreCase): + defaultValue = defaultValue.TrimQuotes(); + break; } var returnType = conventions.GetTypeString(propWithDefault.Type, propWithDefault, true, writer); conventions.WriteInLineDescription(propWithDefault, writer); From 3ad85949ad5b8d0bd6b0ae58e33bb51bb8d60c97 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Thu, 7 Nov 2024 15:33:47 +0300 Subject: [PATCH 3/8] booleans start in uppercase --- src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs index 6f68ffbed..6042fe336 100644 --- a/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs @@ -391,7 +391,7 @@ public class CodeMethodWriter : BaseElementWriter Date: Thu, 7 Nov 2024 16:44:21 +0300 Subject: [PATCH 4/8] fixes parameter types for send methods of type primitive --- .../Writers/Python/CodeMethodWriter.cs | 6 +++--- .../Writers/Python/PythonConventionService.cs | 2 +- .../Writers/Python/CodeMethodWriterTests.cs | 17 ++++++++--------- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs index 6042fe336..a017ad072 100644 --- a/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs @@ -593,7 +593,7 @@ public class CodeMethodWriter : BaseElementWriter "write_object_value", }; } - internal string GetTypeFactory(bool isVoid, bool isStream, bool isEnum, string returnType) + internal string GetTypeFactory(bool isVoid, bool isStream, bool isEnum, string returnType, bool isCollection) { if (isVoid) return string.Empty; if (isStream || isEnum) return $" \"{returnType}\","; - if (conventions.IsPrimitiveType(returnType)) return $" {returnType},"; + if (conventions.IsPrimitiveType(returnType)) return isCollection ? $" {returnType}," : $" \"{returnType}\","; return $" {returnType},"; } private string GetSendRequestMethodName(bool isVoid, bool isStream, bool isCollection, string returnType, diff --git a/src/Kiota.Builder/Writers/Python/PythonConventionService.cs b/src/Kiota.Builder/Writers/Python/PythonConventionService.cs index 6e335f94b..52e4a4692 100644 --- a/src/Kiota.Builder/Writers/Python/PythonConventionService.cs +++ b/src/Kiota.Builder/Writers/Python/PythonConventionService.cs @@ -130,7 +130,7 @@ public class PythonConventionService : CommonLanguageConventionService { return typeName switch { - "int" or "float" or "str" or "bool" or "None" => true, + "int" or "float" or "str" or "bool" or "None" or "datetime.datetime" or "datetime.timedelta" or "datetime.date" or "datetime.time" => true, _ => false, }; } diff --git a/tests/Kiota.Builder.Tests/Writers/Python/CodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/Python/CodeMethodWriterTests.cs index eaf1873c7..277418577 100644 --- a/tests/Kiota.Builder.Tests/Writers/Python/CodeMethodWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/Python/CodeMethodWriterTests.cs @@ -820,12 +820,13 @@ public sealed class CodeMethodWriterTests : IDisposable Assert.Contains("return {}", result); } [Theory] - [InlineData(true, false, false, "string", "")] - [InlineData(false, true, false, "Stream", " \"Stream\",")] - [InlineData(false, false, true, "SomeEnum", " \"SomeEnum\",")] - [InlineData(false, false, false, "int", " int,")] - [InlineData(false, false, false, "CustomType", " CustomType,")] - public void GetTypeFactory_ReturnsCorrectString(bool isVoid, bool isStream, bool isEnum, string returnType, string expected) + [InlineData(true, false, false, false, "string", "")] + [InlineData(false, true, false, false, "Stream", " \"Stream\",")] + [InlineData(false, false, true, false, "SomeEnum", " \"SomeEnum\",")] + [InlineData(false, false, false, true, "int", " int,")] + [InlineData(false, false, false, false, "int", " \"int\",")] + [InlineData(false, false, false, false, "CustomType", " CustomType,")] + public void GetTypeFactory_ReturnsCorrectString(bool isVoid, bool isStream, bool isEnum, bool isCollection, string returnType, string expected) { var mockConventionService = new Mock(); @@ -835,9 +836,7 @@ public sealed class CodeMethodWriterTests : IDisposable false // usesBackingStore ); - var result = codeMethodWriter.GetTypeFactory(isVoid, isStream, isEnum, returnType); - - + var result = codeMethodWriter.GetTypeFactory(isVoid, isStream, isEnum, returnType, isCollection); Assert.Equal(expected, result); } [Fact] From d326eefdefd215600a55f9169dfa77b1dd11ed65 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Fri, 8 Nov 2024 10:19:02 +0300 Subject: [PATCH 5/8] fixes for untyped node parameter and return types present in python generation --- .../Refiners/CommonLanguageRefiner.cs | 25 ++++++++++++---- src/Kiota.Builder/Refiners/PhpRefiner.cs | 2 +- src/Kiota.Builder/Refiners/PythonRefiner.cs | 2 +- src/Kiota.Builder/Refiners/RubyRefiner.cs | 2 +- src/Kiota.Builder/Refiners/SwiftRefiner.cs | 2 +- .../Refiners/PythonLanguageRefinerTests.cs | 29 +++++++++++++++++++ 6 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs b/src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs index 6cdaebe09..13e3b3183 100644 --- a/src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs +++ b/src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs @@ -1475,15 +1475,28 @@ public abstract class CommonLanguageRefiner : ILanguageRefiner CrawlTree(currentElement, x => RemoveRequestConfigurationClassesCommonProperties(x, baseTypeUsing)); } - protected static void RemoveUntypedNodePropertyValues(CodeElement currentElement) + protected static void RemoveUntypedNodeTypeValues(CodeElement currentElement) { - if (currentElement is CodeProperty currentProperty - && currentElement.Parent is CodeClass parentClass - && currentProperty.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase)) + switch (currentElement) { - parentClass.RemoveChildElement(currentProperty); + case CodeProperty currentProperty when currentElement.Parent is CodeClass parentClass && currentProperty.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase): + parentClass.RemoveChildElement(currentProperty); + break; + case CodeMethod currentMethod when currentMethod.IsOfKind(CodeMethodKind.RequestExecutor): + if (currentMethod.ReturnType.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase)) + { + currentMethod.ReturnType = new CodeType { Name = "binary", IsExternal = true }; + } + if (currentMethod.Parameters.Where(x => x.Kind is CodeParameterKind.RequestBody && x.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase)).ToList() is { Count: > 0 } parameters) + { + foreach (var parameter in parameters) + { + parameter.Type = new CodeType { Name = "binary", IsExternal = true }; + } + } + break; } - CrawlTree(currentElement, RemoveUntypedNodePropertyValues); + CrawlTree(currentElement, RemoveUntypedNodeTypeValues); } protected static void RemoveRequestConfigurationClasses(CodeElement currentElement, CodeUsing? configurationParameterTypeUsing = null, CodeType? defaultValueForGenericTypeParam = null, bool keepRequestConfigurationClass = false, bool addDeprecation = false, CodeUsing? usingForDefaultGenericParameter = null) { diff --git a/src/Kiota.Builder/Refiners/PhpRefiner.cs b/src/Kiota.Builder/Refiners/PhpRefiner.cs index 9317d8b17..82e18c62f 100644 --- a/src/Kiota.Builder/Refiners/PhpRefiner.cs +++ b/src/Kiota.Builder/Refiners/PhpRefiner.cs @@ -57,7 +57,7 @@ public class PhpRefiner : CommonLanguageRefiner AddParsableImplementsForModelClasses(generatedCode, "Parsable"); AddRequestConfigurationConstructors(generatedCode); AddDefaultImports(generatedCode, defaultUsingEvaluators); - RemoveUntypedNodePropertyValues(generatedCode); + RemoveUntypedNodeTypeValues(generatedCode); AddCollectionValidationUtilImportToModels(generatedCode); cancellationToken.ThrowIfCancellationRequested(); AddGetterAndSetterMethods(generatedCode, diff --git a/src/Kiota.Builder/Refiners/PythonRefiner.cs b/src/Kiota.Builder/Refiners/PythonRefiner.cs index 337b48994..dc9964867 100644 --- a/src/Kiota.Builder/Refiners/PythonRefiner.cs +++ b/src/Kiota.Builder/Refiners/PythonRefiner.cs @@ -26,7 +26,7 @@ public class PythonRefiner : CommonLanguageRefiner, ILanguageRefiner ); CorrectCommonNames(generatedCode); RemoveMethodByKind(generatedCode, CodeMethodKind.RawUrlConstructor); - RemoveUntypedNodePropertyValues(generatedCode); + RemoveUntypedNodeTypeValues(generatedCode); DisableActionOf(generatedCode, CodeParameterKind.RequestConfiguration); MoveRequestBuilderPropertiesToBaseType(generatedCode, diff --git a/src/Kiota.Builder/Refiners/RubyRefiner.cs b/src/Kiota.Builder/Refiners/RubyRefiner.cs index bf8f314cc..5bfa96034 100644 --- a/src/Kiota.Builder/Refiners/RubyRefiner.cs +++ b/src/Kiota.Builder/Refiners/RubyRefiner.cs @@ -63,7 +63,7 @@ public partial class RubyRefiner : CommonLanguageRefiner, ILanguageRefiner cancellationToken.ThrowIfCancellationRequested(); AddParsableImplementsForModelClasses(generatedCode, "MicrosoftKiotaAbstractions::Parsable"); AddDefaultImports(generatedCode, defaultUsingEvaluators); - RemoveUntypedNodePropertyValues(generatedCode); + RemoveUntypedNodeTypeValues(generatedCode); CorrectCoreType(generatedCode, CorrectMethodType, CorrectPropertyType, CorrectImplements); cancellationToken.ThrowIfCancellationRequested(); ReplacePropertyNames(generatedCode, diff --git a/src/Kiota.Builder/Refiners/SwiftRefiner.cs b/src/Kiota.Builder/Refiners/SwiftRefiner.cs index 21d47fb00..589025c6f 100644 --- a/src/Kiota.Builder/Refiners/SwiftRefiner.cs +++ b/src/Kiota.Builder/Refiners/SwiftRefiner.cs @@ -41,10 +41,10 @@ public class SwiftRefiner : CommonLanguageRefiner true, false, true); + RemoveUntypedNodeTypeValues(generatedCode); AddDefaultImports( generatedCode, defaultUsingEvaluators); - RemoveUntypedNodePropertyValues(generatedCode); cancellationToken.ThrowIfCancellationRequested(); CorrectCoreType( generatedCode, diff --git a/tests/Kiota.Builder.Tests/Refiners/PythonLanguageRefinerTests.cs b/tests/Kiota.Builder.Tests/Refiners/PythonLanguageRefinerTests.cs index 8da41e9c8..cd7af01fd 100644 --- a/tests/Kiota.Builder.Tests/Refiners/PythonLanguageRefinerTests.cs +++ b/tests/Kiota.Builder.Tests/Refiners/PythonLanguageRefinerTests.cs @@ -584,5 +584,34 @@ public class PythonLanguageRefinerTests Assert.Single(requestBuilder.Methods, x => x.IsOfKind(CodeMethodKind.RequestExecutor)); Assert.DoesNotContain("QueryParameters", declaration.Usings.Select(x => x.Name)); } + [Fact] + public async Task ReplacesUntypedNodeInMethodParameterAndReturnTypeAsync() + { + var requestBuilderClass = root.AddClass(new CodeClass() { Name = "NodeRequestBuilder" }).First(); + var method = new CodeMethod + { + Name = "getAsync", + ReturnType = new CodeType() + { + Name = KiotaBuilder.UntypedNodeName,//Returns untyped node + IsExternal = true + }, + Kind = CodeMethodKind.RequestExecutor + }; + method.AddParameter(new CodeParameter() + { + Name = "jsonData", + Type = new CodeType() + { + Name = KiotaBuilder.UntypedNodeName, //Has untyped node parameter + IsExternal = true + }, + Kind = CodeParameterKind.RequestBody + }); + requestBuilderClass.AddMethod(method); + await ILanguageRefiner.RefineAsync(new GenerationConfiguration { Language = GenerationLanguage.Python }, root); + Assert.Equal("bytes", method.Parameters.First().Type.Name);// type is renamed to use the stream type + Assert.Equal("bytes", method.ReturnType.Name);// return type is renamed to use the stream type + } #endregion } From e4c4ca45172d9e5dae8319e23acd6c106363f98b Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Mon, 11 Nov 2024 09:35:08 +0300 Subject: [PATCH 6/8] bump dependencies to pass validations --- it/python/requirements-dev.txt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/it/python/requirements-dev.txt b/it/python/requirements-dev.txt index 9b01f2867..542f94075 100644 --- a/it/python/requirements-dev.txt +++ b/it/python/requirements-dev.txt @@ -98,19 +98,19 @@ httpx[http2]==0.27.2 hyperframe==6.0.1 ; python_full_version >= '3.6.1' -microsoft-kiota-abstractions==1.6.0 +microsoft-kiota-abstractions==1.6.1 -microsoft-kiota-authentication-azure==1.6.0 +microsoft-kiota-authentication-azure==1.6.1 -microsoft-kiota-http==1.6.0 +microsoft-kiota-http==1.6.1 -microsoft-kiota-serialization-json==1.6.0 +microsoft-kiota-serialization-json==1.6.1 -microsoft-kiota-serialization-text==1.6.0 +microsoft-kiota-serialization-text==1.6.1 -microsoft-kiota-serialization-form==1.6.0 +microsoft-kiota-serialization-form==1.6.1 -microsoft-kiota-serialization-multipart==1.6.0 +microsoft-kiota-serialization-multipart==1.6.1 msal==1.31.0 From 1225097fa5eecfae1b294744cac06d4bb76dfde1 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Mon, 11 Nov 2024 10:59:54 +0300 Subject: [PATCH 7/8] move changelog to unreleased section --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8650d018..db607e7f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Fixed python generation in scenarios with opening/closing tags for code comments. [#5636](https://github.com/microsoft/kiota/issues/5636) + ## [1.20.0] - 2024-11-07 ### Added @@ -30,7 +32,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixes typing inconsistencies in generated code and libraries in Python [kiota-python#333](https://github.com/microsoft/kiota-python/issues/333) - Fixes generation of superfluous fields for Models with discriminator due to refiners adding the same properties to the same model [#4178](https://github.com/microsoft/kiota/issues/4178). - Fixes unsigned shim binary when installed as dotnet tool [#5650](https://github.com/microsoft/kiota/issues/5650). -- Fixed python generation in scenarios with opening/closing tags for code comments. [#5636](https://github.com/microsoft/kiota/issues/5636) ## [1.19.1] - 2024-10-11 From a09a94b46a306dcfbe37bb1dd6f62d457bf38bc3 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Tue, 12 Nov 2024 09:51:12 +0300 Subject: [PATCH 8/8] update integration tests configuration --- it/config.json | 4 ---- 1 file changed, 4 deletions(-) diff --git a/it/config.json b/it/config.json index 6f479e3e5..6295abf3c 100644 --- a/it/config.json +++ b/it/config.json @@ -243,10 +243,6 @@ { "Language": "ruby", "Rationale": "https://github.com/microsoft/kiota/issues/2484" - }, - { - "Language": "python", - "Rationale": "https://github.com/microsoft/kiota/issues/5744" } ] },