Skip to content

Commit ce35e62

Browse files
committed
fix #230
1 parent 0337184 commit ce35e62

File tree

3 files changed

+70
-37
lines changed

3 files changed

+70
-37
lines changed

WebApiClientCore.OpenApi.SourceGenerator/HttpApiMethod.cs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,47 @@ public override string ActualOperationName
7171
/// <returns></returns>
7272
protected override string ResolveParameterType(OpenApiParameter parameter)
7373
{
74-
if (parameter.IsBinary || parameter.IsBinaryBodyParameter)
74+
var isFileParameter = IsFileParameter(parameter) || IsFileParameter(parameter.Schema);
75+
if (isFileParameter)
7576
{
76-
if (parameter.CollectionFormat == OpenApiParameterCollectionFormat.Multi && !parameter.ActualSchema.Type.HasFlag(JsonObjectType.Array))
77-
{
78-
return "IEnumerable<FormDataFile>";
79-
}
80-
return "FormDataFile";
77+
var isArrayParameter = IsArrayParameter(parameter) || IsArrayParameter(parameter.Schema);
78+
return isArrayParameter ? "IEnumerable<FormDataFile>" : "FormDataFile";
8179
}
8280
return base.ResolveParameterType(parameter);
8381
}
82+
83+
private static bool IsFileParameter(JsonSchema parameter)
84+
{
85+
if (parameter == null)
86+
{
87+
return false;
88+
}
89+
90+
if (parameter.IsBinary)
91+
{
92+
return true;
93+
}
94+
return parameter.IsArray && parameter.Item?.IsBinary == true;
95+
}
96+
97+
private static bool IsArrayParameter(JsonSchema parameter)
98+
{
99+
if (parameter == null)
100+
{
101+
return false;
102+
}
103+
104+
if (parameter.Type.HasFlag(JsonObjectType.Array))
105+
{
106+
return true;
107+
}
108+
109+
if (parameter is OpenApiParameter apiParameter)
110+
{
111+
return apiParameter.CollectionFormat == OpenApiParameterCollectionFormat.Multi;
112+
}
113+
114+
return false;
115+
}
84116
}
85117
}

WebApiClientCore.OpenApi.SourceGenerator/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"profiles": {
3-
"WebApiClient.Tools.Swagger": {
3+
"WebApiClientCore.OpenApi.SourceGenerator": {
44
"commandName": "Project",
55
"commandLineArgs": "-o petstore.json"
66
}
Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>netcoreapp3.1;net5;net6;net7</TargetFrameworks>
6-
7-
<Summary>将本地或远程OpenApi文档解析生成WebApiClientCore的接口定义代码文件的工具</Summary>
8-
<SatelliteResourceLanguages>zh-Hans</SatelliteResourceLanguages>
3+
<PropertyGroup>
4+
<Version>2.0.4.1</Version>
5+
<OutputType>Exe</OutputType>
6+
<TargetFrameworks>netcoreapp3.1;net5;net6;net7</TargetFrameworks>
97

10-
<Description>将OpenApi的本地或远程文档解析生成WebApiClientCore的接口定义代码文件</Description>
11-
<Summary>将OpenApi的本地或远程文档解析生成WebApiClientCore的接口定义代码文件</Summary>
8+
<Summary>将本地或远程OpenApi文档解析生成WebApiClientCore的接口定义代码文件的工具</Summary>
9+
<SatelliteResourceLanguages>zh-Hans</SatelliteResourceLanguages>
1210

13-
<PackAsTool>true</PackAsTool>
14-
</PropertyGroup>
11+
<Description>将OpenApi的本地或远程文档解析生成WebApiClientCore的接口定义代码文件</Description>
12+
<Summary>将OpenApi的本地或远程文档解析生成WebApiClientCore的接口定义代码文件</Summary>
1513

16-
<ItemGroup>
17-
<PackageReference Include="CommandLineParser" Version="2.8.0" />
18-
<PackageReference Include="RazorEngineCore" Version="2020.6.1" />
19-
<PackageReference Include="NSwag.CodeGeneration.CSharp" Version="13.6.2" />
20-
</ItemGroup>
14+
<PackAsTool>true</PackAsTool>
15+
</PropertyGroup>
2116

22-
<ItemGroup>
23-
<None Update="petstore.json">
24-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
25-
</None>
26-
<None Update="Views\HttpApi.cshtml">
27-
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
28-
<Pack>$(IncludeRazorContentInPack)</Pack>
29-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
30-
</None>
31-
<None Update="Views\HttpModel.cshtml">
32-
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
33-
<Pack>$(IncludeRazorContentInPack)</Pack>
34-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
35-
</None>
36-
</ItemGroup>
17+
<ItemGroup>
18+
<PackageReference Include="CommandLineParser" Version="2.8.0" />
19+
<PackageReference Include="RazorEngineCore" Version="2020.6.1" />
20+
<PackageReference Include="NSwag.CodeGeneration.CSharp" Version="13.19.0" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<None Update="petstore.json">
25+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
26+
</None>
27+
<None Update="Views\HttpApi.cshtml">
28+
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
29+
<Pack>$(IncludeRazorContentInPack)</Pack>
30+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
31+
</None>
32+
<None Update="Views\HttpModel.cshtml">
33+
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
34+
<Pack>$(IncludeRazorContentInPack)</Pack>
35+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
36+
</None>
37+
</ItemGroup>
3738

3839
</Project>

0 commit comments

Comments
 (0)