Skip to content

Commit 072e664

Browse files
authored
Merge pull request #10 from genusP/dev
Support binnary response
2 parents 60c3932 + fa30c9a commit 072e664

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

ApiCodeGenerator.OpenApi.Refit.Tests/FunctionalTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,5 +541,31 @@ public void GenerateClientInterface_AuthHeaderParameter()
541541
//Act & Assert
542542
RunTest(settings, expected, "authSchema.json");
543543
}
544+
545+
[Test]
546+
public void GenerateClientInterface_BinaryResponse()
547+
{
548+
var settings = new RefitCodeGeneratorSettings
549+
{
550+
BinaryResponseType = "System.Net.Http.HttpContent",
551+
GenerateClientInterfaces = true,
552+
GenerateOptionalParameters = false,
553+
CSharpGeneratorSettings =
554+
{
555+
Namespace = "TestNS",
556+
},
557+
};
558+
559+
var expected =
560+
" public partial interface IClient\n" +
561+
" {\n" +
562+
" /// <exception cref=\"Refit.ApiException\">A server side error occurred.</exception>\n" +
563+
" [Get(\"/download\")]\n" +
564+
" System.Threading.Tasks.Task<System.Net.Http.HttpContent> Download();\n" +
565+
"\n" +
566+
" }\n";
567+
568+
RunTest(settings, expected, "streamResponse.yaml", " \n");
569+
}
544570
}
545571
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
openapi: 3.0.0
2+
info:
3+
title: ""
4+
version: "1.0.0"
5+
paths:
6+
/download:
7+
get:
8+
responses:
9+
"200":
10+
description: ""
11+
content:
12+
"application/json":
13+
schema:
14+
type: string
15+
format: binary

ApiCodeGenerator.OpenApi.Refit/RefitCodeGenerator.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ public RefitCodeGenerator(OpenApiDocument openApiDocument, RefitCodeGeneratorSet
3131
OpenApiDocument = openApiDocument;
3232
BaseSettings = settings;
3333
_settings = settings;
34-
_settings.CSharpGeneratorSettings.ExcludedTypeNames = ["FileParameter", .. _settings.CSharpGeneratorSettings.ExcludedTypeNames];
34+
_settings.CSharpGeneratorSettings.ExcludedTypeNames =
35+
[
36+
"FileParameter",
37+
"FileResponse",
38+
.. _settings.CSharpGeneratorSettings.ExcludedTypeNames
39+
];
3540
SetUsages();
3641
}
3742

@@ -48,6 +53,8 @@ internal RefitCodeGenerator(OpenApiDocument openApiDocument, RefitCodeGeneratorS
4853
/// </summary>
4954
protected OpenApiDocument OpenApiDocument { get; }
5055

56+
public override string GetBinaryResponseTypeName() => _settings.BinaryResponseType;
57+
5158
/// <inheritdoc />
5259
protected override IEnumerable<CodeArtifact> GenerateClientTypes(string controllerName, string controllerClassName, IEnumerable<CSharpOperationModel> operations)
5360
{

ApiCodeGenerator.OpenApi.Refit/RefitCodeGeneratorSettings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,10 @@ public RefitCodeGeneratorSettings()
6969
/// Тип используемый для двоичного содержимого.
7070
/// </summary>
7171
public string BinaryPartType { get; set; } = "StreamPart";
72+
73+
/// <summary>
74+
/// Тип используемый для двоичного ответа.
75+
/// </summary>
76+
public string BinaryResponseType { get; set; } = "System.IO.Stream";
7277
}
7378
}

schemas/nswag.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@
7878
"type": "string",
7979
"description": "The .NET type used for binary data (default: StreamPart).",
8080
"default": "StreamPart"
81+
},
82+
"binaryResponseType": {
83+
"type": "string",
84+
"description": "The .NET type used for binary response (default: System.IO.Stream).",
85+
"default": "System.IO.Stream"
8186
}
8287
}
8388
}

0 commit comments

Comments
 (0)