-
Notifications
You must be signed in to change notification settings - Fork 447
/
Copy pathOpenApiDoc.cs
219 lines (195 loc) · 7.42 KB
/
OpenApiDoc.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using NJsonSchema.CodeGeneration;
using NJsonSchema.CodeGeneration.CSharp;
using NSwag;
using NSwag.CodeGeneration;
using NSwag.CodeGeneration.CSharp;
using NSwag.CodeGeneration.CSharp.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace WebApiClientCore.OpenApi.SourceGenerator
{
/// <summary>
/// 表示OpenApi描述
/// </summary>
public class OpenApiDoc
{
private readonly CSharpTypeResolver resolver;
/// <summary>
/// 获��Swagger文档
/// </summary>
public OpenApiDocument Document { get; private set; }
/// <summary>
/// 获取Swagger设置项
/// </summary>
public HttpApiSettings Settings { get; private set; }
/// <summary>
/// OpenApi描述
/// </summary>
/// <param name="options">选项</param>
public OpenApiDoc(OpenApiDocOptions options)
: this(GetDocument(options.OpenApi))
{
if (string.IsNullOrEmpty(options.Namespace) == false)
{
this.Settings.NameSpace = options.Namespace;
this.Settings.CSharpGeneratorSettings.Namespace = options.Namespace;
}
}
/// <summary>
/// OpenApi描述
/// </summary>
/// <param name="document">Swagger文档</param>
public OpenApiDoc(OpenApiDocument document)
{
this.Document = document;
this.Settings = new HttpApiSettings();
this.resolver = CSharpGeneratorBase
.CreateResolverWithExceptionSchema(this.Settings.CSharpGeneratorSettings, document);
}
/// <summary>
/// 获取OpenApi描述文档
/// </summary>
/// <param name="openApi"></param>
/// <returns></returns>
private static OpenApiDocument GetDocument(string openApi)
{
Console.WriteLine($"正在分析OpenApi:{openApi}");
if (Uri.TryCreate(openApi, UriKind.Absolute, out var api))
{
if (api.Scheme.StartsWith(Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase))
{
return OpenApiDocument.FromUrlAsync(openApi).Result;
}
}
return OpenApiDocument.FromFileAsync(openApi).Result;
}
/// <summary>
/// 生成代码并保存到文件
/// </summary>
public void GenerateFiles()
{
var dir = Path.Combine("output", this.Settings.NameSpace);
var apisPath = Path.Combine(dir, "HttpApis");
var modelsPath = Path.Combine(dir, "HttpModels");
Directory.CreateDirectory(apisPath);
Directory.CreateDirectory(modelsPath);
var apis = new HttpApiProvider(this).GetHttpApis();
foreach (var api in apis)
{
var file = Path.Combine(apisPath, $"{api.TypeName}.cs");
File.WriteAllText(file, api.ToString(), Encoding.UTF8);
Console.WriteLine($"输出接口文件:{file}");
}
var models = new HttpModelProvider(this).GetHttpModels();
foreach (var model in models)
{
var file = Path.Combine(modelsPath, $"{model.TypeName}.cs");
File.WriteAllText(file, model.ToString(), Encoding.UTF8);
Console.WriteLine($"输出模型文件:{file}");
}
Console.WriteLine($"共输出{apis.Length + models.Length}个文件..");
}
/// <summary>
/// 表示HttpApi提供者
/// </summary>
private class HttpApiProvider : CSharpControllerGenerator
{
/// <summary>
/// openApi
/// </summary>
private readonly OpenApiDoc openApi;
/// <summary>
/// api列表
/// </summary>
private readonly List<HttpApi> httpApiList = [];
/// <summary>
/// HttpApi提供者
/// </summary>
/// <param name="openApi"></param>
public HttpApiProvider(OpenApiDoc openApi)
: base(openApi.Document, openApi.Settings, openApi.resolver)
{
this.openApi = openApi;
}
/// <summary>
/// 获取所有HttpApi描述模型
/// </summary>
/// <returns></returns>
public HttpApi[] GetHttpApis()
{
this.httpApiList.Clear();
this.GenerateFile();
return this.httpApiList.ToArray();
}
/// <summary>
/// 生成客户端调用代码
/// 但实际只为了获得HttpApi实例
/// </summary>
/// <param name="controllerName"></param>
/// <param name="controllerClassName"></param>
/// <param name="operations"></param>
/// <returns></returns>
protected override IEnumerable<CodeArtifact> GenerateClientTypes(string controllerName, string controllerClassName, IEnumerable<CSharpOperationModel> operations)
{
var model = new HttpApi(controllerClassName, operations, this.openApi.Document, this.openApi.Settings);
this.httpApiList.Add(model);
return [];
}
/// <summary>
/// 生成文件
/// 这里不生成
/// </summary>
/// <param name="clientTypes"></param>
/// <param name="dtoTypes"></param>
/// <param name="outputType"></param>
/// <returns></returns>
protected override string GenerateFile(IEnumerable<CodeArtifact> clientTypes, IEnumerable<CodeArtifact> dtoTypes, ClientGeneratorOutputType outputType)
{
return string.Empty;
}
/// <summary>
/// 创建操作描述
/// 这里创建HttpApiOperation
/// </summary>
/// <param name="operation"></param>
/// <param name="settings"></param>
/// <returns></returns>
protected override CSharpOperationModel CreateOperationModel(OpenApiOperation operation, ClientGeneratorBaseSettings settings)
{
return new HttpApiMethod(operation, (CSharpGeneratorBaseSettings)settings, this, (CSharpTypeResolver)Resolver);
}
}
/// <summary>
/// 表示HttpModel提供者
/// </summary>
private class HttpModelProvider : CSharpGenerator
{
/// <summary>
/// swagger
/// </summary>
private readonly OpenApiDoc swagger;
/// <summary>
/// HttpModel提供者
/// </summary>
/// <param name="swagger"></param>
public HttpModelProvider(OpenApiDoc swagger)
: base(swagger.Document, swagger.Settings.CSharpGeneratorSettings, swagger.resolver)
{
this.swagger = swagger;
}
/// <summary>
/// 获取所有HttpModels
/// </summary>
/// <returns></returns>
public HttpModel[] GetHttpModels()
{
return this.GenerateTypes()
.Select(item => new HttpModel(item, this.swagger.Settings.NameSpace))
.ToArray();
}
}
}
}