Skip to content

Commit da0a309

Browse files
committed
[#1] [add] displaing list of actions with correct names done
1 parent f64ea7f commit da0a309

4 files changed

Lines changed: 178 additions & 85 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using Microsoft.OpenApi.Models;
3+
4+
namespace Simplify.Web.Swagger
5+
{
6+
/// <summary>
7+
/// Represent controller action
8+
/// </summary>
9+
public class ControllerAction
10+
{
11+
private string? _path;
12+
private ControllerActionNames? _names;
13+
14+
/// <summary>
15+
/// Operation type
16+
/// </summary>
17+
public OperationType Type { get; set; }
18+
19+
/// <summary>
20+
/// Controller path
21+
/// </summary>
22+
public string Path
23+
{
24+
get => _path ?? throw new InvalidOperationException("Path is null");
25+
set => _path = value;
26+
}
27+
28+
/// <summary>
29+
/// Controller names
30+
/// </summary>
31+
public ControllerActionNames Names
32+
{
33+
get => _names ?? throw new InvalidOperationException("Names is null");
34+
set => _names = value;
35+
}
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace Simplify.Web.Swagger
2+
{
3+
/// <summary>
4+
/// provides controller action names
5+
/// </summary>
6+
public class ControllerActionNames
7+
{
8+
/// <summary>
9+
/// Initializes ControllerActionNames
10+
/// </summary>
11+
/// <param name="name">Controller full name</param>
12+
/// <param name="groupName">Controller group name</param>
13+
/// <param name="summary">Controller summary</param>
14+
public ControllerActionNames(string name, string groupName, string? summary = null)
15+
{
16+
Name = name;
17+
GroupName = groupName;
18+
Summary = summary;
19+
}
20+
21+
/// <summary>
22+
/// Controller full name
23+
/// </summary>
24+
public string Name { get; set; }
25+
26+
/// <summary>
27+
/// Controller group name
28+
/// </summary>
29+
public string GroupName { get; set; }
30+
31+
/// <summary>
32+
/// Controller summary
33+
/// </summary>
34+
public string? Summary { get; set; }
35+
}
36+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Microsoft.OpenApi.Models;
5+
using Simplify.Web.Meta;
6+
7+
namespace Simplify.Web.Swagger
8+
{
9+
/// <summary>
10+
/// Provides ControllerAction factory
11+
/// </summary>
12+
public class ControllerActionsFactory
13+
{
14+
/// <summary>
15+
/// Provides controller prefixes to remove
16+
/// </summary>
17+
public static IList<string> RemovePrefixes = new List<string>
18+
{
19+
"Controllers.",
20+
"Api.v1."
21+
};
22+
23+
/// <summary>
24+
/// Creates controller actions from Simplify.Web controller meta data
25+
/// </summary>
26+
/// <returns></returns>
27+
public static IEnumerable<ControllerAction> CreateControllerActionsFromControllersMetaData() =>
28+
ControllersMetaStore.Current.ControllersMetaData
29+
.Where(x => x.ExecParameters != null)
30+
.SelectMany(CreateControllerActions);
31+
32+
private static IEnumerable<ControllerAction> CreateControllerActions(IControllerMetaData item) =>
33+
item.ExecParameters!
34+
.Routes
35+
.Select(x => CreateControllerAction(x.Key, x.Value, item));
36+
37+
private static ControllerAction CreateControllerAction(HttpMethod method, string route, IControllerMetaData item) =>
38+
new ControllerAction
39+
{
40+
Type = HttpMethodToOperationType(method),
41+
Path = route.StartsWith("/") ? route : "/" + route,
42+
Names = CreateNames(item.ControllerType)
43+
};
44+
45+
private static ControllerActionNames CreateNames(Type controllerType) =>
46+
CreateNames(controllerType.FullName ?? throw new InvalidOperationException("controllerType.FullName is null"));
47+
48+
private static ControllerActionNames CreateNames(string name)
49+
{
50+
var src = FormatNameSource(name);
51+
52+
var index = src.LastIndexOf("/");
53+
54+
if (index == -1)
55+
return new ControllerActionNames(src, src);
56+
57+
return new ControllerActionNames(src, src.Substring(0, index), src.Substring(index + 1));
58+
}
59+
60+
private static string FormatNameSource(string str)
61+
{
62+
foreach (var prefix in RemovePrefixes)
63+
{
64+
var prefixIndex = str.IndexOf(prefix);
65+
66+
if (prefixIndex == -1)
67+
continue;
68+
69+
str = str.Substring(prefixIndex + prefix.Length);
70+
}
71+
72+
str = str.Replace(".", "/");
73+
74+
if (str.EndsWith("Controller"))
75+
str = str.Substring(0, str.LastIndexOf("Controller"));
76+
77+
return str;
78+
}
79+
80+
private static OperationType HttpMethodToOperationType(HttpMethod method) =>
81+
method switch
82+
{
83+
HttpMethod.Get => OperationType.Get,
84+
HttpMethod.Post => OperationType.Post,
85+
HttpMethod.Put => OperationType.Put,
86+
HttpMethod.Patch => OperationType.Patch,
87+
HttpMethod.Delete => OperationType.Delete,
88+
HttpMethod.Options => OperationType.Options,
89+
_ => OperationType.Get
90+
};
91+
}
92+
}
Lines changed: 13 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using Microsoft.OpenApi.Models;
4-
using Simplify.Web.Meta;
54
using Swashbuckle.AspNetCore.SwaggerGen;
65

76
namespace Simplify.Web.Swagger
@@ -11,14 +10,6 @@ namespace Simplify.Web.Swagger
1110
/// </summary>
1211
public class SimplifyWebDocumentFilter : IDocumentFilter
1312
{
14-
// private const string VersionEndPoint = "/version";
15-
16-
private static IList<string> RemovePrefixes = new List<string>
17-
{
18-
"Controllers.",
19-
"Api.v1."
20-
};
21-
2213
/// <summary>
2314
/// Applies current filter
2415
/// </summary>
@@ -27,99 +18,36 @@ public class SimplifyWebDocumentFilter : IDocumentFilter
2718

2819
public void Apply(OpenApiDocument openApiDocument, DocumentFilterContext context)
2920
{
30-
foreach (var item in CreatePathItemsFromControllersMetaData())
21+
foreach (var item in ControllerActionsFactory.CreateControllerActionsFromControllersMetaData()
22+
.GroupBy(x => x.Path)
23+
.Select(x => new KeyValuePair<string, OpenApiPathItem>(x.Key, CreatePathItem(x))))
3124
openApiDocument?.Paths.Add(item.Key, item.Value);
3225
}
3326

34-
private static IDictionary<string, OpenApiPathItem> CreatePathItemsFromControllersMetaData() =>
35-
ControllersMetaStore.Current.ControllersMetaData
36-
.Where(x => x.ExecParameters != null)
37-
.SelectMany(CreatePathItems)
38-
.ToDictionary(x => x.Key, x => x.Value);
39-
40-
private static IEnumerable<KeyValuePair<string, OpenApiPathItem>> CreatePathItems(IControllerMetaData item)
41-
{
42-
var items = new List<KeyValuePair<string, OpenApiPathItem>>();
43-
var routes = item.ExecParameters!.Routes;
44-
var needToAddPostfix = routes.ContainsDuplicates();
45-
46-
items.AddRange(routes.Select(x => new KeyValuePair<string, OpenApiPathItem>(
47-
FormatControllerName(x.Value, x.Key, needToAddPostfix), CreateOpenApiPathItems(x.Key, x.Value, item))));
48-
49-
return items;
50-
}
51-
52-
private static OpenApiPathItem CreateOpenApiPathItems(HttpMethod method, string route, IControllerMetaData item)
27+
private static OpenApiPathItem CreatePathItem(IEnumerable<ControllerAction> actions)
5328
{
5429
var pathItem = new OpenApiPathItem();
5530

56-
pathItem.AddOperation(HttpMethodToOperationType(method), CreateOperation(item));
31+
foreach (var item in actions)
32+
pathItem.AddOperation(item.Type, CreateOperation(item));
5733

5834
return pathItem;
5935
}
6036

61-
private static OpenApiOperation CreateOperation(IControllerMetaData item)
62-
{
63-
// TODO
6437

65-
var operation = new OpenApiOperation
66-
{
67-
};
38+
private static OpenApiOperation CreateOperation(ControllerAction item)
39+
{
40+
var operation = new OpenApiOperation();
6841

6942
operation.Tags.Add(new OpenApiTag
7043
{
71-
Name = FormatOperationName(item)
44+
Name = item.Names.GroupName
7245
});
7346

74-
return operation;
75-
}
76-
77-
private static OperationType HttpMethodToOperationType(HttpMethod method) =>
78-
method switch
79-
{
80-
HttpMethod.Get => OperationType.Get,
81-
HttpMethod.Post => OperationType.Post,
82-
HttpMethod.Put => OperationType.Put,
83-
HttpMethod.Patch => OperationType.Patch,
84-
HttpMethod.Delete => OperationType.Delete,
85-
HttpMethod.Options => OperationType.Options,
86-
_ => OperationType.Get
87-
};
47+
if (item.Names.Summary != null)
48+
operation.Summary = item.Names.Summary;
8849

89-
// TODO
90-
private static KeyValuePair<string, OpenApiResponse> CreateResponse()
91-
{
92-
var response = new OpenApiResponse
93-
{
94-
};
95-
96-
return new KeyValuePair<string, OpenApiResponse>("", response);
97-
}
98-
99-
private static string FormatControllerName(string route, HttpMethod method, bool needToAddPostfix) =>
100-
needToAddPostfix ? $"{route} ({method})" : route;
101-
102-
private static string? FormatOperationName(IControllerMetaData item) =>
103-
item.ControllerType.FullName != null ? FormatOperationName(item.ControllerType.FullName) : null;
104-
105-
private static string FormatOperationName(string str)
106-
{
107-
foreach (var prefix in RemovePrefixes)
108-
{
109-
var prefixIndex = str.IndexOf(prefix);
110-
111-
if (prefixIndex == -1)
112-
continue;
113-
114-
str = str.Substring(prefixIndex + prefix.Length);
115-
}
116-
117-
str = str.Replace(".", "/");
118-
119-
if (str.EndsWith("Controller"))
120-
str = str.Substring(0, str.LastIndexOf("Controller"));
121-
122-
return str;
50+
return operation;
12351
}
12452
}
12553
}

0 commit comments

Comments
 (0)