Skip to content
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
17 changes: 15 additions & 2 deletions src/Refitter.Core/ParameterExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,21 @@ private static string GetQueryParameterType(
return type;
}

private static string FindSupportedType(string typeName) =>
typeName is "FileResponse" or "FileParameter" ? "StreamPart" : typeName;
private static string FindSupportedType(string typeName)
{
if (typeName is "FileResponse" or "FileParameter")
return "StreamPart";

// Handle collections of FileParameter/FileResponse
if (typeName.Contains("FileParameter") || typeName.Contains("FileResponse"))
{
Comment thread
christianhelle marked this conversation as resolved.
return typeName
.Replace("FileParameter", "StreamPart")
.Replace("FileResponse", "StreamPart");
}

return typeName;
}
Comment thread
christianhelle marked this conversation as resolved.

private static List<string> GetQueryParameters(CSharpOperationModel operationModel, RefitGeneratorSettings settings, string dynamicQuerystringParameterType, out string? dynamicQuerystringParameters)
{
Expand Down
103 changes: 103 additions & 0 deletions src/Refitter.Tests/Examples/MultiPartFormDataArrayTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using FluentAssertions;
using Refitter.Core;
using Refitter.Tests.Build;
using Refitter.Tests.TestUtilities;
using Xunit;

namespace Refitter.Tests.Examples;

public class MultiPartFormDataArrayTests
{
private const string OpenApiSpec = @"
{
""openapi"": ""3.0.2"",
""paths"": {
""/foo/{id}/files"": {
""post"": {
""summary"": ""uploads multiple files"",
""operationId"": ""uploadFiles"",
""parameters"": [
{
""name"": ""id"",
""in"": ""path"",
""description"": ""Id of the foo resource"",
""required"": true,
""schema"": {
""type"": ""integer"",
""format"": ""int64""
}
}
],
""requestBody"": {
""content"": {
""multipart/form-data"": {
""schema"": {
""type"": ""object"",
""properties"": {
""files"": {
""type"": ""array"",
""items"": {
""type"": ""string"",
""format"": ""binary""
}
}
}
}
}
}
},
""responses"": {
""200"": {
""description"": ""successful operation""
}
}
}
}
}
}
";

[Fact]
public async Task Can_Generate_Code()
{
string generatedCode = await GenerateCode();
generatedCode.Should().NotBeNullOrWhiteSpace();
}

[Fact]
public async Task Can_Build_Generated_Code()
{
string generatedCode = await GenerateCode();
BuildHelper
.BuildCSharp(generatedCode)
.Should()
.BeTrue();
}

[Fact]
public async Task Generated_Code_Contains_MultiPart_Attribute()
{
string generatedCode = await GenerateCode();
generatedCode.Should().Contain("[Multipart]");
generatedCode.Should().NotContain("Content-Type: multipart/form-data");
}

[Fact]
public async Task Generated_Code_Uses_IEnumerable_StreamPart_For_File_Array()
{
string generatedCode = await GenerateCode();
generatedCode.Should().Contain("long id, IEnumerable<StreamPart> files");
Comment thread
christianhelle marked this conversation as resolved.
generatedCode.Should().NotContain("IEnumerable<FileParameter> files");
}

private static async Task<string> GenerateCode()
{
var swaggerFile = await SwaggerFileHelper.CreateSwaggerFile(OpenApiSpec);
var settings = new RefitGeneratorSettings { OpenApiPath = swaggerFile };

var sut = await RefitGenerator.CreateAsync(settings);
var generatedCode = sut.Generate();
return generatedCode;
}

}
Loading