Skip to content

Commit 90764ce

Browse files
committed
Implement validation for equivalent paths
1 parent baa555f commit 90764ce

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

src/Dibix.Sdk/CodeGeneration/Validation/EndpointModelValidator.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text.RegularExpressions;
5+
using Dibix.Http;
36

47
namespace Dibix.Sdk.CodeGeneration
58
{
@@ -24,11 +27,33 @@ public bool Validate(CodeGenerationModel model)
2427
if (!this.ValidateAction(action))
2528
isValid = false;
2629
}
30+
31+
var childRouteGroups = controller.Actions
32+
.Where(x => x.ChildRoute != null)
33+
.GroupBy(x => $"{x.Method}#{NormalizeChildRoute(x.ChildRoute)}");
34+
35+
foreach (IGrouping<string, ActionDefinition> childRouteGroup in childRouteGroups)
36+
{
37+
if (childRouteGroup.Count() <= 1)
38+
continue;
39+
40+
foreach (ActionDefinition action in childRouteGroup)
41+
{
42+
string route = RouteBuilder.BuildRoute(model.AreaName, controller.Name, action.ChildRoute);
43+
this._logger.LogError($"Equivalent paths are not allowed: {action.Method.ToString().ToUpperInvariant()} {route}", action.ChildRoute.Source, action.ChildRoute.Line, action.ChildRoute.Column);
44+
}
45+
}
2746
}
2847

2948
return isValid;
3049
}
3150

51+
private static string NormalizeChildRoute(string childRoute)
52+
{
53+
string normalizedChildRoute = Regex.Replace(childRoute, @"\{[^\}]+\}", "");
54+
return normalizedChildRoute;
55+
}
56+
3257
private bool ValidateAction(ActionDefinition action)
3358
{
3459
if (action.ChildRoute == null)

tests/Dibix.Sdk.Tests.Database/Endpoints/GenericEndpointWithErrors.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@
2525
}
2626
}
2727
}
28+
},
29+
{
30+
"method": "GET",
31+
"target": "EmptyWithParams",
32+
"childRoute": "ambiguous/child/route/{a}"
33+
},
34+
{
35+
"method": "GET",
36+
"target": "EmptyWithParams",
37+
"childRoute": "ambiguous/child/route/{b}"
2838
}
2939
]
3040
}

tests/Dibix.Sdk.Tests/CodeGeneration/CodeGenerationTaskTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ public void Endpoints_OpenApi()
518518
}
519519

520520
[TestMethod]
521-
public void Endpoint_WithInvalidPropertySource_Error()
521+
public void Endpoint_WithValidationErrors_Error()
522522
{
523523
this.ExecuteTestAndExpectError
524524
(
@@ -541,7 +541,9 @@ public void Endpoint_WithInvalidPropertySource_Error()
541541
Endpoints\GenericEndpointWithErrors.json(19,23,19,23):error:Property 'Nm' not found on contract 'Dibix.Sdk.Tests.DomainModel.Entry'
542542
Endpoints\GenericEndpointWithErrors.json(24,27,24,27):error:Property 'Nm' not found on contract 'Dibix.Sdk.Tests.DomainModel.Entry'
543543
Endpoints\GenericEndpointWithErrors.json(9,19,9,19):error:Source 'ENV' does not support property 'MachinePassword'
544-
Endpoints\GenericEndpointWithErrors.json(16,27,16,27):error:The path segment 'get' is a known HTTP verb, which should be indicated by the action method and is therefore redundant: this/get/is/wrong"
544+
Endpoints\GenericEndpointWithErrors.json(16,27,16,27):error:The path segment 'get' is a known HTTP verb, which should be indicated by the action method and is therefore redundant: this/get/is/wrong
545+
Endpoints\GenericEndpointWithErrors.json(32,22,32,22):error:Equivalent paths are not allowed: GET Tests/GenericEndpoint/ambiguous/child/route/{a}
546+
Endpoints\GenericEndpointWithErrors.json(37,22,37,22):error:Equivalent paths are not allowed: GET Tests/GenericEndpoint/ambiguous/child/route/{b}"
545547
);
546548
}
547549

0 commit comments

Comments
 (0)