Skip to content

Commit

Permalink
Added Explode and Style to SwaggerParameter
Browse files Browse the repository at this point in the history
  • Loading branch information
aberus committed Sep 4, 2024
1 parent 46c1bc9 commit e903d54
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ private void ApplyPropertyAnnotations(OpenApiParameter parameter, PropertyInfo p

private void ApplyParamAnnotations(OpenApiParameter parameter, ParameterInfo parameterInfo)
{

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

if (swaggerParameterAttribute != null)
Expand All @@ -44,6 +43,12 @@ private void ApplySwaggerParameterAttribute(OpenApiParameter parameter, SwaggerP

if (swaggerParameterAttribute.RequiredFlag.HasValue)
parameter.Required = swaggerParameterAttribute.RequiredFlag.Value;

if (swaggerParameterAttribute.ExplodeFlag.HasValue)
parameter.Explode = swaggerParameterAttribute.ExplodeFlag.Value;

if (swaggerParameterAttribute.ParameterStyle.HasValue)
parameter.Style = swaggerParameterAttribute.ParameterStyle.Value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Swashbuckle.AspNetCore.Annotations.SwaggerParameterAttribute.Explode.get -> bool
Swashbuckle.AspNetCore.Annotations.SwaggerParameterAttribute.Explode.set -> void
Swashbuckle.AspNetCore.Annotations.SwaggerParameterAttribute.Style.get -> string
Swashbuckle.AspNetCore.Annotations.SwaggerParameterAttribute.Style.set -> void
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Swashbuckle.AspNetCore.Annotations
{
Expand Down Expand Up @@ -30,5 +32,37 @@ public bool Required
}

internal bool? RequiredFlag { get; set; }

/// <summary>
/// Describes how the parameter value will be serialized depending on the type of
/// the parameter value. Default values (based on value of in): for query - form;
/// for path - simple; for header - simple; for cookie - form.
/// </summary>
public string Style
{
get { throw new InvalidOperationException($"Use {nameof(ParameterStyle)} instead"); }
set
{
ParameterStyle = Enum.TryParse(value, ignoreCase: true, out ParameterStyle result) ? result :
throw new InvalidOperationException(
message: $"Style '{value}' not defined in OpenAPI specification");
}
}

internal ParameterStyle? ParameterStyle { get; set; }

/// <summary>
/// When this is true, parameter values of type array or object generate separate parameters for
/// each value of the array or key-value pair of the map. For other types of parameters this property
/// has no effect. When style is form, the default value is true. For all other styles,
/// the default value is false.
/// </summary>
public bool Explode
{
get { throw new InvalidOperationException($"Use {nameof(ExplodeFlag)} instead"); }
set { ExplodeFlag = value; }
}

internal bool? ExplodeFlag { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,43 @@ public void Apply_DoesNotModifyTheRequiredFlag_IfNotSpecifiedWithSwaggerParamete
Assert.True(parameter.Required);
}

[Fact]
public void Apply_EnrichesParameterMetadata_IfPropertyDecoratedWithSwaggerParameterAttributeExplode()
{
var parameter = new OpenApiParameter { };
var parameterInfo = typeof(FakeControllerWithSwaggerAnnotations)
.GetMethod(nameof(FakeControllerWithSwaggerAnnotations.ActionWithSwaggerParameterAttributeExplode))
.GetParameters()[0];
var filterContext = new ParameterFilterContext(
apiParameterDescription: null,
schemaGenerator: null,
schemaRepository: null,
parameterInfo: parameterInfo);

Subject().Apply(parameter, filterContext);

Assert.True(parameter.Explode);
}

[Fact]
public void Apply_EnrichesParameterMetadata_IfPropertyDecoratedWithSwaggerParameterAttributeExplodeWithStyle()
{
var parameter = new OpenApiParameter { };
var parameterInfo = typeof(FakeControllerWithSwaggerAnnotations)
.GetMethod(nameof(FakeControllerWithSwaggerAnnotations.ActionWithSwaggerParameterAttributeExplodeWithStyle))
.GetParameters()[0];
var filterContext = new ParameterFilterContext(
apiParameterDescription: null,
schemaGenerator: null,
schemaRepository: null,
parameterInfo: parameterInfo);

Subject().Apply(parameter, filterContext);

Assert.True(parameter.Explode);
Assert.Equal(parameter.Style, ParameterStyle.Form);
}

private AnnotationsParameterFilter Subject()
{
return new AnnotationsParameterFilter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void Apply_DoesNotModifyTheRequiredFlag_IfNotSpecifiedWithSwaggerParamete

var requestBody = new OpenApiRequestBody { Required = true };
var parameterInfo = typeof(FakeControllerWithSwaggerAnnotations)
.GetMethod(nameof(FakeControllerWithSwaggerAnnotations.ActionWithSwaggerRequestbodyAttributeDescriptionOnly))
.GetMethod(nameof(FakeControllerWithSwaggerAnnotations.ActionWithSwaggerRequestBodyAttributeDescriptionOnly))
.GetParameters()[0];
var bodyParameterDescription = new ApiParameterDescription
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public void ActionWithSwaggerParameterAttributeDescriptionOnly(
[SwaggerParameter("Description for param")] string param)
{ }

public void ActionWithSwaggerParameterAttributeExplode(
[SwaggerParameter("Description for param", Explode = true)] List<string> param)
{ }

public void ActionWithSwaggerParameterAttributeExplodeWithStyle(
[SwaggerParameter("Description for param", Explode = true, Style = "form")] List<string> param)
{ }

public void ActionWithSwaggerSchemaAttribute(
[SwaggerSchema("Description for param", Format = "date")] string param)
{ }
Expand All @@ -33,7 +41,7 @@ public void ActionWithSwaggerRequestBodyAttribute(
[SwaggerRequestBody("Description for param", Required = true)] string param)
{ }

public void ActionWithSwaggerRequestbodyAttributeDescriptionOnly(
public void ActionWithSwaggerRequestBodyAttributeDescriptionOnly(
[SwaggerRequestBody("Description for param")] string param)
{ }

Expand Down

0 comments on commit e903d54

Please sign in to comment.