-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDocumentGenerator.Schemas.cs
More file actions
288 lines (242 loc) · 7.92 KB
/
DocumentGenerator.Schemas.cs
File metadata and controls
288 lines (242 loc) · 7.92 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* _____ ______
* /_ / ____ ____ ____ _________ / __/ /_
* / / / __ \/ __ \/ __ \/ ___/ __ \/ /_/ __/
* / /__/ /_/ / / / / /_/ /\_ \/ /_/ / __/ /_
* /____/\____/_/ /_/\__ /____/\____/_/ \__/
* /____/
*
* Authors:
* 钟峰(Popeye Zhong) <zongsoft@qq.com>
*
* Copyright (C) 2020-2025 Zongsoft Studio <http://www.zongsoft.com>
*
* This file is part of Zongsoft.Web.OpenApi library.
*
* The Zongsoft.Web.OpenApi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License,
* or (at your option) any later version.
*
* The Zongsoft.Web.OpenApi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the Zongsoft.Web.OpenApi library. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Text.Json.Nodes;
using System.Collections.Generic;
using Microsoft.OpenApi;
using Zongsoft.Common;
namespace Zongsoft.Web.OpenApi;
partial class DocumentGenerator
{
internal static IOpenApiSchema GenerateSchema(DocumentContext context, Type type)
{
var schema = new OpenApiSchema()
{
Type = GetJsonType(type, out var nullable, out var underlyingType),
Format = GetFormat(underlyingType ?? type),
};
if((underlyingType != null && underlyingType.IsEnum) || type.IsEnum)
{
if(TryAdd(context.Document, schema, underlyingType ?? type, out var reference))
SetSchemaEnumeration(schema, underlyingType ?? type);
return reference;
}
if((underlyingType != null && underlyingType.IsScalarType()) || type.IsScalarType())
{
return schema;
}
if(schema.Type == JsonSchemaType.Array)
{
var elementType = TypeExtension.GetElementType(type);
if(elementType.IsDictionaryEntry())
schema.Type = JsonSchemaType.Object;
else
schema.Items = GenerateSchema(context, elementType);
return schema;
}
if(TryAdd(context.Document, schema, underlyingType ?? type, out var result))
SetSchemaObject(context, schema, underlyingType ?? type);
return result;
static bool TryAdd(OpenApiDocument document, OpenApiSchema schema, Type type, out IOpenApiSchema result)
{
if(type.IsDictionary())
{
result = new OpenApiSchema() { Type = JsonSchemaType.Object };
return false;
}
var key = GetSchemaKey(type);
if(document.Components.Schemas.TryGetValue(key, out var value))
{
result = value is OpenApiSchemaReference reference ? reference : new(key);
return false;
}
document.Components.Schemas.TryAdd(key, schema);
result = new OpenApiSchemaReference(key);
return true;
}
}
private static void SetSchemaObject(DocumentContext context, OpenApiSchema schema, Type type)
{
if(schema.Type != JsonSchemaType.Object)
return;
if(type.IsDictionary() || type.IsDictionaryEntry())
return;
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
if(properties.Length > 0)
{
schema.Properties ??= new Dictionary<string, IOpenApiSchema>(properties.Length);
for(int i = 0; i < properties.Length; i++)
{
var property = properties[i];
if(property.CanRead && property.CanWrite && !IsIgnored(property))
schema.Properties[property.Name] = GenerateSchema(context, property.PropertyType);
}
}
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
if(fields.Length > 0)
{
schema.Properties ??= new Dictionary<string, IOpenApiSchema>(fields.Length);
for(int i = 0; i < fields.Length; i++)
{
var field = fields[i];
if(!field.IsInitOnly && !IsIgnored(field))
schema.Properties[field.Name] = GenerateSchema(context, field.FieldType);
}
}
}
private static void SetSchemaEnumeration(OpenApiSchema schema, Type type)
{
var underlyingType = Enum.GetUnderlyingType(type);
schema.Type = JsonSchemaType.Integer;
schema.Format = GetFormat(underlyingType);
var entries = EnumUtility.GetEnumEntries(type, true);
schema.Enum = [.. entries.Select(entry => (JsonNode)Common.Convert.ConvertValue<int>(entry.Value))];
schema.AddExtension("x-enum-varnames", Extensions.Helper.Array(entries.Select(entry => entry.Name)));
schema.AddExtension("x-enum-descriptions", Extensions.Helper.Array(entries.Select(entry => string.IsNullOrEmpty(entry.Description) ? entry.Name : entry.Description)));
}
private static string GetSchemaKey(Type type) => type.FullName;
private static bool IsIgnored(MemberInfo member)
{
if(member.GetCustomAttribute<System.Text.Json.Serialization.JsonIgnoreAttribute>(true) != null)
return true;
var attribute = member.GetCustomAttribute<Zongsoft.Serialization.SerializationMemberAttribute>(true);
return attribute != null && attribute.Ignored;
}
/*
* https://spec.openapis.org/registry/format
*/
private static string GetFormat(Type type)
{
switch(Type.GetTypeCode(type))
{
case TypeCode.String:
return null;
case TypeCode.DateTime:
return "date-time";
case TypeCode.Byte:
return "byte";
case TypeCode.Int16:
return "int16";
case TypeCode.Int32:
return "int32";
case TypeCode.Int64:
return "int64";
case TypeCode.UInt16:
return "uint16";
case TypeCode.UInt32:
return "uint32";
case TypeCode.UInt64:
return "uint64";
case TypeCode.Single:
return "float";
case TypeCode.Double:
return "double";
case TypeCode.Decimal:
return "decimal";
}
if(type == typeof(byte[]))
return "byte";
if(type == typeof(Uri))
return "uri";
if(type == typeof(Guid))
return "uuid";
if(type == typeof(DateTimeOffset))
return "date-time";
if(type == typeof(DateOnly))
return "date";
if(type == typeof(TimeOnly))
return "time";
if(type == typeof(TimeSpan))
return "duration";
return null;
}
private static JsonSchemaType GetJsonType(Type type, out bool nullable, out Type underlyingType)
{
if(type == typeof(string) || type == typeof(StringBuilder) ||
type == typeof(ReadOnlySpan<char>) || type == typeof(Span<char>) ||
type == typeof(ReadOnlyMemory<char>) || type == typeof(Memory<char>))
{
nullable = true;
underlyingType = null;
return JsonSchemaType.String;
}
underlyingType = null;
nullable = type.IsClass || type.IsInterface || type.IsNullable(out underlyingType);
if(type.IsArray || type.IsEnumerable())
return JsonSchemaType.Array;
if(nullable && underlyingType != null)
type = underlyingType;
if(type.IsEnum)
return JsonSchemaType.String;
switch(Type.GetTypeCode(type))
{
case TypeCode.Boolean:
return JsonSchemaType.Boolean;
case TypeCode.Char:
case TypeCode.String:
case TypeCode.DateTime:
return JsonSchemaType.String;
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
return JsonSchemaType.Integer;
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
return JsonSchemaType.Number;
case TypeCode.Empty:
case TypeCode.DBNull:
return JsonSchemaType.Null;
}
if(IsParsable(type))
return JsonSchemaType.String;
var converter = Zongsoft.Common.Convert.GetTypeConverter(type, true);
if(converter != null && converter.CanConvertFrom(typeof(string)))
return JsonSchemaType.String;
return JsonSchemaType.Object;
static bool IsParsable(Type type)
{
var contracts = type.GetInterfaces();
for(int i = 0; i < contracts.Length; i++)
{
if(contracts[i].IsGenericType && contracts[i].GetGenericTypeDefinition() == typeof(IParsable<>))
return true;
}
return false;
}
}
}