Skip to content

Commit d62a9f3

Browse files
Adopt file-scoped namespaces (#3331)
- Switch to file-scoped namespaces. - Simplify the hash define constants to either `#if NET` or `#if !NET` - most of the versions are now redundant since we only support .NET 8+.
1 parent 99ccafa commit d62a9f3

File tree

309 files changed

+19815
-20128
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

309 files changed

+19815
-20128
lines changed

src/Swashbuckle.AspNetCore.Annotations/AnnotationsDocumentFilter.cs

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,46 @@
22
using Microsoft.OpenApi.Models;
33
using Swashbuckle.AspNetCore.SwaggerGen;
44

5-
namespace Swashbuckle.AspNetCore.Annotations
5+
namespace Swashbuckle.AspNetCore.Annotations;
6+
7+
public class AnnotationsDocumentFilter : IDocumentFilter
68
{
7-
public class AnnotationsDocumentFilter : IDocumentFilter
9+
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
810
{
9-
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
10-
{
11-
if (swaggerDoc.Tags == null)
12-
swaggerDoc.Tags = new List<OpenApiTag>();
11+
if (swaggerDoc.Tags == null)
12+
swaggerDoc.Tags = new List<OpenApiTag>();
1313

14-
// Collect (unique) controller names and custom attributes in a dictionary
15-
var controllerNamesAndAttributes = context.ApiDescriptions
16-
.Select(apiDesc => apiDesc.ActionDescriptor as ControllerActionDescriptor)
17-
.Where(actionDesc => actionDesc != null)
18-
.GroupBy(actionDesc => actionDesc.ControllerName)
19-
.Select(group => new KeyValuePair<string, IEnumerable<object>>(group.Key, group.First().ControllerTypeInfo.GetCustomAttributes(true)));
14+
// Collect (unique) controller names and custom attributes in a dictionary
15+
var controllerNamesAndAttributes = context.ApiDescriptions
16+
.Select(apiDesc => apiDesc.ActionDescriptor as ControllerActionDescriptor)
17+
.Where(actionDesc => actionDesc != null)
18+
.GroupBy(actionDesc => actionDesc.ControllerName)
19+
.Select(group => new KeyValuePair<string, IEnumerable<object>>(group.Key, group.First().ControllerTypeInfo.GetCustomAttributes(true)));
2020

21-
foreach (var entry in controllerNamesAndAttributes)
22-
{
23-
ApplySwaggerTagAttribute(swaggerDoc, entry.Key, entry.Value);
24-
}
21+
foreach (var entry in controllerNamesAndAttributes)
22+
{
23+
ApplySwaggerTagAttribute(swaggerDoc, entry.Key, entry.Value);
2524
}
25+
}
2626

27-
private void ApplySwaggerTagAttribute(
28-
OpenApiDocument swaggerDoc,
29-
string controllerName,
30-
IEnumerable<object> customAttributes)
31-
{
32-
var swaggerTagAttribute = customAttributes
33-
.OfType<SwaggerTagAttribute>()
34-
.FirstOrDefault();
27+
private void ApplySwaggerTagAttribute(
28+
OpenApiDocument swaggerDoc,
29+
string controllerName,
30+
IEnumerable<object> customAttributes)
31+
{
32+
var swaggerTagAttribute = customAttributes
33+
.OfType<SwaggerTagAttribute>()
34+
.FirstOrDefault();
3535

36-
if (swaggerTagAttribute == null) return;
36+
if (swaggerTagAttribute == null) return;
3737

38-
swaggerDoc.Tags.Add(new OpenApiTag
39-
{
40-
Name = controllerName,
41-
Description = swaggerTagAttribute.Description,
42-
ExternalDocs = (swaggerTagAttribute.ExternalDocsUrl != null)
43-
? new OpenApiExternalDocs { Url = new Uri(swaggerTagAttribute.ExternalDocsUrl) }
44-
: null
45-
});
46-
}
38+
swaggerDoc.Tags.Add(new OpenApiTag
39+
{
40+
Name = controllerName,
41+
Description = swaggerTagAttribute.Description,
42+
ExternalDocs = (swaggerTagAttribute.ExternalDocsUrl != null)
43+
? new OpenApiExternalDocs { Url = new Uri(swaggerTagAttribute.ExternalDocsUrl) }
44+
: null
45+
});
4746
}
4847
}

src/Swashbuckle.AspNetCore.Annotations/AnnotationsOperationFilter.cs

Lines changed: 90 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,122 @@
11
using Microsoft.OpenApi.Models;
22
using Swashbuckle.AspNetCore.SwaggerGen;
33

4-
namespace Swashbuckle.AspNetCore.Annotations
4+
namespace Swashbuckle.AspNetCore.Annotations;
5+
6+
public class AnnotationsOperationFilter : IOperationFilter
57
{
6-
public class AnnotationsOperationFilter : IOperationFilter
8+
public void Apply(OpenApiOperation operation, OperationFilterContext context)
79
{
8-
public void Apply(OpenApiOperation operation, OperationFilterContext context)
9-
{
10-
IEnumerable<object> controllerAttributes = [];
11-
IEnumerable<object> actionAttributes = [];
12-
IEnumerable<object> metadataAttributes = [];
10+
IEnumerable<object> controllerAttributes = [];
11+
IEnumerable<object> actionAttributes = [];
12+
IEnumerable<object> metadataAttributes = [];
1313

14-
if (context.MethodInfo != null)
15-
{
16-
controllerAttributes = context.MethodInfo.DeclaringType.GetCustomAttributes(true);
17-
actionAttributes = context.MethodInfo.GetCustomAttributes(true);
18-
}
14+
if (context.MethodInfo != null)
15+
{
16+
controllerAttributes = context.MethodInfo.DeclaringType.GetCustomAttributes(true);
17+
actionAttributes = context.MethodInfo.GetCustomAttributes(true);
18+
}
1919

20-
#if NET6_0_OR_GREATER
21-
if (context.ApiDescription?.ActionDescriptor?.EndpointMetadata != null)
22-
{
23-
metadataAttributes = context.ApiDescription.ActionDescriptor.EndpointMetadata;
24-
}
20+
#if NET
21+
if (context.ApiDescription?.ActionDescriptor?.EndpointMetadata != null)
22+
{
23+
metadataAttributes = context.ApiDescription.ActionDescriptor.EndpointMetadata;
24+
}
2525
#endif
2626

27-
// NOTE: When controller and action attributes are applicable, action attributes should take priority.
28-
// Hence, why they're at the end of the list (i.e. last one wins).
29-
// Distinct() is applied due to an ASP.NET Core issue: https://github.com/dotnet/aspnetcore/issues/34199.
30-
var allAttributes = controllerAttributes
31-
.Union(actionAttributes)
32-
.Union(metadataAttributes)
33-
.Distinct();
34-
35-
var actionAndEndpointAttributes = actionAttributes
36-
.Union(metadataAttributes)
37-
.Distinct();
38-
39-
ApplySwaggerOperationAttribute(operation, actionAndEndpointAttributes);
40-
ApplySwaggerOperationFilterAttributes(operation, context, allAttributes);
41-
ApplySwaggerResponseAttributes(operation, context, allAttributes);
42-
}
27+
// NOTE: When controller and action attributes are applicable, action attributes should take priority.
28+
// Hence, why they're at the end of the list (i.e. last one wins).
29+
// Distinct() is applied due to an ASP.NET Core issue: https://github.com/dotnet/aspnetcore/issues/34199.
30+
var allAttributes = controllerAttributes
31+
.Union(actionAttributes)
32+
.Union(metadataAttributes)
33+
.Distinct();
34+
35+
var actionAndEndpointAttributes = actionAttributes
36+
.Union(metadataAttributes)
37+
.Distinct();
38+
39+
ApplySwaggerOperationAttribute(operation, actionAndEndpointAttributes);
40+
ApplySwaggerOperationFilterAttributes(operation, context, allAttributes);
41+
ApplySwaggerResponseAttributes(operation, context, allAttributes);
42+
}
4343

44-
private static void ApplySwaggerOperationAttribute(
45-
OpenApiOperation operation,
46-
IEnumerable<object> actionAttributes)
47-
{
48-
var swaggerOperationAttribute = actionAttributes
49-
.OfType<SwaggerOperationAttribute>()
50-
.FirstOrDefault();
44+
private static void ApplySwaggerOperationAttribute(
45+
OpenApiOperation operation,
46+
IEnumerable<object> actionAttributes)
47+
{
48+
var swaggerOperationAttribute = actionAttributes
49+
.OfType<SwaggerOperationAttribute>()
50+
.FirstOrDefault();
5151

52-
if (swaggerOperationAttribute == null) return;
52+
if (swaggerOperationAttribute == null) return;
5353

54-
if (swaggerOperationAttribute.Summary != null)
55-
operation.Summary = swaggerOperationAttribute.Summary;
54+
if (swaggerOperationAttribute.Summary != null)
55+
operation.Summary = swaggerOperationAttribute.Summary;
5656

57-
if (swaggerOperationAttribute.Description != null)
58-
operation.Description = swaggerOperationAttribute.Description;
57+
if (swaggerOperationAttribute.Description != null)
58+
operation.Description = swaggerOperationAttribute.Description;
5959

60-
if (swaggerOperationAttribute.OperationId != null)
61-
operation.OperationId = swaggerOperationAttribute.OperationId;
60+
if (swaggerOperationAttribute.OperationId != null)
61+
operation.OperationId = swaggerOperationAttribute.OperationId;
6262

63-
if (swaggerOperationAttribute.Tags != null)
64-
{
65-
operation.Tags = [.. swaggerOperationAttribute.Tags.Select(tagName => new OpenApiTag { Name = tagName })];
66-
}
63+
if (swaggerOperationAttribute.Tags != null)
64+
{
65+
operation.Tags = [.. swaggerOperationAttribute.Tags.Select(tagName => new OpenApiTag { Name = tagName })];
6766
}
67+
}
6868

69-
public static void ApplySwaggerOperationFilterAttributes(
70-
OpenApiOperation operation,
71-
OperationFilterContext context,
72-
IEnumerable<object> controllerAndActionAttributes)
73-
{
74-
var swaggerOperationFilterAttributes = controllerAndActionAttributes
75-
.OfType<SwaggerOperationFilterAttribute>();
69+
public static void ApplySwaggerOperationFilterAttributes(
70+
OpenApiOperation operation,
71+
OperationFilterContext context,
72+
IEnumerable<object> controllerAndActionAttributes)
73+
{
74+
var swaggerOperationFilterAttributes = controllerAndActionAttributes
75+
.OfType<SwaggerOperationFilterAttribute>();
7676

77-
foreach (var swaggerOperationFilterAttribute in swaggerOperationFilterAttributes)
78-
{
79-
var filter = (IOperationFilter)Activator.CreateInstance(swaggerOperationFilterAttribute.FilterType);
80-
filter.Apply(operation, context);
81-
}
77+
foreach (var swaggerOperationFilterAttribute in swaggerOperationFilterAttributes)
78+
{
79+
var filter = (IOperationFilter)Activator.CreateInstance(swaggerOperationFilterAttribute.FilterType);
80+
filter.Apply(operation, context);
8281
}
82+
}
8383

84-
private static void ApplySwaggerResponseAttributes(
85-
OpenApiOperation operation,
86-
OperationFilterContext context,
87-
IEnumerable<object> controllerAndActionAttributes)
84+
private static void ApplySwaggerResponseAttributes(
85+
OpenApiOperation operation,
86+
OperationFilterContext context,
87+
IEnumerable<object> controllerAndActionAttributes)
88+
{
89+
var swaggerResponseAttributes = controllerAndActionAttributes.OfType<SwaggerResponseAttribute>();
90+
91+
foreach (var swaggerResponseAttribute in swaggerResponseAttributes)
8892
{
89-
var swaggerResponseAttributes = controllerAndActionAttributes.OfType<SwaggerResponseAttribute>();
93+
var statusCode = swaggerResponseAttribute.StatusCode.ToString();
9094

91-
foreach (var swaggerResponseAttribute in swaggerResponseAttributes)
92-
{
93-
var statusCode = swaggerResponseAttribute.StatusCode.ToString();
95+
operation.Responses ??= [];
9496

95-
operation.Responses ??= [];
97+
if (!operation.Responses.TryGetValue(statusCode, out OpenApiResponse response))
98+
{
99+
response = new OpenApiResponse();
100+
}
96101

97-
if (!operation.Responses.TryGetValue(statusCode, out OpenApiResponse response))
98-
{
99-
response = new OpenApiResponse();
100-
}
102+
if (swaggerResponseAttribute.Description != null)
103+
{
104+
response.Description = swaggerResponseAttribute.Description;
105+
}
101106

102-
if (swaggerResponseAttribute.Description != null)
103-
{
104-
response.Description = swaggerResponseAttribute.Description;
105-
}
107+
operation.Responses[statusCode] = response;
106108

107-
operation.Responses[statusCode] = response;
109+
if (swaggerResponseAttribute.ContentTypes != null)
110+
{
111+
response.Content.Clear();
108112

109-
if (swaggerResponseAttribute.ContentTypes != null)
113+
foreach (var contentType in swaggerResponseAttribute.ContentTypes)
110114
{
111-
response.Content.Clear();
112-
113-
foreach (var contentType in swaggerResponseAttribute.ContentTypes)
114-
{
115-
var schema = (swaggerResponseAttribute.Type != null && swaggerResponseAttribute.Type != typeof(void))
116-
? context.SchemaGenerator.GenerateSchema(swaggerResponseAttribute.Type, context.SchemaRepository)
117-
: null;
115+
var schema = (swaggerResponseAttribute.Type != null && swaggerResponseAttribute.Type != typeof(void))
116+
? context.SchemaGenerator.GenerateSchema(swaggerResponseAttribute.Type, context.SchemaRepository)
117+
: null;
118118

119-
response.Content.Add(contentType, new OpenApiMediaType { Schema = schema });
120-
}
119+
response.Content.Add(contentType, new OpenApiMediaType { Schema = schema });
121120
}
122121
}
123122
}

src/Swashbuckle.AspNetCore.Annotations/AnnotationsParameterFilter.cs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,46 @@
22
using Microsoft.OpenApi.Models;
33
using Swashbuckle.AspNetCore.SwaggerGen;
44

5-
namespace Swashbuckle.AspNetCore.Annotations
5+
namespace Swashbuckle.AspNetCore.Annotations;
6+
7+
public class AnnotationsParameterFilter : IParameterFilter
68
{
7-
public class AnnotationsParameterFilter : IParameterFilter
9+
public void Apply(OpenApiParameter parameter, ParameterFilterContext context)
810
{
9-
public void Apply(OpenApiParameter parameter, ParameterFilterContext context)
11+
if (context.PropertyInfo != null)
1012
{
11-
if (context.PropertyInfo != null)
12-
{
13-
ApplyPropertyAnnotations(parameter, context.PropertyInfo);
14-
}
15-
else if (context.ParameterInfo != null)
16-
{
17-
ApplyParamAnnotations(parameter, context.ParameterInfo);
18-
}
13+
ApplyPropertyAnnotations(parameter, context.PropertyInfo);
1914
}
20-
21-
private void ApplyPropertyAnnotations(OpenApiParameter parameter, PropertyInfo propertyInfo)
15+
else if (context.ParameterInfo != null)
2216
{
23-
var swaggerParameterAttribute = propertyInfo.GetCustomAttributes<SwaggerParameterAttribute>()
24-
.FirstOrDefault();
25-
26-
if (swaggerParameterAttribute != null)
27-
ApplySwaggerParameterAttribute(parameter, swaggerParameterAttribute);
17+
ApplyParamAnnotations(parameter, context.ParameterInfo);
2818
}
19+
}
2920

30-
private void ApplyParamAnnotations(OpenApiParameter parameter, ParameterInfo parameterInfo)
31-
{
21+
private void ApplyPropertyAnnotations(OpenApiParameter parameter, PropertyInfo propertyInfo)
22+
{
23+
var swaggerParameterAttribute = propertyInfo.GetCustomAttributes<SwaggerParameterAttribute>()
24+
.FirstOrDefault();
3225

33-
var swaggerParameterAttribute = parameterInfo.GetCustomAttribute<SwaggerParameterAttribute>();
26+
if (swaggerParameterAttribute != null)
27+
ApplySwaggerParameterAttribute(parameter, swaggerParameterAttribute);
28+
}
3429

35-
if (swaggerParameterAttribute != null)
36-
ApplySwaggerParameterAttribute(parameter, swaggerParameterAttribute);
37-
}
30+
private void ApplyParamAnnotations(OpenApiParameter parameter, ParameterInfo parameterInfo)
31+
{
3832

39-
private void ApplySwaggerParameterAttribute(OpenApiParameter parameter, SwaggerParameterAttribute swaggerParameterAttribute)
40-
{
41-
if (swaggerParameterAttribute.Description != null)
42-
parameter.Description = swaggerParameterAttribute.Description;
33+
var swaggerParameterAttribute = parameterInfo.GetCustomAttribute<SwaggerParameterAttribute>();
4334

44-
if (swaggerParameterAttribute.RequiredFlag.HasValue)
45-
parameter.Required = swaggerParameterAttribute.RequiredFlag.Value;
46-
}
35+
if (swaggerParameterAttribute != null)
36+
ApplySwaggerParameterAttribute(parameter, swaggerParameterAttribute);
37+
}
38+
39+
private void ApplySwaggerParameterAttribute(OpenApiParameter parameter, SwaggerParameterAttribute swaggerParameterAttribute)
40+
{
41+
if (swaggerParameterAttribute.Description != null)
42+
parameter.Description = swaggerParameterAttribute.Description;
43+
44+
if (swaggerParameterAttribute.RequiredFlag.HasValue)
45+
parameter.Required = swaggerParameterAttribute.RequiredFlag.Value;
4746
}
4847
}

0 commit comments

Comments
 (0)