-
Notifications
You must be signed in to change notification settings - Fork 14
/
Open_API_specs.cs
87 lines (80 loc) · 3.04 KB
/
Open_API_specs.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
namespace Open_API_specs;
public class Open_API_data_type
{
[Test]
public void Resolved_by_base_if_not_decorated()
=> OpenApiDataType.FromType(typeof(ForUuid))
.Should().Be(new OpenApiDataType(
dataType: typeof(CustomUuid),
description: "UUID based identifier",
type: "string",
example: "lmZO_haEOTCwGsCcbIZFFg",
format: "uuid-base64",
nullable: true));
[Test]
public void Identifier_behavior_is_resolved_as_Id_type()
=> OpenApiDataType.FromType(typeof(DecoratedId))
.Should().Be(new OpenApiDataType(
dataType: typeof(Id<DecoratedId>),
description: "Custom description",
type: "string",
example: "custom example",
format: "custom-uuid"));
[Test]
public void has_custom_debug_display()
=> new OpenApiDataType(
dataType: typeof(Date),
description: "Custom description",
type: "string",
example: "custom example",
format: "custom-uuid",
pattern: "[0-9]{4}-[0-9]{2}-[0-9]{2}")
.Should().HaveDebuggerDisplay("{ type: string, desc: Custom description, example: custom example, format: custom-uuid, pattern: [0-9]{4}-[0-9]{2}-[0-9]{2} }");
[OpenApiDataType(description: "Custom description", example: "custom example", type: "string", format: "custom-uuid")]
internal sealed class DecoratedId : UuidBehavior { }
}
#if NET6_0_OR_GREATER
public class README_md
{
[Test]
public void Describes()
{
var all = OpenApiDataTypes.FromAssemblies(typeof(Date).Assembly)
.OrderBy(attr => attr.DataType.Namespace)
.ThenBy(attr => attr.DataType.Name)
.ToDictionary(
info => $"{info.DataType.Namespace}.{info.DataType.Name}".Replace("Qowaiv.", "", StringComparison.InvariantCulture),
info => new OpenApiDataTypeInfo
{
description = info.Description,
example = info.Example,
type = info.Type,
format = info.Format,
pattern = info.Pattern,
nullable = info.Nullable,
@enum = info.Enum?.ToArray(),
});
#if DEBUG
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(all, Options));
#endif
all.Should().NotBeEmpty();
}
private sealed record OpenApiDataTypeInfo
{
public string? description { get; init; }
public object? example { get; init; }
public string? type { get; init; }
public string? format { get; init; }
public string? pattern { get; init; }
public bool nullable { get; init; }
public string[]? @enum { get; init; } = [];
}
#if DEBUG
private static readonly System.Text.Json.JsonSerializerOptions Options = new()
{
WriteIndented = true,
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
};
#endif
}
#endif