-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSchemaMapper.cs
More file actions
91 lines (81 loc) · 2.41 KB
/
Copy pathSchemaMapper.cs
File metadata and controls
91 lines (81 loc) · 2.41 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
using System.Text.Json.Nodes;
namespace NpgsqlRest.Common;
/// <summary>
/// Maps a PostgreSQL <see cref="TypeDescriptor"/> to a JSON Schema fragment (type + format). Shared
/// by the OpenApi and Mcp plugins via linked source (internal, own copy per assembly). AOT-safe —
/// builds System.Text.Json.Nodes only.
/// </summary>
internal static class SchemaMapper
{
public static JsonObject GetSchemaForType(TypeDescriptor type)
{
var schema = new JsonObject();
if (type.IsArray)
{
schema["type"] = "array";
var itemType = new TypeDescriptor(type.Type, type.HasDefault);
schema["items"] = GetSchemaForType(itemType);
return schema;
}
if (type.IsNumeric)
{
if (type.Type.Contains("int", StringComparison.OrdinalIgnoreCase))
{
schema["type"] = "integer";
if (type.Type.Contains("big", StringComparison.OrdinalIgnoreCase) ||
type.Type == "int8")
{
schema["format"] = "int64";
}
else
{
schema["format"] = "int32";
}
}
else
{
schema["type"] = "number";
if (type.Type == "real" || type.Type == "float4")
{
schema["format"] = "float";
}
else if (type.Type == "double precision" || type.Type == "float8")
{
schema["format"] = "double";
}
}
return schema;
}
if (type.IsBoolean)
{
schema["type"] = "boolean";
return schema;
}
if (type.IsDateTime)
{
schema["type"] = "string";
schema["format"] = "date-time";
return schema;
}
if (type.IsDate)
{
schema["type"] = "string";
schema["format"] = "date";
return schema;
}
if (type.Type == "uuid")
{
schema["type"] = "string";
schema["format"] = "uuid";
return schema;
}
if (type.IsJson)
{
schema["type"] = "object";
return schema;
}
// Default to string
schema["type"] = "string";
return schema;
}
}