Skip to content

Commit 58ff3bc

Browse files
committed
[up] Simplify.Web bump to 5.0
[fix] analyzer warnings [r] to new C# features [del]Simplify.Web.Json dependency, swithc to internal Json usage
1 parent a0f1af7 commit 58ff3bc

13 files changed

Lines changed: 151 additions & 151 deletions

src/Simplify.Web.Swagger/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [1.0.0] - Unreleased
4+
5+
### Dependencies
6+
7+
- Simplify.Web bump to 5.0
8+
39
## [0.4.1] - 2023-12-22
410

511
### Fixed

src/Simplify.Web.Swagger/ControllerAction.cs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using Microsoft.OpenApi.Models;
4-
using Simplify.Web.Routing;
4+
using Simplify.Web.Controllers.Meta.Routing;
55

66
namespace Simplify.Web.Swagger
77
{
@@ -10,37 +10,45 @@ namespace Simplify.Web.Swagger
1010
/// </summary>
1111
public class ControllerAction
1212
{
13-
private string? _path;
1413
private ControllerActionNames? _names;
14+
private IControllerRoute? _controllerRoute;
1515

16-
/// <summary>
17-
/// Operation type
18-
/// </summary>
19-
public OperationType Type { get; set; }
20-
2116
/// <summary>
2217
/// Request body
2318
/// </summary>
24-
public OpenApiRequestBody RequestBody = new OpenApiRequestBody();
19+
public OpenApiRequestBody RequestBody { get; set; } = new();
2520

2621
/// <summary>
2722
/// Controller responses
2823
/// </summary>
29-
public IDictionary<int, OpenApiResponse> Responses = new Dictionary<int, OpenApiResponse>();
24+
public IDictionary<int, OpenApiResponse> Responses { get; set; } = new Dictionary<int, OpenApiResponse>();
25+
26+
/// <summary>
27+
/// Operation type
28+
/// </summary>
29+
public OperationType Type { get; set; }
3030

3131
/// <summary>
3232
/// Controller path
3333
/// </summary>
34-
public string Path
35-
{
36-
get => _path ?? throw new InvalidOperationException("Path is null");
37-
set => _path = value;
38-
}
34+
#if NETSTANDARD2_0
35+
public string Path => ControllerRoute.Path.StartsWith("/") ? ControllerRoute.Path : "/" + ControllerRoute.Path;
36+
#else
37+
public string Path => ControllerRoute.Path.StartsWith('/') ? ControllerRoute.Path : "/" + ControllerRoute.Path;
38+
#endif
3939

4040
/// <summary>
41-
/// Controller parsed path
41+
/// Gets or sets the controller route.
4242
/// </summary>
43-
public IControllerPath ParsedPath => new ControllerPathParser().Parse(Path);
43+
/// <value>
44+
/// The controller route.
45+
/// </value>
46+
/// <exception cref="InvalidOperationException">ControllerRoute is null</exception>
47+
public IControllerRoute ControllerRoute
48+
{
49+
get => _controllerRoute ?? throw new InvalidOperationException("ControllerRoute is null");
50+
set => _controllerRoute = value;
51+
}
4452

4553
/// <summary>
4654
/// Controller names

src/Simplify.Web.Swagger/ControllerActionsFactory.cs

Lines changed: 66 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,77 @@
33
using System.Linq;
44
using System.Text.RegularExpressions;
55
using Microsoft.OpenApi.Models;
6-
using Simplify.Web.Meta;
6+
using Simplify.Web.Controllers.Meta;
7+
using Simplify.Web.Controllers.Meta.MetaStore;
8+
using Simplify.Web.Controllers.Meta.Routing;
9+
using Simplify.Web.Http;
710
using Swashbuckle.AspNetCore.SwaggerGen;
811

912
namespace Simplify.Web.Swagger
1013
{
1114
/// <summary>
1215
/// Provides ControllerAction factory
1316
/// </summary>
14-
public class ControllerActionsFactory
17+
public static class ControllerActionsFactory
1518
{
19+
private static readonly IReadOnlyCollection<KeyValuePair<string, string>> ResponseDescriptionMap =
20+
[
21+
new KeyValuePair<string, string>("1\\d{2}", "Information"),
22+
23+
new KeyValuePair<string, string>("201", "Created"),
24+
new KeyValuePair<string, string>("202", "Accepted"),
25+
new KeyValuePair<string, string>("204", "No Content"),
26+
new KeyValuePair<string, string>("2\\d{2}", "Success"),
27+
28+
new KeyValuePair<string, string>("304", "Not Modified"),
29+
new KeyValuePair<string, string>("3\\d{2}", "Redirect"),
30+
31+
new KeyValuePair<string, string>("400", "Bad Request"),
32+
new KeyValuePair<string, string>("401", "Unauthorized"),
33+
new KeyValuePair<string, string>("403", "Forbidden"),
34+
new KeyValuePair<string, string>("404", "Not Found"),
35+
new KeyValuePair<string, string>("405", "Method Not Allowed"),
36+
new KeyValuePair<string, string>("406", "Not Acceptable"),
37+
new KeyValuePair<string, string>("408", "Request Timeout"),
38+
new KeyValuePair<string, string>("409", "Conflict"),
39+
new KeyValuePair<string, string>("429", "Too Many Requests"),
40+
new KeyValuePair<string, string>("4\\d{2}", "Client Error"),
41+
42+
new KeyValuePair<string, string>("5\\d{2}", "Server Error"),
43+
new KeyValuePair<string, string>("default", "Error")
44+
];
45+
1646
/// <summary>
1747
/// Provides controller prefixes to remove
1848
/// </summary>
19-
public static IList<string> RemovePrefixes = new List<string>
20-
{
21-
"Controllers.",
22-
"Api.v1."
23-
};
49+
public static IList<string> RemovePrefixes { get; } =
50+
[
51+
"Controllers.",
52+
"Api.v1."
53+
];
2454

2555
/// <summary>
2656
/// Creates controller actions from Simplify.Web controller meta data
2757
/// </summary>
2858
/// <returns></returns>
2959
public static IEnumerable<ControllerAction> CreateControllerActionsFromControllersMetaData(DocumentFilterContext context) =>
30-
ControllersMetaStore.Current.ControllersMetaData
31-
.Where(x => x.ExecParameters != null)
60+
ControllersMetaStore.Current.RoutedControllers
3261
.SelectMany(item => CreateControllerActions(item, context));
3362

34-
private static IEnumerable<ControllerAction> CreateControllerActions(IControllerMetaData item, DocumentFilterContext context) =>
63+
private static IEnumerable<ControllerAction> CreateControllerActions(IControllerMetadata item, DocumentFilterContext context) =>
3564
item.ExecParameters!
3665
.Routes
3766
.Select(x => CreateControllerAction(x.Key, x.Value, item, context));
3867

39-
private static ControllerAction CreateControllerAction(HttpMethod method, string route, IControllerMetaData item, DocumentFilterContext context) =>
40-
new ControllerAction
68+
private static ControllerAction CreateControllerAction(HttpMethod method, IControllerRoute route, IControllerMetadata item, DocumentFilterContext context) =>
69+
new()
4170
{
4271
Type = HttpMethodToOperationType(method),
43-
Path = route.StartsWith("/") ? route : "/" + route,
72+
ControllerRoute = route,
4473
Names = CreateNames(item.ControllerType),
4574
Responses = CreateResponses(item.ControllerType, context),
4675
RequestBody = CreateRequestBody(item.ControllerType, context),
47-
IsAuthorizationRequired = item.Security != null && item.Security.IsAuthorizationRequired
76+
IsAuthorizationRequired = item.Security is { IsAuthorizationRequired: true }
4877
};
4978

5079
private static ControllerActionNames CreateNames(Type controllerType) =>
@@ -56,10 +85,9 @@ private static ControllerActionNames CreateNames(string name)
5685

5786
var index = src.LastIndexOf("/");
5887

59-
if (index == -1)
60-
return new ControllerActionNames(src, src);
61-
62-
return new ControllerActionNames(src, src.Substring(0, index), src.Substring(index + 1));
88+
return index == -1
89+
? new ControllerActionNames(src, src)
90+
: new ControllerActionNames(src, src.Substring(0, index), src.Substring(index + 1));
6391
}
6492

6593
private static string FormatNameSource(string str)
@@ -99,72 +127,39 @@ private static OpenApiRequestBody CreateRequestBody(Type controllerType, Documen
99127
var request = new OpenApiRequestBody();
100128
var attributes = controllerType.GetCustomAttributes(typeof(RequestBodyAttribute), false);
101129

102-
if (attributes.Length > 0)
103-
{
104-
var item = (RequestBodyAttribute)attributes.First();
130+
if (attributes.Length <= 0)
131+
return request;
105132

106-
request.Content = new Dictionary<string, OpenApiMediaType>
107-
{
108-
["application/json"] = new() { Schema = context.SchemaGenerator.GenerateSchema(item.Model, context.SchemaRepository) }
109-
};
110-
}
133+
var item = (RequestBodyAttribute)attributes[0];
134+
135+
request.Content = new Dictionary<string, OpenApiMediaType>
136+
{
137+
["application/json"] = new() { Schema = context.SchemaGenerator.GenerateSchema(item.Model, context.SchemaRepository) }
138+
};
111139

112140
return request;
113141
}
114142

115-
private static IDictionary<int, OpenApiResponse> CreateResponses(Type controllerType, DocumentFilterContext context)
116-
{
117-
var items = new Dictionary<int, OpenApiResponse>();
118-
119-
var attributes = controllerType.GetCustomAttributes(typeof(ProducesResponseAttribute), false);
120-
121-
foreach (ProducesResponseAttribute item in attributes)
122-
items.Add(item.StatusCode, CreateResponse(item, context));
123-
124-
return items;
125-
}
143+
private static IDictionary<int, OpenApiResponse> CreateResponses(Type controllerType, DocumentFilterContext context) =>
144+
controllerType.GetCustomAttributes(typeof(ProducesResponseAttribute), false)
145+
.Cast<ProducesResponseAttribute>()
146+
.ToDictionary(item => item.StatusCode, item => CreateResponse(item, context));
126147

127148
private static OpenApiResponse CreateResponse(ProducesResponseAttribute producesResponse, DocumentFilterContext context)
128149
{
129-
var response = new OpenApiResponse();
130-
131-
response.Description = ResponseDescriptionMap
150+
var response = new OpenApiResponse
151+
{
152+
Description = ResponseDescriptionMap
132153
.FirstOrDefault((entry) => Regex.IsMatch(producesResponse.StatusCode.ToString(), entry.Key))
133-
.Value;
154+
.Value
155+
};
134156

135157
foreach (var item in producesResponse.ContentTypes.Distinct())
136-
response.Content.Add(item, producesResponse.Type is null
137-
? new OpenApiMediaType()
138-
: new () {Schema = context.SchemaGenerator.GenerateSchema(producesResponse.Type, context.SchemaRepository)});
158+
response.Content.Add(item, producesResponse.Type is null
159+
? new OpenApiMediaType()
160+
: new OpenApiMediaType { Schema = context.SchemaGenerator.GenerateSchema(producesResponse.Type, context.SchemaRepository) });
139161

140162
return response;
141163
}
142-
143-
private static readonly IReadOnlyCollection<KeyValuePair<string, string>> ResponseDescriptionMap = new[]
144-
{
145-
new KeyValuePair<string, string>("1\\d{2}", "Information"),
146-
147-
new KeyValuePair<string, string>("201", "Created"),
148-
new KeyValuePair<string, string>("202", "Accepted"),
149-
new KeyValuePair<string, string>("204", "No Content"),
150-
new KeyValuePair<string, string>("2\\d{2}", "Success"),
151-
152-
new KeyValuePair<string, string>("304", "Not Modified"),
153-
new KeyValuePair<string, string>("3\\d{2}", "Redirect"),
154-
155-
new KeyValuePair<string, string>("400", "Bad Request"),
156-
new KeyValuePair<string, string>("401", "Unauthorized"),
157-
new KeyValuePair<string, string>("403", "Forbidden"),
158-
new KeyValuePair<string, string>("404", "Not Found"),
159-
new KeyValuePair<string, string>("405", "Method Not Allowed"),
160-
new KeyValuePair<string, string>("406", "Not Acceptable"),
161-
new KeyValuePair<string, string>("408", "Request Timeout"),
162-
new KeyValuePair<string, string>("409", "Conflict"),
163-
new KeyValuePair<string, string>("429", "Too Many Requests"),
164-
new KeyValuePair<string, string>("4\\d{2}", "Client Error"),
165-
166-
new KeyValuePair<string, string>("5\\d{2}", "Server Error"),
167-
new KeyValuePair<string, string>("default", "Error")
168-
};
169164
}
170165
}

src/Simplify.Web.Swagger/RequestBodyAttribute.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ namespace Simplify.Web.Swagger;
55
/// <summary>
66
/// A filter that specifies the request body received by the controller.
77
/// </summary>
8-
public class RequestBodyAttribute : Attribute
8+
/// <seealso cref="Attribute" />
9+
/// <remarks>
10+
/// Initializes an instance of <see cref="RequestBodyAttribute" />.
11+
/// </remarks>
12+
/// <param name="model">The request body model type.</param>
13+
[AttributeUsage(AttributeTargets.Class)]
14+
public class RequestBodyAttribute(Type model) : Attribute
915
{
10-
/// <summary>
11-
/// Initializes an instance of <see cref="RequestBodyAttribute"/>.
12-
/// </summary>
13-
/// <param name="model">The request body model type.</param>
14-
public RequestBodyAttribute(Type model) => Model = model ?? throw new ArgumentNullException(nameof(model));
15-
1616
/// <summary>
1717
/// Request body model type
1818
/// </summary>
19-
public Type Model { get; private set; }
19+
public Type Model { get; private set; } = model ?? throw new ArgumentNullException(nameof(model));
2020
}

src/Simplify.Web.Swagger/RoutesExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Collections.Generic;
22
using System.Linq;
3-
using Simplify.Web.Meta;
3+
using Simplify.Web.Http;
44

55
namespace Simplify.Web.Swagger
66
{

src/Simplify.Web.Swagger/Simplify.Web.Swagger.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
4-
<LangVersion>10.0</LangVersion>
4+
<LangVersion>latest</LangVersion>
55
<Nullable>enable</Nullable>
66
<WarningsAsErrors>nullable</WarningsAsErrors>
77
<EmbedUntrackedSources>true</EmbedUntrackedSources>
88
<IncludeSymbols>true</IncludeSymbols>
99
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
1010
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1111

12-
<Version>0.4.1</Version>
12+
<Version>1.0-pre01</Version>
1313

1414
<Description>Swagger extensions for Simplify.Web web-framework</Description>
1515
<Product>Simplify</Product>
16-
<Authors>Alexander Krylkov</Authors>
16+
<Authors>Simplify community</Authors>
1717
<Copyright>Licensed under LGPL</Copyright>
1818
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
1919
<PackageProjectUrl>https://web.simplifynet.dev</PackageProjectUrl>
@@ -25,7 +25,7 @@
2525
</PropertyGroup>
2626
<ItemGroup>
2727
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.1.0" />
28-
<PackageReference Include="Simplify.Web" Version="4.8.1" />
28+
<PackageReference Include="Simplify.Web" Version="5.0-pre01" />
2929
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
3030
</ItemGroup>
3131
<ItemGroup>

0 commit comments

Comments
 (0)