Skip to content

Commit c90282a

Browse files
committed
Add support for SwaggerIgnoreAttribute on parameters
1 parent cd831e2 commit c90282a

4 files changed

Lines changed: 34 additions & 1 deletion

File tree

src/Swashbuckle.AspNetCore.SwaggerGen/Annotations/SwaggerIgnoreAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ namespace Swashbuckle.AspNetCore.Annotations
1111
/// Can be used in combination with <see cref="System.Text.Json.Serialization.JsonExtensionDataAttribute"/>
1212
/// to capture and invalidate unsupported properties.
1313
/// </remarks>
14-
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property)]
14+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Parameter | AttributeTargets.Property)]
1515
public class SwaggerIgnoreAttribute : Attribute { }
1616
}

src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ private IList<OpenApiParameter> GenerateParameters(ApiDescription apiDescription
314314
{
315315
return (!apiParam.IsFromBody() && !apiParam.IsFromForm())
316316
&& (!apiParam.CustomAttributes().OfType<BindNeverAttribute>().Any())
317+
&& (!apiParam.CustomAttributes().OfType<SwaggerIgnoreAttribute>().Any())
317318
&& (apiParam.ModelMetadata == null || apiParam.ModelMetadata.IsBindingAllowed);
318319
});
319320

test/Swashbuckle.AspNetCore.SwaggerGen.Test/Fixtures/FakeController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public void ActionWithIntParameterWithDefaultValueAttribute([DefaultValue(3)]int
5050
public void ActionWithIntParameterWithRequiredAttribute([Required]int param)
5151
{ }
5252

53+
public void ActionWithIntParameterWithSwaggerIgnoreAttribute([SwaggerIgnore] int param)
54+
{ }
55+
5356
public void ActionWithObjectParameter(XmlAnnotatedType param)
5457
{ }
5558

test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,35 @@ public void GetSwagger_IgnoresParameters_IfActionParameterHasBindNeverAttribute(
473473
Assert.Empty(operation.Parameters);
474474
}
475475

476+
[Fact]
477+
public void GetSwagger_IgnoresParameters_IfActionParameterHasSwaggerIgnoreAttribute()
478+
{
479+
var subject = Subject(
480+
new[]
481+
{
482+
ApiDescriptionFactory.Create<FakeController>(
483+
c => nameof(c.ActionWithIntParameterWithSwaggerIgnoreAttribute),
484+
groupName: "v1",
485+
httpMethod: "POST",
486+
relativePath: "resource",
487+
parameterDescriptions: new[]
488+
{
489+
new ApiParameterDescription
490+
{
491+
Name = "param",
492+
Source = BindingSource.Query
493+
}
494+
}
495+
)
496+
}
497+
);
498+
499+
var document = subject.GetSwagger("v1");
500+
501+
var operation = document.Paths["/resource"].Operations[OperationType.Post];
502+
Assert.Empty(operation.Parameters);
503+
}
504+
476505
[Fact]
477506
public void GetSwagger_SetsParameterRequired_IfApiParameterIsBoundToPath()
478507
{

0 commit comments

Comments
 (0)