From 307480ddf304719793b72bba6eb2967952ffdf23 Mon Sep 17 00:00:00 2001 From: "Per Christian B. Viken" Date: Sat, 23 Nov 2024 17:50:18 +0100 Subject: [PATCH] feat(tool): Handle name collisions for controllers Fixes #106 --- CHANGELOG.md | 4 ++++ README.md | 11 +++++++++++ TypeContractor.Tool/Generator.cs | 12 ++++++++++++ 3 files changed, 27 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 698126d..49d0091 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [unreleased] +### Added + +- Handle name collisions when creationg API clients (#106) + ## [0.14.0] - 2024-11-17 ### Added diff --git a/README.md b/README.md index 0e55862..a236bdf 100644 --- a/README.md +++ b/README.md @@ -250,6 +250,17 @@ that does what you want. The available data model can be found in `TypeContractor/Templates/ApiClientTemplateDto.cs` and an example template is `TypeContractor/Templates/aurelia.hbs`. +Since the name is just the controller name with `Controller` replaced +with `Client`, collisions between controllers with the same name but +different namespaces are possible. If this happens, the first +controller found gets to keep the original name and TypeContractor +will prefix the colliding client with the last part of its namespace. +So for example: + +`ExampleApp.Controllers.DataController` turns into `DataClient`, +while `ExampleApp.Controllers.Subsystem.DataController` collides and +gets turned into `SubsystemDataClient`. + ## Future improvements * Kebab-case output files and directories diff --git a/TypeContractor.Tool/Generator.cs b/TypeContractor.Tool/Generator.cs index c190813..6df10dd 100644 --- a/TypeContractor.Tool/Generator.cs +++ b/TypeContractor.Tool/Generator.cs @@ -103,7 +103,19 @@ public Task Execute() var client = ApiHelpers.BuildApiClient(controller, endpoints); if (client.Endpoints.Any()) + { + if (clients.Any(x => x.Name == client.Name)) + { + var originalName = client.Name; + var newName = string.Join("", controller.FullName!.Split('.')[^2..]); + newName = $"{newName[0..1].ToUpper()}{newName[1..]}"; + + Log.Instance.LogWarning($"Found existing client with name {originalName}. Will rename to {newName}"); + client = client with { Name = newName }; + } + clients.Add(client); + } } foreach (var returnType in returnTypes)