Skip to content

Commit

Permalink
feat(tool): Handle name collisions for controllers
Browse files Browse the repository at this point in the history
Fixes #106
  • Loading branch information
PerfectlyNormal committed Nov 23, 2024
1 parent e11a5cb commit 307480d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions TypeContractor.Tool/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,19 @@ public Task<int> 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)
Expand Down

0 comments on commit 307480d

Please sign in to comment.