Skip to content
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

Added Explode and Style to SwaggerParameter #3065

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -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,34 @@ public bool Required
}

internal bool? RequiredFlag { get; set; }

/// <summary>
/// Describes how the parameter value will be serialized based on its type.
/// If not set, default values are for: query - form; path - simple; header - simple; 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 NotSupportedException($"'{value}' style is not supported");
}
}

internal ParameterStyle? ParameterStyle { get; set; }

/// <summary>
/// When true, array and object parameters are split into separate parameters. This has no
/// effect on other parameter types.
/// The default is true for form style and false for all other styles.
/// </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