Skip to content

Commit 84944a0

Browse files
Apply analyzer suggestions (#3334)
Apply code analyzer suggestions for various types.
1 parent 32463c2 commit 84944a0

90 files changed

Lines changed: 1402 additions & 1157 deletions

File tree

Some content is hidden

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

src/Swashbuckle.AspNetCore.Annotations/AnnotationsDocumentFilter.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ public class AnnotationsDocumentFilter : IDocumentFilter
88
{
99
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
1010
{
11-
if (swaggerDoc.Tags == null)
12-
swaggerDoc.Tags = new List<OpenApiTag>();
11+
swaggerDoc.Tags ??= [];
1312

1413
// Collect (unique) controller names and custom attributes in a dictionary
1514
var controllerNamesAndAttributes = context.ApiDescriptions
@@ -24,7 +23,7 @@ public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
2423
}
2524
}
2625

27-
private void ApplySwaggerTagAttribute(
26+
private static void ApplySwaggerTagAttribute(
2827
OpenApiDocument swaggerDoc,
2928
string controllerName,
3029
IEnumerable<object> customAttributes)
@@ -33,7 +32,10 @@ private void ApplySwaggerTagAttribute(
3332
.OfType<SwaggerTagAttribute>()
3433
.FirstOrDefault();
3534

36-
if (swaggerTagAttribute == null) return;
35+
if (swaggerTagAttribute == null)
36+
{
37+
return;
38+
}
3739

3840
swaggerDoc.Tags.Add(new OpenApiTag
3941
{

src/Swashbuckle.AspNetCore.Annotations/AnnotationsOperationFilter.cs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context)
1111
IEnumerable<object> actionAttributes = [];
1212
IEnumerable<object> metadataAttributes = [];
1313

14-
if (context.MethodInfo != null)
14+
if (context.MethodInfo is { } methodInfo)
1515
{
16-
controllerAttributes = context.MethodInfo.DeclaringType.GetCustomAttributes(true);
17-
actionAttributes = context.MethodInfo.GetCustomAttributes(true);
16+
controllerAttributes = methodInfo.DeclaringType.GetCustomAttributes(true);
17+
actionAttributes = methodInfo.GetCustomAttributes(true);
1818
}
1919

2020
#if NET
21-
if (context.ApiDescription?.ActionDescriptor?.EndpointMetadata != null)
21+
if (context.ApiDescription?.ActionDescriptor?.EndpointMetadata is { } metadata)
2222
{
23-
metadataAttributes = context.ApiDescription.ActionDescriptor.EndpointMetadata;
23+
metadataAttributes = metadata;
2424
}
2525
#endif
2626

@@ -49,20 +49,29 @@ private static void ApplySwaggerOperationAttribute(
4949
.OfType<SwaggerOperationAttribute>()
5050
.FirstOrDefault();
5151

52-
if (swaggerOperationAttribute == null) return;
52+
if (swaggerOperationAttribute == null)
53+
{
54+
return;
55+
}
5356

54-
if (swaggerOperationAttribute.Summary != null)
55-
operation.Summary = swaggerOperationAttribute.Summary;
57+
if (swaggerOperationAttribute.Summary is { } summary)
58+
{
59+
operation.Summary = summary;
60+
}
5661

57-
if (swaggerOperationAttribute.Description != null)
58-
operation.Description = swaggerOperationAttribute.Description;
62+
if (swaggerOperationAttribute.Description is { } description)
63+
{
64+
operation.Description = description;
65+
}
5966

60-
if (swaggerOperationAttribute.OperationId != null)
61-
operation.OperationId = swaggerOperationAttribute.OperationId;
67+
if (swaggerOperationAttribute.OperationId is { } operationId)
68+
{
69+
operation.OperationId = operationId;
70+
}
6271

63-
if (swaggerOperationAttribute.Tags != null)
72+
if (swaggerOperationAttribute.Tags is { } tags)
6473
{
65-
operation.Tags = [.. swaggerOperationAttribute.Tags.Select(tagName => new OpenApiTag { Name = tagName })];
74+
operation.Tags = [.. tags.Select(tagName => new OpenApiTag { Name = tagName })];
6675
}
6776
}
6877

@@ -99,18 +108,18 @@ private static void ApplySwaggerResponseAttributes(
99108
response = new OpenApiResponse();
100109
}
101110

102-
if (swaggerResponseAttribute.Description != null)
111+
if (swaggerResponseAttribute.Description is { } description)
103112
{
104-
response.Description = swaggerResponseAttribute.Description;
113+
response.Description = description;
105114
}
106115

107116
operation.Responses[statusCode] = response;
108117

109-
if (swaggerResponseAttribute.ContentTypes != null)
118+
if (swaggerResponseAttribute.ContentTypes is { } contentTypes)
110119
{
111120
response.Content.Clear();
112121

113-
foreach (var contentType in swaggerResponseAttribute.ContentTypes)
122+
foreach (var contentType in contentTypes)
114123
{
115124
var schema = (swaggerResponseAttribute.Type != null && swaggerResponseAttribute.Type != typeof(void))
116125
? context.SchemaGenerator.GenerateSchema(swaggerResponseAttribute.Type, context.SchemaRepository)

src/Swashbuckle.AspNetCore.Annotations/AnnotationsParameterFilter.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,37 @@ public void Apply(OpenApiParameter parameter, ParameterFilterContext context)
1818
}
1919
}
2020

21-
private void ApplyPropertyAnnotations(OpenApiParameter parameter, PropertyInfo propertyInfo)
21+
private static void ApplyPropertyAnnotations(OpenApiParameter parameter, PropertyInfo propertyInfo)
2222
{
2323
var swaggerParameterAttribute = propertyInfo.GetCustomAttributes<SwaggerParameterAttribute>()
2424
.FirstOrDefault();
2525

2626
if (swaggerParameterAttribute != null)
27+
{
2728
ApplySwaggerParameterAttribute(parameter, swaggerParameterAttribute);
29+
}
2830
}
2931

30-
private void ApplyParamAnnotations(OpenApiParameter parameter, ParameterInfo parameterInfo)
32+
private static void ApplyParamAnnotations(OpenApiParameter parameter, ParameterInfo parameterInfo)
3133
{
32-
3334
var swaggerParameterAttribute = parameterInfo.GetCustomAttribute<SwaggerParameterAttribute>();
3435

3536
if (swaggerParameterAttribute != null)
37+
{
3638
ApplySwaggerParameterAttribute(parameter, swaggerParameterAttribute);
39+
}
3740
}
3841

39-
private void ApplySwaggerParameterAttribute(OpenApiParameter parameter, SwaggerParameterAttribute swaggerParameterAttribute)
42+
private static void ApplySwaggerParameterAttribute(OpenApiParameter parameter, SwaggerParameterAttribute swaggerParameterAttribute)
4043
{
4144
if (swaggerParameterAttribute.Description != null)
45+
{
4246
parameter.Description = swaggerParameterAttribute.Description;
47+
}
4348

4449
if (swaggerParameterAttribute.RequiredFlag.HasValue)
50+
{
4551
parameter.Required = swaggerParameterAttribute.RequiredFlag.Value;
52+
}
4653
}
4754
}

src/Swashbuckle.AspNetCore.Annotations/AnnotationsRequestBodyFilter.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ public void Apply(OpenApiRequestBody requestBody, RequestBodyFilterContext conte
1010
{
1111
var bodyParameterDescription = context.BodyParameterDescription;
1212

13-
if (bodyParameterDescription == null) return;
13+
if (bodyParameterDescription == null)
14+
{
15+
return;
16+
}
1417

1518
var propertyInfo = bodyParameterDescription.PropertyInfo();
1619
if (propertyInfo != null)
@@ -33,23 +36,31 @@ private void ApplyPropertyAnnotations(OpenApiRequestBody parameter, PropertyInfo
3336
.FirstOrDefault();
3437

3538
if (swaggerRequestBodyAttribute != null)
39+
{
3640
ApplySwaggerRequestBodyAttribute(parameter, swaggerRequestBodyAttribute);
41+
}
3742
}
3843

3944
private void ApplyParamAnnotations(OpenApiRequestBody requestBody, ParameterInfo parameterInfo)
4045
{
4146
var swaggerRequestBodyAttribute = parameterInfo.GetCustomAttribute<SwaggerRequestBodyAttribute>();
4247

4348
if (swaggerRequestBodyAttribute != null)
49+
{
4450
ApplySwaggerRequestBodyAttribute(requestBody, swaggerRequestBodyAttribute);
51+
}
4552
}
4653

4754
private void ApplySwaggerRequestBodyAttribute(OpenApiRequestBody parameter, SwaggerRequestBodyAttribute swaggerRequestBodyAttribute)
4855
{
4956
if (swaggerRequestBodyAttribute.Description != null)
57+
{
5058
parameter.Description = swaggerRequestBodyAttribute.Description;
59+
}
5160

5261
if (swaggerRequestBodyAttribute.RequiredFlag.HasValue)
62+
{
5363
parameter.Required = swaggerRequestBodyAttribute.RequiredFlag.Value;
64+
}
5465
}
5566
}

src/Swashbuckle.AspNetCore.Annotations/AnnotationsSchemaFilter.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@
55

66
namespace Swashbuckle.AspNetCore.Annotations;
77

8-
public class AnnotationsSchemaFilter : ISchemaFilter
8+
public class AnnotationsSchemaFilter(IServiceProvider serviceProvider) : ISchemaFilter
99
{
10-
private readonly IServiceProvider _serviceProvider;
11-
12-
public AnnotationsSchemaFilter(IServiceProvider serviceProvider)
13-
{
14-
_serviceProvider = serviceProvider;
15-
}
10+
private readonly IServiceProvider _serviceProvider = serviceProvider;
1611

1712
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
1813
{
@@ -37,7 +32,9 @@ private void ApplyTypeAnnotations(OpenApiSchema schema, SchemaFilterContext cont
3732
.FirstOrDefault();
3833

3934
if (schemaAttribute != null)
35+
{
4036
ApplySchemaAttribute(schema, schemaAttribute);
37+
}
4138

4239
var schemaFilterAttribute = context.Type.GetCustomAttributes<SwaggerSchemaFilterAttribute>()
4340
.FirstOrDefault();
@@ -53,45 +50,63 @@ private void ApplyTypeAnnotations(OpenApiSchema schema, SchemaFilterContext cont
5350
}
5451
}
5552

56-
private void ApplyParamAnnotations(OpenApiSchema schema, ParameterInfo parameterInfo)
53+
private static void ApplyParamAnnotations(OpenApiSchema schema, ParameterInfo parameterInfo)
5754
{
5855
var schemaAttribute = parameterInfo.GetCustomAttributes<SwaggerSchemaAttribute>()
5956
.FirstOrDefault();
6057

6158
if (schemaAttribute != null)
59+
{
6260
ApplySchemaAttribute(schema, schemaAttribute);
61+
}
6362
}
6463

65-
private void ApplyMemberAnnotations(OpenApiSchema schema, MemberInfo memberInfo)
64+
private static void ApplyMemberAnnotations(OpenApiSchema schema, MemberInfo memberInfo)
6665
{
6766
var schemaAttribute = memberInfo.GetCustomAttributes<SwaggerSchemaAttribute>()
6867
.FirstOrDefault();
6968

7069
if (schemaAttribute != null)
70+
{
7171
ApplySchemaAttribute(schema, schemaAttribute);
72+
}
7273
}
7374

74-
private void ApplySchemaAttribute(OpenApiSchema schema, SwaggerSchemaAttribute schemaAttribute)
75+
private static void ApplySchemaAttribute(OpenApiSchema schema, SwaggerSchemaAttribute schemaAttribute)
7576
{
7677
if (schemaAttribute.Description != null)
78+
{
7779
schema.Description = schemaAttribute.Description;
80+
}
7881

7982
if (schemaAttribute.Format != null)
83+
{
8084
schema.Format = schemaAttribute.Format;
85+
}
8186

8287
if (schemaAttribute.ReadOnlyFlag.HasValue)
88+
{
8389
schema.ReadOnly = schemaAttribute.ReadOnlyFlag.Value;
90+
}
8491

8592
if (schemaAttribute.WriteOnlyFlag.HasValue)
93+
{
8694
schema.WriteOnly = schemaAttribute.WriteOnlyFlag.Value;
95+
}
8796

8897
if (schemaAttribute.NullableFlag.HasValue)
98+
{
8999
schema.Nullable = schemaAttribute.NullableFlag.Value;
100+
}
90101

91102
if (schemaAttribute.Required != null)
103+
{
92104
schema.Required = new SortedSet<string>(schemaAttribute.Required);
105+
}
93106

94107
if (schemaAttribute.Title != null)
108+
{
95109
schema.Title = schemaAttribute.Title;
110+
}
96111
}
97112
}

src/Swashbuckle.AspNetCore.Annotations/AnnotationsSwaggerGenOptionsExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private static IEnumerable<Type> AnnotationsSubTypesSelector(Type type)
8484
}
8585
#endif
8686

87-
return Enumerable.Empty<Type>();
87+
return [];
8888
}
8989

9090
private static string AnnotationsDiscriminatorNameSelector(Type baseType)
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
namespace Swashbuckle.AspNetCore.Annotations;
22

33
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface, AllowMultiple = false)]
4-
public class SwaggerDiscriminatorAttribute : Attribute
4+
public class SwaggerDiscriminatorAttribute(string propertyName) : Attribute
55
{
6-
public SwaggerDiscriminatorAttribute(string propertyName)
7-
{
8-
PropertyName = propertyName;
9-
}
10-
11-
public string PropertyName { get; set; }
6+
public string PropertyName { get; set; } = propertyName;
127
}

src/Swashbuckle.AspNetCore.Annotations/SwaggerOperationAttribute.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,18 @@
44
/// Enriches Operation metadata for a given action method
55
/// </summary>
66
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
7-
public class SwaggerOperationAttribute : Attribute
7+
public class SwaggerOperationAttribute(string summary = null, string description = null) : Attribute
88
{
9-
public SwaggerOperationAttribute(string summary = null, string description = null)
10-
{
11-
Summary = summary;
12-
Description = description;
13-
}
14-
159
/// <summary>
1610
/// A short summary of what the operation does. For maximum readability in the swagger-ui,
1711
/// this field SHOULD be less than 120 characters.
1812
/// </summary>
19-
public string Summary { get; set; }
13+
public string Summary { get; set; } = summary;
2014

2115
/// <summary>
2216
/// A verbose explanation of the operation behavior. GFM syntax can be used for rich text representation.
2317
/// </summary>
24-
public string Description { get; set; }
18+
public string Description { get; set; } = description;
2519

2620
/// <summary>
2721
/// Unique string used to identify the operation. The id MUST be unique among all operations described
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
namespace Swashbuckle.AspNetCore.Annotations;
22

33
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
4-
public class SwaggerOperationFilterAttribute : Attribute
4+
public class SwaggerOperationFilterAttribute(Type filterType) : Attribute
55
{
6-
public SwaggerOperationFilterAttribute(Type filterType)
7-
{
8-
FilterType = filterType;
9-
}
10-
11-
public Type FilterType { get; private set; }
6+
public Type FilterType { get; private set; } = filterType;
127
}

src/Swashbuckle.AspNetCore.Annotations/SwaggerParameterAttribute.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@
44
/// Enriches Parameter metadata for "path", "query" or "header" bound parameters or properties
55
/// </summary>
66
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false)]
7-
public class SwaggerParameterAttribute : Attribute
7+
public class SwaggerParameterAttribute(string description = null) : Attribute
88
{
9-
public SwaggerParameterAttribute(string description = null)
10-
{
11-
Description = description;
12-
}
13-
149
/// <summary>
1510
/// A brief description of the parameter. This could contain examples of use.
1611
/// GFM syntax can be used for rich text representation
1712
/// </summary>
18-
public string Description { get; set; }
13+
public string Description { get; set; } = description;
1914

2015
/// <summary>
2116
/// Determines whether the parameter is mandatory. If the parameter is in "path",

0 commit comments

Comments
 (0)