From 4a72f639e0a116c6920862ac800d74c8dafb4f5f Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Tue, 19 Dec 2023 19:17:08 +0000 Subject: [PATCH 1/9] Fix clashing constructors and class names in Go --- .github/workflows/integration-tests.yml | 1 + src/Kiota.Builder/Refiners/GoRefiner.cs | 31 +++++++++++++++ .../ToDoApi.yaml | 38 +++++++++++-------- 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/.github/workflows/integration-tests.yml b/.github/workflows/integration-tests.yml index 82d3457902..50d2e9b645 100644 --- a/.github/workflows/integration-tests.yml +++ b/.github/workflows/integration-tests.yml @@ -66,6 +66,7 @@ jobs: description: - "./tests/Kiota.Builder.IntegrationTests/InheritingErrors.yaml" - "./tests/Kiota.Builder.IntegrationTests/NoUnderscoresInModel.yaml" + - "./tests/Kiota.Builder.IntegrationTests/ToDoApi.yaml" - "oas::petstore" - "apisguru::twitter.com:current" - "apisguru::notion.com" diff --git a/src/Kiota.Builder/Refiners/GoRefiner.cs b/src/Kiota.Builder/Refiners/GoRefiner.cs index 90ccddfe81..96f68dc415 100644 --- a/src/Kiota.Builder/Refiners/GoRefiner.cs +++ b/src/Kiota.Builder/Refiners/GoRefiner.cs @@ -1,11 +1,13 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Security.Cryptography.X509Certificates; using System.Threading; using System.Threading.Tasks; using Kiota.Builder.CodeDOM; using Kiota.Builder.Configuration; using Kiota.Builder.Extensions; +using Kiota.Builder.Writers; using Kiota.Builder.Writers.Go; namespace Kiota.Builder.Refiners; @@ -88,6 +90,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance RemoveModelPropertiesThatDependOnSubNamespaces( generatedCode ); + FixConstructorClashes(generatedCode, x => $"{x}Escaped"); ReplaceReservedNames( generatedCode, new GoReservedNamesProvider(), @@ -421,6 +424,34 @@ private void FlattenGoFileNames(CodeElement currentElement) CrawlTree(currentElement, FlattenGoFileNames); } + private static void FixConstructorClashes(CodeElement currentElement, Func nameCorrection) + { + var allClasses = new List(); + FindAllClasses(currentElement, allClasses); + + foreach (var codeClass in allClasses) + { + if (codeClass.Name.StartsWith("New", StringComparison.OrdinalIgnoreCase)) + { + var targetName = codeClass.Name.Substring(3); + if (allClasses.Exists(x => x.Name.Equals(targetName, StringComparison.OrdinalIgnoreCase))) + codeClass.Name = nameCorrection(codeClass.Name); + } + } + } + + private static List FindAllClasses(CodeElement currentElement, List codeClasses) + { + // add the namespace to the name of the code element and the file name + if (currentElement is CodeClass codeClass) + { + codeClasses.Add(codeClass); + } + + CrawlTree(currentElement, x => FindAllClasses(x, codeClasses)); + return codeClasses; + } + protected static void RenameCancellationParameter(CodeElement currentElement) { if (currentElement is CodeMethod currentMethod && currentMethod.IsOfKind(CodeMethodKind.RequestExecutor) && currentMethod.Parameters.OfKind(CodeParameterKind.Cancellation) is CodeParameter parameter) diff --git a/tests/Kiota.Builder.IntegrationTests/ToDoApi.yaml b/tests/Kiota.Builder.IntegrationTests/ToDoApi.yaml index 164aee1ae2..6e3c2eef55 100644 --- a/tests/Kiota.Builder.IntegrationTests/ToDoApi.yaml +++ b/tests/Kiota.Builder.IntegrationTests/ToDoApi.yaml @@ -6,21 +6,21 @@ servers: - url: https://mytodos.doesnotexist/ description: Core paths: - /todos: + /todos: get: description: Return a list of Todo entities operationId: todos_ListTodos parameters: - - name: active - in: query - schema: - type: boolean - - name: keyword - in: query - schema: - type: string + - name: active + in: query + schema: + type: boolean + - name: keyword + in: query + schema: + type: string responses: - '200': + "200": description: OK content: application/json: @@ -30,7 +30,7 @@ paths: properties: value: type: array - items: + items: $ref: "#/components/schemas/todo" post: @@ -39,21 +39,21 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/todo' + $ref: "#/components/schemas/NewTodo" required: true responses: - '201': + "201": description: OK /todos/{todoId}: get: description: Return a single Todo object responses: - '200': + "200": description: OK delete: description: Delete a single Todo object responses: - '200': + "200": description: OK components: schemas: @@ -67,3 +67,11 @@ components: type: string Notes: type: string + NewTodo: + title: NewTodo + type: object + properties: + subject: + type: string + Notes: + type: string From ad9e885342600b26b5137d374945cdd69c31c578 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Tue, 19 Dec 2023 19:18:23 +0000 Subject: [PATCH 2/9] CHANGELOG.md --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c34afd5d9d..f9eafd00a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Fixed a bug where the discriminator validation rule would report false positives on nullable union types. +- Fixed a bug where constructors and model names where clashing in Go. [#3920](https://github.com/microsoft/kiota/issues/3920) ## [1.9.1] - 2023-12-13 @@ -1201,5 +1202,3 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Initial GitHub release - - From a45c0527b9e744320a25317dfc45f84b3477348d Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Tue, 19 Dec 2023 19:19:01 +0000 Subject: [PATCH 3/9] minor --- src/Kiota.Builder/Refiners/GoRefiner.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Kiota.Builder/Refiners/GoRefiner.cs b/src/Kiota.Builder/Refiners/GoRefiner.cs index 96f68dc415..43d43b0138 100644 --- a/src/Kiota.Builder/Refiners/GoRefiner.cs +++ b/src/Kiota.Builder/Refiners/GoRefiner.cs @@ -442,7 +442,6 @@ private static void FixConstructorClashes(CodeElement currentElement, Func FindAllClasses(CodeElement currentElement, List codeClasses) { - // add the namespace to the name of the code element and the file name if (currentElement is CodeClass codeClass) { codeClasses.Add(codeClass); From 0d0121cdf9ddb90576c1d5bd2c11fc44511bd293 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Tue, 19 Dec 2023 19:36:50 +0000 Subject: [PATCH 4/9] Update src/Kiota.Builder/Refiners/GoRefiner.cs Co-authored-by: Vincent Biret --- src/Kiota.Builder/Refiners/GoRefiner.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Kiota.Builder/Refiners/GoRefiner.cs b/src/Kiota.Builder/Refiners/GoRefiner.cs index 43d43b0138..7a94119943 100644 --- a/src/Kiota.Builder/Refiners/GoRefiner.cs +++ b/src/Kiota.Builder/Refiners/GoRefiner.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Security.Cryptography.X509Certificates; using System.Threading; using System.Threading.Tasks; using Kiota.Builder.CodeDOM; From b3991e9e48bb377b6d8c5479c5dd4ba22bd7a27a Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Tue, 19 Dec 2023 19:46:10 +0000 Subject: [PATCH 5/9] avoid fork tress --- src/Kiota.Builder/Refiners/GoRefiner.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Kiota.Builder/Refiners/GoRefiner.cs b/src/Kiota.Builder/Refiners/GoRefiner.cs index 7a94119943..cf9724a152 100644 --- a/src/Kiota.Builder/Refiners/GoRefiner.cs +++ b/src/Kiota.Builder/Refiners/GoRefiner.cs @@ -433,8 +433,9 @@ private static void FixConstructorClashes(CodeElement currentElement, Func x.Name.Equals(targetName, StringComparison.OrdinalIgnoreCase))) - codeClass.Name = nameCorrection(codeClass.Name); + if (allClasses.Exists(x => x.Name.Equals(targetName, StringComparison.OrdinalIgnoreCase)) && + codeClass.Parent is IBlock parentBlock) + parentBlock.RenameChildElement(codeClass.Name, nameCorrection(codeClass.Name)); } } } From 00f9c1988f8187c7f7eb08fa0da0799a422cb6b3 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Tue, 19 Dec 2023 15:00:08 -0500 Subject: [PATCH 6/9] - refactoring --- src/Kiota.Builder/Refiners/GoRefiner.cs | 32 ++++++++----------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/src/Kiota.Builder/Refiners/GoRefiner.cs b/src/Kiota.Builder/Refiners/GoRefiner.cs index cf9724a152..5952e13bb0 100644 --- a/src/Kiota.Builder/Refiners/GoRefiner.cs +++ b/src/Kiota.Builder/Refiners/GoRefiner.cs @@ -425,30 +425,18 @@ private void FlattenGoFileNames(CodeElement currentElement) private static void FixConstructorClashes(CodeElement currentElement, Func nameCorrection) { - var allClasses = new List(); - FindAllClasses(currentElement, allClasses); - - foreach (var codeClass in allClasses) - { - if (codeClass.Name.StartsWith("New", StringComparison.OrdinalIgnoreCase)) + if (currentElement is CodeNamespace currentNamespace) + foreach (var codeClassName in currentNamespace + .Classes + .Where(static x => x.Name.StartsWith("New", StringComparison.OrdinalIgnoreCase)) + .Select(static x => x.Name) + .ToArray()) { - var targetName = codeClass.Name.Substring(3); - if (allClasses.Exists(x => x.Name.Equals(targetName, StringComparison.OrdinalIgnoreCase)) && - codeClass.Parent is IBlock parentBlock) - parentBlock.RenameChildElement(codeClass.Name, nameCorrection(codeClass.Name)); + var targetName = codeClassName[3..]; + if (currentNamespace.FindChildByName(targetName) is not null) + currentNamespace.RenameChildElement(codeClassName, nameCorrection(codeClassName)); } - } - } - - private static List FindAllClasses(CodeElement currentElement, List codeClasses) - { - if (currentElement is CodeClass codeClass) - { - codeClasses.Add(codeClass); - } - - CrawlTree(currentElement, x => FindAllClasses(x, codeClasses)); - return codeClasses; + CrawlTree(currentElement, x => FixConstructorClashes(x, nameCorrection)); } protected static void RenameCancellationParameter(CodeElement currentElement) From ab13c11984614205d181828452f5ca8383944cdc Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Tue, 19 Dec 2023 15:02:06 -0500 Subject: [PATCH 7/9] - code linting Signed-off-by: Vincent Biret --- src/Kiota.Builder/Refiners/GoRefiner.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Kiota.Builder/Refiners/GoRefiner.cs b/src/Kiota.Builder/Refiners/GoRefiner.cs index 5952e13bb0..8b7e19ff54 100644 --- a/src/Kiota.Builder/Refiners/GoRefiner.cs +++ b/src/Kiota.Builder/Refiners/GoRefiner.cs @@ -6,7 +6,6 @@ using Kiota.Builder.CodeDOM; using Kiota.Builder.Configuration; using Kiota.Builder.Extensions; -using Kiota.Builder.Writers; using Kiota.Builder.Writers.Go; namespace Kiota.Builder.Refiners; From 72d0726d87b5d98612b1aafb53ecba366a776e23 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Wed, 20 Dec 2023 08:36:15 -0500 Subject: [PATCH 8/9] - narrows the find to the current namespace --- src/Kiota.Builder/Refiners/GoRefiner.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Kiota.Builder/Refiners/GoRefiner.cs b/src/Kiota.Builder/Refiners/GoRefiner.cs index 8b7e19ff54..6a63209638 100644 --- a/src/Kiota.Builder/Refiners/GoRefiner.cs +++ b/src/Kiota.Builder/Refiners/GoRefiner.cs @@ -432,7 +432,7 @@ private static void FixConstructorClashes(CodeElement currentElement, Func(targetName) is not null) + if (currentNamespace.FindChildByName(targetName, false) is not null) currentNamespace.RenameChildElement(codeClassName, nameCorrection(codeClassName)); } CrawlTree(currentElement, x => FixConstructorClashes(x, nameCorrection)); From d90f502aaebdd244db1bfd2cc2d0b06e5ffe3654 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Wed, 20 Dec 2023 08:54:54 -0500 Subject: [PATCH 9/9] - bumps kiota dependencies for ruby integration test Signed-off-by: Vincent Biret --- it/ruby/Gemfile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/it/ruby/Gemfile b/it/ruby/Gemfile index 799e58e777..b4e0cf883c 100644 --- a/it/ruby/Gemfile +++ b/it/ruby/Gemfile @@ -11,10 +11,10 @@ gem "rspec", "~> 3.0" gem "rubocop", "~> 1.21" -gem "microsoft_kiota_abstractions", "~> 0.13.0" +gem "microsoft_kiota_abstractions", "~> 0.14.3" -gem "microsoft_kiota_faraday", "~> 0.11.0" +gem "microsoft_kiota_faraday", "~> 0.12.0" -gem "microsoft_kiota_serialization_json", "~> 0.8.0" +gem "microsoft_kiota_serialization_json", "~> 0.9.1" -gem "microsoft_kiota_authentication_oauth", "~> 0.7.0" +gem "microsoft_kiota_authentication_oauth", "~> 0.8.0"