Skip to content

Commit 78df4f7

Browse files
committed
[add] authorization, body, accept-language
1 parent 6037976 commit 78df4f7

File tree

6 files changed

+86
-25
lines changed

6 files changed

+86
-25
lines changed

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/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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
/// Initializes an instance of <see cref="ProducesResponseAttribute"/>.
13+
/// </summary>
14+
public SimplifyWebSwaggerArgs() => Parameters = new List<OpenApiParameter>();
15+
16+
/// <summary>
17+
/// Open Api Parameters
18+
/// </summary>
19+
public IList<OpenApiParameter> Parameters { get; set; }
20+
}

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? parameter = null) => options.DocumentFilter<SimplifyWebDocumentFilter>(parameter);
15+
}

0 commit comments

Comments
 (0)