Skip to content

Commit

Permalink
Show DisplayName for conflicting actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Saibamen committed Nov 26, 2024
1 parent eb48a5f commit eee5e12
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ public static JsonSerializerOptions GetSystemTextJsonSettings(IServiceProvider s
var options = serviceProvider.GetService(optionsType);
var value = optionsType.GetProperty("Value")?.GetValue(options);
var jsonOptions = value?.GetType().GetProperty("JsonSerializerOptions")?.GetValue(value);
if (jsonOptions is JsonSerializerOptions)
if (jsonOptions is JsonSerializerOptions serializerOptions)
{
return (JsonSerializerOptions)jsonOptions;
return serializerOptions;
}
}
catch
Expand Down Expand Up @@ -336,7 +336,11 @@ private List<Tuple<OpenApiOperationDescription, ApiDescription, MethodInfo>> Add

if (pathItem.ContainsKey(operation.Method))
{
throw new InvalidOperationException($"The method '{operation.Method}' on path '{path}' is registered multiple times.");
var conflictingApiDescription = addedOperations
.First(t => t.Item1.Path == operation.Path && t.Item1.Method == operation.Method)
.Item2;

throw new InvalidOperationException($"The method '{operation.Method}' on path '{path}' is registered multiple times for actions {conflictingApiDescription.ActionDescriptor.DisplayName} and {apiDescription.ActionDescriptor.DisplayName}.");
}

pathItem[operation.Method] = operation.Operation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ private static void EnsureSingleBodyParameter(OpenApiOperationDescription operat
{
if (operationDescription.Operation.ActualParameters.Count(p => p.Kind == OpenApiParameterKind.Body) > 1)
{
throw new InvalidOperationException("The operation '" + operationDescription.Operation.OperationId + "' has more than one body parameter.");
throw new InvalidOperationException($"The operation '{operationDescription.Operation.OperationId}' has more than one body parameter.");
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/NSwag.Generation.WebApi/WebApiOpenApiDocumentGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,19 @@ private bool AddOperationDescriptionsToDocument(OpenApiDocument document, Type c
{
var path = operation.Path.Replace("//", "/");

if (!document.Paths.TryGetValue(path, out OpenApiPathItem value))
if (!document.Paths.TryGetValue(path, out var pathItem))
{
value = [];
document.Paths[path] = value;
pathItem = [];
document.Paths[path] = pathItem;
}

if (value.ContainsKey(operation.Method))
if (pathItem.ContainsKey(operation.Method))
{
throw new InvalidOperationException("The method '" + operation.Method + "' on path '" + path + "' is registered multiple times " +
throw new InvalidOperationException($"The method '{operation.Method}' on path '{path}' is registered multiple times " +
"(check the DefaultUrlTemplate setting [default for Web API: 'api/{controller}/{id}'; for MVC projects: '{controller}/{action}/{id?}']).");
}

value[operation.Method] = operation.Operation;
pathItem[operation.Method] = operation.Operation;
addedOperations++;
}
}
Expand Down Expand Up @@ -326,11 +326,11 @@ private string GetOperationId(OpenApiDocument document, string controllerName, M
controllerName = controllerName.Substring(0, controllerName.Length - 10);
}

operationId = controllerName + "_" + GetActionName(method);
operationId = $"{controllerName}_{GetActionName(method)}";
}

var number = 1;
while (document.Operations.Any(o => o.Operation.OperationId == operationId + (number > 1 ? "_" + number : string.Empty)))
while (document.Operations.Any(o => o.Operation.OperationId == operationId + (number > 1 ? $"_{number}" : string.Empty)))
{
number++;
}
Expand Down Expand Up @@ -359,7 +359,7 @@ private List<string> GetHttpPaths(Type controllerType, MethodInfo method)
}
else if (routePrefixAttribute != null)
{
httpPaths.Add(routePrefixAttribute.Prefix + "/" + attribute.Template);
httpPaths.Add($"{routePrefixAttribute.Prefix}/{attribute.Template}");
}
else if (routeAttributesOnClass != null)
{
Expand All @@ -371,7 +371,7 @@ private List<string> GetHttpPaths(Type controllerType, MethodInfo method)
{
foreach (var routeAttributeOnClass in routeAttributesOnClass)
{
httpPaths.Add(routeAttributeOnClass.Template + "/" + attribute.Template);
httpPaths.Add($"{routeAttributeOnClass.Template}/{attribute.Template}");
}
}
}
Expand All @@ -385,7 +385,7 @@ private List<string> GetHttpPaths(Type controllerType, MethodInfo method)
{
foreach (var routeAttributeOnClass in routeAttributesOnClass)
{
httpPaths.Add(routePrefixAttribute.Prefix + "/" + routeAttributeOnClass.Template);
httpPaths.Add($"{routePrefixAttribute.Prefix}/{routeAttributeOnClass.Template}");
}
}
else if (routePrefixAttribute != null)
Expand Down Expand Up @@ -422,7 +422,7 @@ private List<string> GetHttpPaths(Type controllerType, MethodInfo method)
private static IEnumerable<string> ExpandOptionalHttpPathParameters(string path, MethodInfo method)
{
var segments = path.Split(['/'], StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < segments.Length; i++)
for (var i = 0; i < segments.Length; i++)
{
var segment = segments[i];
if (segment.EndsWith("?}", StringComparison.Ordinal))
Expand Down

0 comments on commit eee5e12

Please sign in to comment.