Skip to content

Commit

Permalink
Merge pull request #3940 from andreaTP/issue-3920
Browse files Browse the repository at this point in the history
[Go] Detect and fix model names with clashing constructor
  • Loading branch information
baywet authored Dec 20, 2023
2 parents 2d48db9 + d90f502 commit 7a5c7dd
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 deletions.
1 change: 1 addition & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- Fixed a bug where the order of enum declaration might results in a missing enum type. [#3935](https://github.com/microsoft/kiota/issues/3935)

## [1.9.1] - 2023-12-13
Expand Down Expand Up @@ -1202,5 +1203,3 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Initial GitHub release


8 changes: 4 additions & 4 deletions it/ruby/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
17 changes: 17 additions & 0 deletions src/Kiota.Builder/Refiners/GoRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
RemoveModelPropertiesThatDependOnSubNamespaces(
generatedCode
);
FixConstructorClashes(generatedCode, x => $"{x}Escaped");
ReplaceReservedNames(
generatedCode,
new GoReservedNamesProvider(),
Expand Down Expand Up @@ -421,6 +422,22 @@ private void FlattenGoFileNames(CodeElement currentElement)
CrawlTree(currentElement, FlattenGoFileNames);
}

private static void FixConstructorClashes(CodeElement currentElement, Func<string, string> nameCorrection)
{
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 = codeClassName[3..];
if (currentNamespace.FindChildByName<CodeClass>(targetName, false) is not null)
currentNamespace.RenameChildElement(codeClassName, nameCorrection(codeClassName));
}
CrawlTree(currentElement, x => FixConstructorClashes(x, nameCorrection));
}

protected static void RenameCancellationParameter(CodeElement currentElement)
{
if (currentElement is CodeMethod currentMethod && currentMethod.IsOfKind(CodeMethodKind.RequestExecutor) && currentMethod.Parameters.OfKind(CodeParameterKind.Cancellation) is CodeParameter parameter)
Expand Down
38 changes: 23 additions & 15 deletions tests/Kiota.Builder.IntegrationTests/ToDoApi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -30,7 +30,7 @@ paths:
properties:
value:
type: array
items:
items:
$ref: "#/components/schemas/todo"

post:
Expand All @@ -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:
Expand All @@ -67,3 +67,11 @@ components:
type: string
Notes:
type: string
NewTodo:
title: NewTodo
type: object
properties:
subject:
type: string
Notes:
type: string

0 comments on commit 7a5c7dd

Please sign in to comment.