-
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathOpenApiSchemaExtensions.cs
More file actions
48 lines (37 loc) · 1.33 KB
/
OpenApiSchemaExtensions.cs
File metadata and controls
48 lines (37 loc) · 1.33 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
using Microsoft.OpenApi.Models;
namespace JsonApiDotNetCore.OpenApi.Swashbuckle;
internal static class OpenApiSchemaExtensions
{
public static void ReorderProperties(this OpenApiSchema fullSchema, IEnumerable<string> propertyNamesInOrder)
{
ArgumentNullException.ThrowIfNull(fullSchema);
ArgumentNullException.ThrowIfNull(propertyNamesInOrder);
var propertiesInOrder = new Dictionary<string, OpenApiSchema>();
foreach (string propertyName in propertyNamesInOrder)
{
if (fullSchema.Properties.TryGetValue(propertyName, out OpenApiSchema? schema))
{
propertiesInOrder.Add(propertyName, schema);
}
}
ConsistencyGuard.ThrowIf(fullSchema.Properties.Count != propertiesInOrder.Count);
fullSchema.Properties = propertiesInOrder;
}
public static OpenApiSchema WrapInExtendedSchema(this OpenApiSchema source)
{
ArgumentNullException.ThrowIfNull(source);
return new OpenApiSchema
{
AllOf = [source]
};
}
public static OpenApiSchema UnwrapLastExtendedSchema(this OpenApiSchema source)
{
ArgumentNullException.ThrowIfNull(source);
if (source.AllOf is { Count: > 0 })
{
return source.AllOf.Last();
}
return source;
}
}