-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathHelper.OpenApiFactory.cs
124 lines (97 loc) · 3.52 KB
/
Helper.OpenApiFactory.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
/*
Source is mostly: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/OpenApiAnyFactory.cs
*/
using System.Text.Json;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
public class OpenApiFactory
{
public static OpenApiHeader CreateHeaderInt(string? description = null) => new()
{
Description = description,
Schema = CreateSchemaRef("GenericInt")
};
public static OpenApiHeader CreateHeaderString(string? description = null) => new()
{
Description = description,
Schema = CreateSchemaRef("GenericString")
};
public static OpenApiHeader CreateHeaderRef(string headerId, string? description = null) => new()
{
Description = description,
Reference = new()
{
Type = ReferenceType.Header,
Id = headerId
}
};
public static OpenApiSchema CreateSchemaRef(string schemaId) => new()
{
Reference = new()
{
Type = ReferenceType.Schema,
Id = schemaId
}
};
public static OpenApiSecurityScheme CreateSecuritySchemaRef(string schemaId) => new()
{
Reference = new()
{
Type = ReferenceType.SecurityScheme,
Id = schemaId
}
};
public static IOpenApiAny? CreateFromJson(string json)
{
try
{
var jsonElement = JsonSerializer.Deserialize<JsonElement>(json);
return CreateFromJsonElement(jsonElement);
}
catch { }
return null;
}
static IOpenApiAny CreateOpenApiArray(JsonElement jsonElement)
{
OpenApiArray openApiArray = [];
foreach (var item in jsonElement.EnumerateArray())
{
openApiArray.Add(CreateFromJsonElement(item));
}
return openApiArray;
}
static IOpenApiAny CreateOpenApiObject(JsonElement jsonElement)
{
OpenApiObject openApiObject = [];
foreach (var property in jsonElement.EnumerateObject())
{
openApiObject.Add(property.Name, CreateFromJsonElement(property.Value));
}
return openApiObject;
}
static IOpenApiAny CreateFromJsonElement(JsonElement jsonElement)
{
if (jsonElement.ValueKind == JsonValueKind.Null)
return new OpenApiNull();
if (jsonElement.ValueKind == JsonValueKind.True || jsonElement.ValueKind == JsonValueKind.False)
return new OpenApiBoolean(jsonElement.GetBoolean());
if (jsonElement.ValueKind == JsonValueKind.Number)
{
if (jsonElement.TryGetInt32(out int intValue))
return new OpenApiInteger(intValue);
if (jsonElement.TryGetInt64(out long longValue))
return new OpenApiLong(longValue);
if (jsonElement.TryGetSingle(out float floatValue) && !float.IsInfinity(floatValue))
return new OpenApiFloat(floatValue);
if (jsonElement.TryGetDouble(out double doubleValue))
return new OpenApiDouble(doubleValue);
}
if (jsonElement.ValueKind == JsonValueKind.String)
return new OpenApiString(jsonElement.ToString());
if (jsonElement.ValueKind == JsonValueKind.Array)
return CreateOpenApiArray(jsonElement);
if (jsonElement.ValueKind == JsonValueKind.Object)
return CreateOpenApiObject(jsonElement);
throw new System.ArgumentException($"Unsupported value kind {jsonElement.ValueKind}");
}
}