summary refs log tree commit diff
path: root/extra/admin-api/Utilities/Spacebar.AdminApi.TestClient/Classes/OpenAPI/OpenAPISchema.cs
blob: 8995dee5fcd04f433ffa4c2c2e96905662b737f0 (plain) (blame)
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using System.Collections.Frozen;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Spacebar.AdminApi.TestClient.Classes.OpenAPI;

public class OpenApiSchema {
    [JsonPropertyName("openapi")]
    public string Version { get; set; } = null!;

    [JsonPropertyName("info")]
    public OpenApiInfo Info { get; set; } = null!;

    [JsonPropertyName("externalDocs")]
    public OpenApiExternalDocs? ExternalDocs { get; set; }

    [JsonPropertyName("paths")]
    public Dictionary<string, OpenApiPath> Paths { get; set; } = null!;

    [JsonPropertyName("servers")]
    public List<OpenApiServer> Servers { get; set; } = null!;

    [JsonPropertyName("components")]
    public OpenApiComponents? Components { get; set; } = null!;

    public class OpenApiComponents {
        [JsonPropertyName("schemas")]
        public Dictionary<string, OpenApiSchemaRef>? Schemas { get; set; } = null!;
    }
}

public class OpenApiServer {
    [JsonPropertyName("url")]
    public string Url { get; set; } = null!;

    [JsonPropertyName("description")]
    public string? Description { get; set; }
}

public class OpenApiPath {
    public FrozenSet<string> GetAvailableMethods() {
        List<string> methods = new();
        if (Get != null) methods.Add("GET");
        if (Post != null) methods.Add("POST");
        if (Put != null) methods.Add("PUT");
        if (Delete != null) methods.Add("DELETE");
        if (Patch != null) methods.Add("PATCH");
        if (Options != null) methods.Add("OPTIONS");
        return methods.ToFrozenSet();
    }

    public bool HasMethod(string method) {
        return method.ToLower() switch {
            "get" => Get != null,
            "post" => Post != null,
            "put" => Put != null,
            "delete" => Delete != null,
            "patch" => Patch != null,
            "options" => Options != null,
            _ => false
        };
    }

    public OpenApiOperation? GetOperation(string method) {
        if (!HasMethod(method)) return null;
        return method.ToLower() switch {
            "get" => Get,
            "post" => Post,
            "put" => Put,
            "delete" => Delete,
            "patch" => Patch,
            "options" => Options,
            _ => null
        };
    }

    [JsonPropertyName("get")]
    public OpenApiOperation? Get { get; set; }

    [JsonPropertyName("post")]
    public OpenApiOperation? Post { get; set; }

    [JsonPropertyName("put")]
    public OpenApiOperation? Put { get; set; }

    [JsonPropertyName("delete")]
    public OpenApiOperation? Delete { get; set; }

    [JsonPropertyName("patch")]
    public OpenApiOperation? Patch { get; set; }

    [JsonPropertyName("options")]
    public OpenApiOperation? Options { get; set; }

    public class OpenApiOperation {
        [JsonPropertyName("description")]
        public string Description { get; set; } = null!;

        [JsonPropertyName("parameters")]
        public List<OpenApiParameter>? Parameters { get; set; }

        [JsonPropertyName("requestBody")]
        public OpenApiRequestBody? RequestBody { get; set; }

        public class OpenApiParameter {
            [JsonPropertyName("name")]
            public string Name { get; set; } = null!;

            [JsonPropertyName("in")]
            public string In { get; set; } = null!;

            [JsonPropertyName("required")]
            public bool Required { get; set; }

            [JsonPropertyName("schema")]
            public OpenApiSchemaRef Schema { get; set; } = null!;

            [JsonPropertyName("description")]
            public string? Description { get; set; }
        }
    }
}

public class OpenApiExternalDocs {
    [JsonPropertyName("description")]
    public string Description { get; set; } = null!;

    [JsonPropertyName("url")]
    public string Url { get; set; } = null!;
}

public class OpenApiInfo {
    [JsonPropertyName("title")]
    public string Title { get; set; } = null!;

    [JsonPropertyName("version")]
    public string Version { get; set; } = null!;

    [JsonPropertyName("description")]
    public string Description { get; set; } = null!;

    [JsonPropertyName("license")]
    public OpenApiLicense License { get; set; } = null!;

    public class OpenApiLicense {
        [JsonPropertyName("name")]
        public string Name { get; set; } = null!;

        [JsonPropertyName("url")]
        public string Url { get; set; } = null!;
    }
}

public class OpenApiRequestBody {
    [JsonPropertyName("content")]
    public OpenApiContent Content { get; set; } = null!;

    [JsonPropertyName("required")]
    public bool Required { get; set; }
}

public class OpenApiContent {
    [JsonPropertyName("application/json")]
    public OpenApiSchemaContainer? ApplicationJson { get; set; }

    public class OpenApiSchemaContainer {
        [JsonPropertyName("schema")]
        public OpenApiSchemaRef Schema { get; set; } = null!;
    }
}

[JsonConverter(typeof(OpenApiSchemaRefConverter))]
public class OpenApiSchemaRef {
    public string? Description { get; set; }
    public string? Type { get; set; } = null!;
    public List<string>? Types { get; set; } = null!;
    public string? Ref { get; set; } = null!;
    public Dictionary<string, OpenApiSchemaRef>? Properties { get; set; }
    public List<string>? Required { get; set; }
    public int? MinLength { get; set; }
    public int? MaxLength { get; set; }
    public int? MinItems { get; set; }
    public int? MaxItems { get; set; }
    public object? Constant { get; set; }
    public object? Default { get; set; }
    public bool Nullable { get; set; }
    public List<OpenApiSchemaRef>? AnyOf { get; set; }
    public List<object>? Enum { get; set; }
    public string? Format { get; set; }

    public OpenApiSchemaRef? GetReferencedSchema(OpenApiSchema schema) {
        if (Ref == null) return null;
        string refKey = Ref.Replace("#/components/schemas/", "");
        if (schema.Components?.Schemas != null && schema.Components.Schemas.TryGetValue(refKey, out var referencedSchema)) {
            return referencedSchema;
        }

        throw new KeyNotFoundException($"Referenced schema '{refKey}' not found.");
    }
}

public class OpenApiSchemaRefConverter : JsonConverter<OpenApiSchemaRef> {
    public override OpenApiSchemaRef? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
        if (reader.TokenType != JsonTokenType.StartObject) {
            throw new JsonException("Expected StartObject token");
        }

        using var jsonDoc = JsonDocument.ParseValue(ref reader);
        var jsonObject = jsonDoc.RootElement;
        var schemaRef = new OpenApiSchemaRef();

        foreach (var property in jsonObject.EnumerateObject()) {
            switch (property.Name) {
                case "type":
                    if (property.Value.ValueKind == JsonValueKind.String) {
                        schemaRef.Type = property.Value.GetString();
                    }
                    else if (property.Value.ValueKind == JsonValueKind.Array) {
                        var types = new List<string>();
                        foreach (var item in property.Value.EnumerateArray()) {
                            if (item.ValueKind == JsonValueKind.String) {
                                types.Add(item.GetString()!);
                            }
                            else throw new JsonException("Expected string in type array");
                        }

                        schemaRef.Types = types;
                    }

                    break;
                case "$ref":
                    schemaRef.Ref = property.Value.GetString();
                    break;
                case "description":
                    schemaRef.Description = property.Value.GetString();
                    break;
                case "properties":
                    schemaRef.Properties = property.Value.EnumerateObject().ToDictionary(x => x.Name, x => x.Value.Deserialize<OpenApiSchemaRef>(options)!);
                    break;
                case "required":
                    schemaRef.Required = property.Value.EnumerateArray().Select(item => item.GetString()!).ToList();
                    break;
                case "minLength":
                    schemaRef.MinLength = property.Value.GetInt32();
                    break;
                case "maxLength":
                    schemaRef.MaxLength = property.Value.GetInt32();
                    break;
                case "minItems":
                    schemaRef.MinItems = property.Value.GetInt32();
                    break;
                case "maxItems":
                    schemaRef.MaxItems = property.Value.GetInt32();
                    break;
                case "const":
                    if (property.Value.ValueKind == JsonValueKind.String)
                        schemaRef.Constant = property.Value.GetString();
                    else if (property.Value.ValueKind == JsonValueKind.Number)
                        schemaRef.Constant = property.Value.GetInt32();
                    else if (property.Value.ValueKind is JsonValueKind.True or JsonValueKind.False)
                        schemaRef.Constant = property.Value.GetBoolean();
                    else throw new JsonException($"Expected string|int|bool in const, got {property.Value.ValueKind}");
                    break;
                case "default":
                    if (property.Value.ValueKind == JsonValueKind.String)
                        schemaRef.Default = property.Value.GetString();
                    else if (property.Value.ValueKind == JsonValueKind.Number)
                        schemaRef.Default = property.Value.GetInt32();
                    else if (property.Value.ValueKind is JsonValueKind.True or JsonValueKind.False)
                        schemaRef.Default = property.Value.GetBoolean();
                    else if (property.Value.ValueKind == JsonValueKind.Null)
                        schemaRef.Default = null;
                    else if (property.Value.ValueKind == JsonValueKind.Array)
                        if (property.Value.GetArrayLength() > 0) throw new JsonException("Expected empty array in default");
                        else schemaRef.Default = Array.Empty<object>();
                    else throw new JsonException($"Expected string|int|bool|null in default, got {property.Value.ValueKind}");
                    break;
                case "enum":
                    var enumValues = new List<object>();
                    foreach (var item in property.Value.EnumerateArray()) {
                        if (item.ValueKind == JsonValueKind.String)
                            enumValues.Add(item.GetString()!);
                        else if (item.ValueKind == JsonValueKind.Number)
                            enumValues.Add(item.GetInt32());
                        else if (item.ValueKind is JsonValueKind.True or JsonValueKind.False)
                            enumValues.Add(item.GetBoolean());
                        else if (item.ValueKind == JsonValueKind.Null)
                            enumValues.Add(null!);
                        else throw new JsonException($"Expected string|int|bool|null in enum, got {item.ValueKind}");
                    }

                    schemaRef.Enum = enumValues;
                    break;
                case "nullable":
                    schemaRef.Nullable = property.Value.GetBoolean();
                    break;
                case "anyOf":
                    schemaRef.AnyOf = property.Value.EnumerateArray().Select(item => item.Deserialize<OpenApiSchemaRef>(options)!).ToList();
                    break;
                case "format":
                    schemaRef.Format = property.Value.GetString();
                    break;
                case "additionalProperties": //TODO
                case "patternProperties": // Side effect of using JsonValue in typescript
                    break;
                default:
                    Console.WriteLine($"Got unexpected prop {property.Name} in OpenApiSchemaRef!");
                    break;
            }
        }

        return schemaRef;
    }

    public override void Write(Utf8JsonWriter writer, OpenApiSchemaRef value, JsonSerializerOptions options) {
        // throw new NotImplementedException("Serialization not implemented for OpenApiSchemaRef");
        writer.WriteStartObject();
        if (value.Type != null) {
            writer.WriteString("type", value.Type);
        }
        else if (value.Types != null) {
            writer.WritePropertyName("type");
            writer.WriteStartArray();
            foreach (var type in value.Types) {
                writer.WriteStringValue(type);
            }

            writer.WriteEndArray();
        }

        if (value.Ref != null) {
            writer.WriteString("$ref", value.Ref);
        }

        if (value.Description != null) {
            writer.WriteString("description", value.Description);
        }

        if (value.Properties != null) {
            writer.WritePropertyName("properties");
            writer.WriteStartObject();
            foreach (var prop in value.Properties) {
                writer.WritePropertyName(prop.Key);
                JsonSerializer.Serialize(writer, prop.Value, options);
            }

            writer.WriteEndObject();
        }

        writer.WriteEndObject();
    }
}