Skip to content

Apply analyzer suggestions #3334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ public class AnnotationsDocumentFilter : IDocumentFilter
{
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
if (swaggerDoc.Tags == null)
swaggerDoc.Tags = new List<OpenApiTag>();
swaggerDoc.Tags ??= [];

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

private void ApplySwaggerTagAttribute(
private static void ApplySwaggerTagAttribute(
OpenApiDocument swaggerDoc,
string controllerName,
IEnumerable<object> customAttributes)
Expand All @@ -33,7 +32,10 @@ private void ApplySwaggerTagAttribute(
.OfType<SwaggerTagAttribute>()
.FirstOrDefault();

if (swaggerTagAttribute == null) return;
if (swaggerTagAttribute == null)
{
return;
}

swaggerDoc.Tags.Add(new OpenApiTag
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context)
IEnumerable<object> actionAttributes = [];
IEnumerable<object> metadataAttributes = [];

if (context.MethodInfo != null)
if (context.MethodInfo is { } methodInfo)
{
controllerAttributes = context.MethodInfo.DeclaringType.GetCustomAttributes(true);
actionAttributes = context.MethodInfo.GetCustomAttributes(true);
controllerAttributes = methodInfo.DeclaringType.GetCustomAttributes(true);
actionAttributes = methodInfo.GetCustomAttributes(true);
}

#if NET
if (context.ApiDescription?.ActionDescriptor?.EndpointMetadata != null)
if (context.ApiDescription?.ActionDescriptor?.EndpointMetadata is { } metadata)
{
metadataAttributes = context.ApiDescription.ActionDescriptor.EndpointMetadata;
metadataAttributes = metadata;
}
#endif

Expand Down Expand Up @@ -49,20 +49,29 @@ private static void ApplySwaggerOperationAttribute(
.OfType<SwaggerOperationAttribute>()
.FirstOrDefault();

if (swaggerOperationAttribute == null) return;
if (swaggerOperationAttribute == null)
{
return;
}

if (swaggerOperationAttribute.Summary != null)
operation.Summary = swaggerOperationAttribute.Summary;
if (swaggerOperationAttribute.Summary is { } summary)
{
operation.Summary = summary;
}

if (swaggerOperationAttribute.Description != null)
operation.Description = swaggerOperationAttribute.Description;
if (swaggerOperationAttribute.Description is { } description)
{
operation.Description = description;
}

if (swaggerOperationAttribute.OperationId != null)
operation.OperationId = swaggerOperationAttribute.OperationId;
if (swaggerOperationAttribute.OperationId is { } operationId)
{
operation.OperationId = operationId;
}

if (swaggerOperationAttribute.Tags != null)
if (swaggerOperationAttribute.Tags is { } tags)
{
operation.Tags = [.. swaggerOperationAttribute.Tags.Select(tagName => new OpenApiTag { Name = tagName })];
operation.Tags = [.. tags.Select(tagName => new OpenApiTag { Name = tagName })];
}
}

Expand Down Expand Up @@ -99,18 +108,18 @@ private static void ApplySwaggerResponseAttributes(
response = new OpenApiResponse();
}

if (swaggerResponseAttribute.Description != null)
if (swaggerResponseAttribute.Description is { } description)
{
response.Description = swaggerResponseAttribute.Description;
response.Description = description;
}

operation.Responses[statusCode] = response;

if (swaggerResponseAttribute.ContentTypes != null)
if (swaggerResponseAttribute.ContentTypes is { } contentTypes)
{
response.Content.Clear();

foreach (var contentType in swaggerResponseAttribute.ContentTypes)
foreach (var contentType in contentTypes)
{
var schema = (swaggerResponseAttribute.Type != null && swaggerResponseAttribute.Type != typeof(void))
? context.SchemaGenerator.GenerateSchema(swaggerResponseAttribute.Type, context.SchemaRepository)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,37 @@ public void Apply(OpenApiParameter parameter, ParameterFilterContext context)
}
}

private void ApplyPropertyAnnotations(OpenApiParameter parameter, PropertyInfo propertyInfo)
private static void ApplyPropertyAnnotations(OpenApiParameter parameter, PropertyInfo propertyInfo)
{
var swaggerParameterAttribute = propertyInfo.GetCustomAttributes<SwaggerParameterAttribute>()
.FirstOrDefault();

if (swaggerParameterAttribute != null)
{
ApplySwaggerParameterAttribute(parameter, swaggerParameterAttribute);
}
}

private void ApplyParamAnnotations(OpenApiParameter parameter, ParameterInfo parameterInfo)
private static void ApplyParamAnnotations(OpenApiParameter parameter, ParameterInfo parameterInfo)
{

var swaggerParameterAttribute = parameterInfo.GetCustomAttribute<SwaggerParameterAttribute>();

if (swaggerParameterAttribute != null)
{
ApplySwaggerParameterAttribute(parameter, swaggerParameterAttribute);
}
}

private void ApplySwaggerParameterAttribute(OpenApiParameter parameter, SwaggerParameterAttribute swaggerParameterAttribute)
private static void ApplySwaggerParameterAttribute(OpenApiParameter parameter, SwaggerParameterAttribute swaggerParameterAttribute)
{
if (swaggerParameterAttribute.Description != null)
{
parameter.Description = swaggerParameterAttribute.Description;
}

if (swaggerParameterAttribute.RequiredFlag.HasValue)
{
parameter.Required = swaggerParameterAttribute.RequiredFlag.Value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ public void Apply(OpenApiRequestBody requestBody, RequestBodyFilterContext conte
{
var bodyParameterDescription = context.BodyParameterDescription;

if (bodyParameterDescription == null) return;
if (bodyParameterDescription == null)
{
return;
}

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

if (swaggerRequestBodyAttribute != null)
{
ApplySwaggerRequestBodyAttribute(parameter, swaggerRequestBodyAttribute);
}
}

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

if (swaggerRequestBodyAttribute != null)
{
ApplySwaggerRequestBodyAttribute(requestBody, swaggerRequestBodyAttribute);
}
}

private void ApplySwaggerRequestBodyAttribute(OpenApiRequestBody parameter, SwaggerRequestBodyAttribute swaggerRequestBodyAttribute)
{
if (swaggerRequestBodyAttribute.Description != null)
{
parameter.Description = swaggerRequestBodyAttribute.Description;
}

if (swaggerRequestBodyAttribute.RequiredFlag.HasValue)
{
parameter.Required = swaggerRequestBodyAttribute.RequiredFlag.Value;
}
}
}
35 changes: 25 additions & 10 deletions src/Swashbuckle.AspNetCore.Annotations/AnnotationsSchemaFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@

namespace Swashbuckle.AspNetCore.Annotations;

public class AnnotationsSchemaFilter : ISchemaFilter
public class AnnotationsSchemaFilter(IServiceProvider serviceProvider) : ISchemaFilter
{
private readonly IServiceProvider _serviceProvider;

public AnnotationsSchemaFilter(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
private readonly IServiceProvider _serviceProvider = serviceProvider;

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

if (schemaAttribute != null)
{
ApplySchemaAttribute(schema, schemaAttribute);
}

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

private void ApplyParamAnnotations(OpenApiSchema schema, ParameterInfo parameterInfo)
private static void ApplyParamAnnotations(OpenApiSchema schema, ParameterInfo parameterInfo)
{
var schemaAttribute = parameterInfo.GetCustomAttributes<SwaggerSchemaAttribute>()
.FirstOrDefault();

if (schemaAttribute != null)
{
ApplySchemaAttribute(schema, schemaAttribute);
}
}

private void ApplyMemberAnnotations(OpenApiSchema schema, MemberInfo memberInfo)
private static void ApplyMemberAnnotations(OpenApiSchema schema, MemberInfo memberInfo)
{
var schemaAttribute = memberInfo.GetCustomAttributes<SwaggerSchemaAttribute>()
.FirstOrDefault();

if (schemaAttribute != null)
{
ApplySchemaAttribute(schema, schemaAttribute);
}
}

private void ApplySchemaAttribute(OpenApiSchema schema, SwaggerSchemaAttribute schemaAttribute)
private static void ApplySchemaAttribute(OpenApiSchema schema, SwaggerSchemaAttribute schemaAttribute)
{
if (schemaAttribute.Description != null)
{
schema.Description = schemaAttribute.Description;
}

if (schemaAttribute.Format != null)
{
schema.Format = schemaAttribute.Format;
}

if (schemaAttribute.ReadOnlyFlag.HasValue)
{
schema.ReadOnly = schemaAttribute.ReadOnlyFlag.Value;
}

if (schemaAttribute.WriteOnlyFlag.HasValue)
{
schema.WriteOnly = schemaAttribute.WriteOnlyFlag.Value;
}

if (schemaAttribute.NullableFlag.HasValue)
{
schema.Nullable = schemaAttribute.NullableFlag.Value;
}

if (schemaAttribute.Required != null)
{
schema.Required = new SortedSet<string>(schemaAttribute.Required);
}

if (schemaAttribute.Title != null)
{
schema.Title = schemaAttribute.Title;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private static IEnumerable<Type> AnnotationsSubTypesSelector(Type type)
}
#endif

return Enumerable.Empty<Type>();
return [];
}

private static string AnnotationsDiscriminatorNameSelector(Type baseType)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
namespace Swashbuckle.AspNetCore.Annotations;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface, AllowMultiple = false)]
public class SwaggerDiscriminatorAttribute : Attribute
public class SwaggerDiscriminatorAttribute(string propertyName) : Attribute
{
public SwaggerDiscriminatorAttribute(string propertyName)
{
PropertyName = propertyName;
}

public string PropertyName { get; set; }
public string PropertyName { get; set; } = propertyName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,18 @@
/// Enriches Operation metadata for a given action method
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class SwaggerOperationAttribute : Attribute
public class SwaggerOperationAttribute(string summary = null, string description = null) : Attribute
{
public SwaggerOperationAttribute(string summary = null, string description = null)
{
Summary = summary;
Description = description;
}

/// <summary>
/// A short summary of what the operation does. For maximum readability in the swagger-ui,
/// this field SHOULD be less than 120 characters.
/// </summary>
public string Summary { get; set; }
public string Summary { get; set; } = summary;

/// <summary>
/// A verbose explanation of the operation behavior. GFM syntax can be used for rich text representation.
/// </summary>
public string Description { get; set; }
public string Description { get; set; } = description;

/// <summary>
/// Unique string used to identify the operation. The id MUST be unique among all operations described
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
namespace Swashbuckle.AspNetCore.Annotations;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
public class SwaggerOperationFilterAttribute : Attribute
public class SwaggerOperationFilterAttribute(Type filterType) : Attribute
{
public SwaggerOperationFilterAttribute(Type filterType)
{
FilterType = filterType;
}

public Type FilterType { get; private set; }
public Type FilterType { get; private set; } = filterType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@
/// Enriches Parameter metadata for "path", "query" or "header" bound parameters or properties
/// </summary>
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false)]
public class SwaggerParameterAttribute : Attribute
public class SwaggerParameterAttribute(string description = null) : Attribute
{
public SwaggerParameterAttribute(string description = null)
{
Description = description;
}

/// <summary>
/// A brief description of the parameter. This could contain examples of use.
/// GFM syntax can be used for rich text representation
/// </summary>
public string Description { get; set; }
public string Description { get; set; } = description;

/// <summary>
/// Determines whether the parameter is mandatory. If the parameter is in "path",
Expand Down
Loading