Skip to content

Commit 25e4755

Browse files
authored
Merge pull request #17 from asset-t/feature/authorization-language
[add] authorization, body, accept-language
2 parents 62ebcc1 + 0c1ea3a commit 25e4755

13 files changed

+175
-40
lines changed

src/Simplify.Web.Swagger/CHANGELOG.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# Changelog
22

3+
## [0.3] - 2023-12-20
4+
5+
### Added
6+
7+
- Request body display
8+
- SimplifyWebSwaggerArgs
9+
10+
### Changed
11+
12+
- Authorization display
13+
14+
### Dependencies
15+
16+
- Simplify.Web bump to 4.8
17+
- Swashbuckle.AspNetCore.SwaggerGen bump to 6.5
18+
319
## [0.2] - 2022-06-14
420

521
### Added
@@ -16,4 +32,4 @@
1632

1733
### Added
1834

19-
- Initial version with controllers list including response types handling
35+
- Initial version with controllers list including response types handling

src/Simplify.Web.Swagger/ControllerAction.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public class ControllerAction
1717
/// Operation type
1818
/// </summary>
1919
public OperationType Type { get; set; }
20+
21+
/// <summary>
22+
/// Request body
23+
/// </summary>
24+
public OpenApiRequestBody RequestBody = new OpenApiRequestBody();
2025

2126
/// <summary>
2227
/// Controller responses

src/Simplify.Web.Swagger/ControllerActionsFactory.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Text.RegularExpressions;
55
using Microsoft.OpenApi.Models;
66
using Simplify.Web.Meta;
7+
using Swashbuckle.AspNetCore.SwaggerGen;
78

89
namespace Simplify.Web.Swagger
910
{
@@ -25,23 +26,24 @@ public class ControllerActionsFactory
2526
/// Creates controller actions from Simplify.Web controller meta data
2627
/// </summary>
2728
/// <returns></returns>
28-
public static IEnumerable<ControllerAction> CreateControllerActionsFromControllersMetaData() =>
29+
public static IEnumerable<ControllerAction> CreateControllerActionsFromControllersMetaData(DocumentFilterContext context) =>
2930
ControllersMetaStore.Current.ControllersMetaData
3031
.Where(x => x.ExecParameters != null)
31-
.SelectMany(CreateControllerActions);
32+
.SelectMany(item => CreateControllerActions(item, context));
3233

33-
private static IEnumerable<ControllerAction> CreateControllerActions(IControllerMetaData item) =>
34+
private static IEnumerable<ControllerAction> CreateControllerActions(IControllerMetaData item, DocumentFilterContext context) =>
3435
item.ExecParameters!
3536
.Routes
36-
.Select(x => CreateControllerAction(x.Key, x.Value, item));
37+
.Select(x => CreateControllerAction(x.Key, x.Value, item, context));
3738

38-
private static ControllerAction CreateControllerAction(HttpMethod method, string route, IControllerMetaData item) =>
39+
private static ControllerAction CreateControllerAction(HttpMethod method, string route, IControllerMetaData item, DocumentFilterContext context) =>
3940
new ControllerAction
4041
{
4142
Type = HttpMethodToOperationType(method),
4243
Path = route.StartsWith("/") ? route : "/" + route,
4344
Names = CreateNames(item.ControllerType),
4445
Responses = CreateResponses(item.ControllerType),
46+
RequestBody = CreateRequestBody(item.ControllerType, context),
4547
IsAuthorizationRequired = item.Security != null && item.Security.IsAuthorizationRequired
4648
};
4749

@@ -92,6 +94,24 @@ private static OperationType HttpMethodToOperationType(HttpMethod method) =>
9294
_ => OperationType.Get
9395
};
9496

97+
private static OpenApiRequestBody CreateRequestBody(Type controllerType, DocumentFilterContext context)
98+
{
99+
var request = new OpenApiRequestBody();
100+
var attributes = controllerType.GetCustomAttributes(typeof(ProducesRequestBodyAttribute), false);
101+
102+
if (attributes.Length > 0)
103+
{
104+
var item = (ProducesRequestBodyAttribute) attributes.First();
105+
106+
request.Content = new Dictionary<string, OpenApiMediaType>
107+
{
108+
["application/json"] = new() { Schema = context.SchemaGenerator.GenerateSchema(item.Model, context.SchemaRepository) }
109+
};
110+
}
111+
112+
return request;
113+
}
114+
95115
private static IDictionary<int, OpenApiResponse> CreateResponses(Type controllerType)
96116
{
97117
var items = new Dictionary<int, OpenApiResponse>();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
3+
namespace Simplify.Web.Swagger;
4+
5+
/// <summary>
6+
/// A filter that specifies the request body received by the controller.
7+
/// </summary>
8+
public class ProducesRequestBodyAttribute : Attribute
9+
{
10+
/// <summary>
11+
/// Initializes an instance of <see cref="ProducesRequestBodyAttribute"/>.
12+
/// </summary>
13+
/// <param name="model">The schema model.</param>
14+
public ProducesRequestBodyAttribute(Type? model) => Model = model;
15+
16+
/// <summary>
17+
/// Request body
18+
/// </summary>
19+
public Type? Model { get; set; }
20+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
1010
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1111

12-
<Version>0.2</Version>
12+
<Version>0.3</Version>
1313

1414
<Description>Swagger extensions for Simplify.Web web-framework</Description>
1515
<Product>Simplify</Product>
@@ -26,7 +26,7 @@
2626
<ItemGroup>
2727
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.1.0" />
2828
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
29-
<PackageReference Include="Simplify.Web" Version="4.6.0" />
29+
<PackageReference Include="Simplify.Web" Version="4.8.0" />
3030
</ItemGroup>
3131
<ItemGroup>
3232
<None Include="..\..\images\icon.png" Pack="true" Visible="false" PackagePath="" />

src/Simplify.Web.Swagger/SimplifyWebDocumentFilter.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@ namespace Simplify.Web.Swagger
1111
/// </summary>
1212
public class SimplifyWebDocumentFilter : IDocumentFilter
1313
{
14+
private static SimplifyWebSwaggerArgs? _args;
15+
16+
/// <summary>
17+
/// Initializes an instance of <see cref="SimplifyWebDocumentFilter"/>.
18+
/// </summary>
19+
/// <param name="args">The registration args</param>
20+
public SimplifyWebDocumentFilter(SimplifyWebSwaggerArgs? args) => _args = args;
21+
1422
/// <summary>
1523
/// Applies current filter
1624
/// </summary>
1725
/// <param name="openApiDocument">The Open API document</param>
1826
/// <param name="context">The context</param>
1927
public void Apply(OpenApiDocument openApiDocument, DocumentFilterContext context)
2028
{
21-
foreach (var item in ControllerActionsFactory.CreateControllerActionsFromControllersMetaData()
29+
foreach (var item in ControllerActionsFactory.CreateControllerActionsFromControllersMetaData(context)
2230
.GroupBy(x => x.Path)
2331
.Select(x => new KeyValuePair<string, OpenApiPathItem>(x.Key, CreatePathItem(x))))
2432
openApiDocument?.Paths.Add(item.Key, item.Value);
@@ -49,28 +57,16 @@ private static OpenApiOperation CreateOperation(ControllerAction item)
4957
foreach (var response in item.Responses)
5058
operation.Responses.Add(response.Key.ToString(), response.Value);
5159

52-
if (item.IsAuthorizationRequired)
53-
AddSecurity(operation);
54-
5560
operation.Parameters = CreateParameters(item.ParsedPath);
61+
operation.RequestBody = item.RequestBody;
62+
63+
if (_args != null)
64+
foreach (var parameter in _args.Parameters)
65+
operation.Parameters.Add(parameter);
5666

5767
return operation;
5868
}
5969

60-
private static void AddSecurity(OpenApiOperation operation) =>
61-
operation.Security.Add(new OpenApiSecurityRequirement
62-
{
63-
{
64-
new OpenApiSecurityScheme
65-
{
66-
Reference = new OpenApiReference {
67-
Type = ReferenceType.SecurityScheme,
68-
Id = "bearerAuth"
69-
}
70-
}, new List<string>()
71-
}
72-
});
73-
7470
private static IList<OpenApiParameter> CreateParameters(IControllerPath path) =>
7571
path.Items
7672
.Where(x => x is PathParameter)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Collections.Generic;
2+
using Microsoft.OpenApi.Models;
3+
4+
namespace Simplify.Web.Swagger;
5+
6+
/// <summary>
7+
/// SimplifyWebSwagger registration args.
8+
/// </summary>
9+
public class SimplifyWebSwaggerArgs
10+
{
11+
/// <summary>
12+
/// Open Api Parameters
13+
/// </summary>
14+
public IList<OpenApiParameter> Parameters { get; } = new List<OpenApiParameter>();
15+
}

src/Simplify.Web.Swagger/SimplifyWebSwaggerExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ public static class SimplifyWebSwaggerServiceCollectionExtensions
1111
/// <summary>
1212
/// Add Simplify.Web controllers to Swagger documentation generation
1313
/// </summary>
14-
public static void AddSimplifyWebSwagger(this SwaggerGenOptions options) => options.DocumentFilter<SimplifyWebDocumentFilter>();
15-
}
14+
public static void AddSimplifyWebSwagger(this SwaggerGenOptions options, SimplifyWebSwaggerArgs? args = null) => options.DocumentFilter<SimplifyWebDocumentFilter>(args);
15+
}

src/TesterApp/Controllers/Api/v1/Groups/GetMultipleController.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@ public class GetMultipleController : Simplify.Web.Controller
1515
{
1616
public override ControllerResponse Invoke()
1717
{
18-
var items = new List<GroupViewModel>
18+
var languageCode = Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;
19+
20+
var items = new List<GroupViewModel>();
21+
22+
switch (languageCode)
1923
{
20-
new GroupViewModel
21-
{
22-
Name = "Group 1"
23-
},
24-
new GroupViewModel
25-
{
26-
Name = "Group 2"
27-
}
28-
};
24+
case "ru":
25+
items.Add(new() {Name = "Группа 1"});
26+
items.Add(new() {Name = "Группа 2"});
27+
break;
28+
default:
29+
items.Add(new() {Name = "Group 1"});
30+
items.Add(new() {Name = "Group 2"});
31+
break;
32+
}
2933

3034
// Items retrieve
3135

src/TesterApp/Controllers/Api/v1/Users/CreateController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
using Microsoft.AspNetCore.Mvc;
22
using Simplify.Web;
33
using Simplify.Web.Attributes;
4+
using Simplify.Web.Swagger;
45
using TesterApp.ViewModels;
56

67
namespace TesterApp.Controllers.Api.v1.Users;
78

89
[Post("/api/v1/users")]
910
[ApiVersion("1.0")]
1011
[Produces("application/text")]
12+
[ProducesRequestBody(typeof(UserAddViewModel))]
1113
public class CreateController : AsyncController<UserAddViewModel>
1214
{
1315
public override async Task<ControllerResponse> Invoke()

0 commit comments

Comments
 (0)