Skip to content

Commit f427332

Browse files
committed
[add] authorization indicator
1 parent ece19ff commit f427332

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/Simplify.Web.Swagger/ControllerAction.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,10 @@ public ControllerActionNames Names
3939
get => _names ?? throw new InvalidOperationException("Names is null");
4040
set => _names = value;
4141
}
42+
43+
/// <summary>
44+
/// Gets or sets the value indicating whether controller requires user authorization.
45+
/// </summary>
46+
public bool IsAuthorizationRequired { get; set; }
4247
}
4348
}

src/Simplify.Web.Swagger/ControllerActionsFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ private static ControllerAction CreateControllerAction(HttpMethod method, string
4141
Type = HttpMethodToOperationType(method),
4242
Path = route.StartsWith("/") ? route : "/" + route,
4343
Names = CreateNames(item.ControllerType),
44-
Responses = CreateResponses(item.ControllerType)
44+
Responses = CreateResponses(item.ControllerType),
45+
IsAuthorizationRequired = item.Security != null && item.Security.IsAuthorizationRequired
4546
};
4647

4748
private static ControllerActionNames CreateNames(Type controllerType) =>

src/Simplify.Web.Swagger/SimplifyWebDocumentFilter.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,24 @@ private static OpenApiOperation CreateOperation(ControllerAction item)
4949
foreach (var response in item.Responses)
5050
operation.Responses.Add(response.Key.ToString(), response.Value);
5151

52+
if (item.IsAuthorizationRequired)
53+
AddSecurity(operation);
54+
5255
return operation;
5356
}
57+
58+
private static void AddSecurity(OpenApiOperation operation) =>
59+
operation.Security.Add(new OpenApiSecurityRequirement
60+
{
61+
{
62+
new OpenApiSecurityScheme
63+
{
64+
Reference = new OpenApiReference {
65+
Type = ReferenceType.SecurityScheme,
66+
Id = "bearerAuth"
67+
}
68+
}, new List<string>()
69+
}
70+
});
5471
}
5572
}

0 commit comments

Comments
 (0)