Skip to content

Commit 041e9b5

Browse files
author
nikunj-bhargava
committed
Minor: Added Test Cases
1 parent d228bb2 commit 041e9b5

File tree

5 files changed

+107
-16
lines changed

5 files changed

+107
-16
lines changed

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"sdk": {
3-
"version": "8.0.204",
3+
"version": "7.0.2",
44
"allowPrerelease": false,
55
"rollForward": "latestMajor"
66
}

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Linq;
55
using System.Reflection;
66
using Microsoft.AspNetCore.Http;
7-
using Microsoft.AspNetCore.Mvc;
87
using Microsoft.AspNetCore.Mvc.ApiExplorer;
98
using Microsoft.AspNetCore.Mvc.Controllers;
109
using Microsoft.AspNetCore.Mvc.ModelBinding;
@@ -112,17 +111,5 @@ internal static bool IsFromForm(this ApiParameterDescription apiParameter)
112111
return (source == BindingSource.Form || source == BindingSource.FormFile)
113112
|| (elementType != null && typeof(IFormFile).IsAssignableFrom(elementType));
114113
}
115-
116-
internal static bool IsFromFormAttributeUsedWithFormFile(this ApiParameterDescription apiParameter)
117-
{
118-
// Retrieve parameter information
119-
var parameterInfo = ParameterInfo(apiParameter);
120-
121-
// Retrieve attributes from the parameter
122-
var fromFormAttribute = parameterInfo?.GetCustomAttribute<FromFormAttribute>();
123-
124-
return fromFormAttribute != null && parameterInfo?.ParameterType == typeof(IFormFile);
125-
126-
}
127114
}
128115
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using Microsoft.OpenApi.Models;
1212
using Swashbuckle.AspNetCore.Annotations;
1313
using Swashbuckle.AspNetCore.Swagger;
14+
using Microsoft.AspNetCore.Http;
15+
1416
#if NET7_0_OR_GREATER
1517
using Microsoft.AspNetCore.Http.Metadata;
1618
#endif
@@ -328,10 +330,10 @@ private IList<OpenApiTag> GenerateOperationTags(ApiDescription apiDescription)
328330

329331
private IList<OpenApiParameter> GenerateParameters(ApiDescription apiDescription, SchemaRepository schemaRespository)
330332
{
331-
if (apiDescription.ParameterDescriptions.Any(apiParam => apiParam.IsFromFormAttributeUsedWithFormFile()))
333+
if (apiDescription.ParameterDescriptions.Any(apiParam => IsFromFormAttributeUsedWithIFormFile(apiParam)))
332334
throw new SwaggerGeneratorException(string.Format(
333335
"Error reading parameter(s) for action {0} as [FromForm] attribute used with IFormFile. " +
334-
"Please refer https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/README.md#handle-forms-and-file-uploads",
336+
"Please refer to https://github.com/domaindrivendev/Swashbuckle.AspNetCore#handle-forms-and-file-uploads for more information",
335337
apiDescription.ActionDescriptor.DisplayName));
336338

337339
var applicableApiParameters = apiDescription.ParameterDescriptions
@@ -632,6 +634,17 @@ private OpenApiMediaType CreateResponseMediaType(ModelMetadata modelMetadata, Sc
632634
};
633635
}
634636

637+
private bool IsFromFormAttributeUsedWithIFormFile(ApiParameterDescription apiParameter)
638+
{
639+
// Retrieve parameter information
640+
var parameterInfo = apiParameter.ParameterInfo();
641+
642+
// Check if Parameter has FromForm Attribute
643+
var fromFormAttribute = parameterInfo?.GetCustomAttribute<FromFormAttribute>();
644+
645+
return fromFormAttribute != null && parameterInfo?.ParameterType == typeof(IFormFile);
646+
}
647+
635648
private static readonly Dictionary<string, OperationType> OperationTypeMap = new Dictionary<string, OperationType>
636649
{
637650
{ "GET", OperationType.Get },

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.ComponentModel;
33
using System.ComponentModel.DataAnnotations;
4+
using Microsoft.AspNetCore.Http;
45
using Microsoft.AspNetCore.Mvc;
56
using Microsoft.AspNetCore.Mvc.ModelBinding;
67
using Swashbuckle.AspNetCore.Annotations;
@@ -84,5 +85,11 @@ public int ActionWithProducesAttribute()
8485
[SwaggerIgnore]
8586
public void ActionWithSwaggerIgnoreAttribute()
8687
{ }
88+
89+
public void ActionHavingIFormFileParamWithFromFormAtribute([FromForm] IFormFile fileUpload)
90+
{ }
91+
92+
public void ActionHavingFromFormAtributeButNotWithIFormFile([FromForm] string param1, IFormFile param2)
93+
{ }
8794
}
8895
}

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
using Swashbuckle.AspNetCore.Swagger;
1515
using Swashbuckle.AspNetCore.TestSupport;
1616
using Xunit;
17+
using System.Threading.Tasks;
18+
using Microsoft.AspNetCore.Authentication;
19+
using Microsoft.AspNetCore.Mvc.Controllers;
1720

1821
namespace Swashbuckle.AspNetCore.SwaggerGen.Test
1922
{
@@ -1433,6 +1436,87 @@ public void GetSwagger_GeneratesSwaggerDocument_ThrowsIfHttpMethodNotSupported(s
14331436
Assert.Equal($"The \"{httpMethod}\" HTTP method is not supported.", exception.Message);
14341437
}
14351438

1439+
[Fact]
1440+
public void GetSwagger_Throws_Exception_When_FromForm_Attribute_Used_With_IFormFile()
1441+
{
1442+
var parameterInfo = typeof(FakeController)
1443+
.GetMethod(nameof(FakeController.ActionHavingIFormFileParamWithFromFormAtribute))
1444+
.GetParameters()[0];
1445+
1446+
1447+
var subject = Subject(
1448+
apiDescriptions: new[]
1449+
{
1450+
ApiDescriptionFactory.Create<FakeController>(
1451+
c => nameof(c.ActionHavingIFormFileParamWithFromFormAtribute),
1452+
groupName: "v1",
1453+
httpMethod: "POST",
1454+
relativePath: "resource",
1455+
parameterDescriptions: new[]
1456+
{
1457+
new ApiParameterDescription
1458+
{
1459+
Name = "fileUpload", // Name of the parameter
1460+
Type = typeof(IFormFile), // Type of the parameter
1461+
ParameterDescriptor = new ControllerParameterDescriptor { ParameterInfo = parameterInfo }
1462+
}
1463+
})
1464+
}
1465+
);
1466+
1467+
var exception = Assert.Throws<SwaggerGeneratorException>(() => subject.GetSwagger("v1"));
1468+
}
1469+
1470+
[Fact]
1471+
public void GetSwagger_Works_As_Expected_When_FromForm_Attribute_Not_Used_With_IFormFile()
1472+
{
1473+
var paraminfo = typeof(FakeController)
1474+
.GetMethod(nameof(FakeController.ActionHavingFromFormAtributeButNotWithIFormFile))
1475+
.GetParameters()[0];
1476+
1477+
var fileUploadParameterInfo = typeof(FakeController)
1478+
.GetMethod(nameof(FakeController.ActionHavingFromFormAtributeButNotWithIFormFile))
1479+
.GetParameters()[1];
1480+
1481+
var subject = Subject(
1482+
apiDescriptions: new[]
1483+
{
1484+
ApiDescriptionFactory.Create<FakeController>(
1485+
c => nameof(c.ActionHavingFromFormAtributeButNotWithIFormFile),
1486+
groupName: "v1",
1487+
httpMethod: "POST",
1488+
relativePath: "resource",
1489+
parameterDescriptions: new[]
1490+
{
1491+
new ApiParameterDescription
1492+
{
1493+
Name = "param1", // Name of the parameter
1494+
Type = typeof(string), // Type of the parameter
1495+
ParameterDescriptor = new ControllerParameterDescriptor { ParameterInfo = paraminfo }
1496+
},
1497+
new ApiParameterDescription
1498+
{
1499+
Name = "param2", // Name of the parameter
1500+
Type = typeof(IFormFile), // Type of the parameter
1501+
ParameterDescriptor = new ControllerParameterDescriptor { ParameterInfo = fileUploadParameterInfo }
1502+
}
1503+
})
1504+
}
1505+
);
1506+
1507+
var document = subject.GetSwagger("v1");
1508+
1509+
Assert.Equal("V1", document.Info.Version);
1510+
Assert.Equal("Test API", document.Info.Title);
1511+
Assert.Equal(new[] { "/resource" }, document.Paths.Keys.ToArray());
1512+
1513+
var operation = document.Paths["/resource"].Operations[OperationType.Post];
1514+
Assert.NotNull(operation.Parameters);
1515+
Assert.Equal(2, operation.Parameters.Count);
1516+
Assert.Equal("param1", operation.Parameters[0].Name);
1517+
Assert.Equal("param2", operation.Parameters[1].Name);
1518+
}
1519+
14361520
private static SwaggerGenerator Subject(
14371521
IEnumerable<ApiDescription> apiDescriptions,
14381522
SwaggerGeneratorOptions options = null,

0 commit comments

Comments
 (0)