-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSharpInlineSchemas.cs
More file actions
229 lines (211 loc) · 10.4 KB
/
CSharpInlineSchemas.cs
File metadata and controls
229 lines (211 loc) · 10.4 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Json.Pointer;
using DarkPatterns.OpenApi.Transformations;
using DarkPatterns.OpenApi.Abstractions;
using DarkPatterns.Json.Specifications;
using DarkPatterns.Json.Documents;
using DarkPatterns.Json.Diagnostics;
using DarkPatterns.Json.Specifications.Keywords.Draft2020_12Validation;
using System.Diagnostics.CodeAnalysis;
namespace DarkPatterns.OpenApi.CSharp;
using v3_0 = DarkPatterns.OpenApi.Specifications.v3_0;
public class CSharpInlineSchemas(CSharpSchemaOptions options, DocumentRegistry documentRegistry)
{
public static readonly CSharpInlineDefinition AnyObject = new("object", Nullable: true);
[return: System.Diagnostics.CodeAnalysis.NotNullIfNotNull(nameof(schema))]
public CSharpInlineDefinition? ToInlineDataType(JsonSchema? schema)
{
if (schema == null) return null;
return ToInlineDataType(schema.ResolveSchemaInfo());
}
[return: NotNullIfNotNull(nameof(schema))]
public CSharpInlineDefinition? ToInlineDataType(JsonSchemaInfo? schema)
{
if (schema == null) return null;
schema = schema with { Original = schema.EffectiveSchema.Metadata };
var schemaInfo = CSharpTypeInfo.From(schema);
CSharpInlineDefinition result = schemaInfo switch
{
{ Info.EffectiveSchema.BoolValue: false } => new(options.Find(TypeAnnotation.Common.Object, "never")),
{ Info.EffectiveSchema.BoolValue: true } => new(options.Find(TypeAnnotation.Common.Object, "any")),
{ Info.Annotations.Count: 0 } => new(options.Find(TypeAnnotation.Common.Object, "any")),
// Dictionary
{ TypeAnnotation: { AllowsObject: true }, Properties: { Count: 0 }, AdditionalProperties: JsonSchema dictionaryValueSchema } =>
new(options.ToMapType(ToInlineDataType(dictionaryValueSchema).Text), IsEnumerable: true),
// Array
{ TypeAnnotation: { AllowsArray: true }, Items: null } =>
new(options.ToArrayType(options.FallbackType), IsEnumerable: true),
{ Items: JsonSchema items } =>
new(options.ToArrayType(ToInlineDataType(items).Text), IsEnumerable: true),
// Generates a source file, so therefore it must have a class name
_ when ProduceSourceEntry(schema) =>
new($"global::{options.GetNamespace(schema)}.{GetClassName(schema)}"),
// Specifically-mapped type
{ TypeAnnotation: v3_0.TypeKeyword { OpenApiType: var primitiveType }, Format: var format } =>
new(options.Find(TypeAnnotation.ToPrimitiveTypeString(primitiveType), format)),
{ TypeAnnotation.AllowsNumber: true, Format: var format } =>
new(options.Find(TypeAnnotation.Common.Number, format)),
{ TypeAnnotation.AllowsInteger: true, Format: var format } =>
new(options.Find(TypeAnnotation.Common.Integer, format)),
{ TypeAnnotation.AllowsString: true, Format: var format } =>
new(options.Find(TypeAnnotation.Common.String, format)),
{ TypeAnnotation.AllowsBoolean: true, Format: var format } =>
new(options.Find(TypeAnnotation.Common.Boolean, format)),
{ TypeAnnotation.AllowsObject: true, Format: var format } =>
new(options.Find(TypeAnnotation.Common.Object, format)),
// TODO: additional reference?
_ => throw new DiagnosticException(UnableToCreateInlineSchemaDiagnostic.Builder(schema.Original.Id)),
};
return schema.TryGetAnnotation<v3_0.NullableKeyword>() is { IsNullable: true }
? result.MakeNullable()
: result;
}
public bool ProduceSourceEntry(JsonSchema schema)
{
return ProduceSourceEntry(schema.ResolveSchemaInfo());
}
public bool ProduceSourceEntry(JsonSchemaInfo schema)
{
if (schema.Original.Id.OriginalString != schema.EffectiveSchema.Metadata.Id.OriginalString) return false;
var nodes = documentRegistry.GetNodesTo(schema.EffectiveSchema.Metadata.Id);
if (nodes.LastOrDefault() is (["allOf", _], JsonSchema))
return false;
// C# can't inline things that must be referenced, and vice versa.
// (Except with tuples, but those don't serialize/deserialize reliably yet.)
return CSharpTypeInfo.From(schema) switch
{
{ TypeAnnotation: { AllowsObject: true }, Properties.Count: 0, AdditionalProperties: JsonSchema } => false,
{ AllOf.Count: > 0 } => true,
{ AnyOf.Count: > 0 } => true,
{ OneOf.Count: > 0 } => true,
{ TypeAnnotation: { AllowsString: true }, Enum.Count: > 0 } => true,
{ TypeAnnotation: { AllowsArray: true } } or { Items: JsonSchema } => false,
{ TypeAnnotation: { AllowsObject: true }, Format: null } => true,
{ Properties.Count: > 0 } => true,
{ TypeAnnotation: { AllowsString: true } or { AllowsNumber: true } or { AllowsInteger: true } or { AllowsBoolean: true } } => false,
{ } => false,
_ => throw new NotSupportedException("Unknown schema"),
};
}
private string GetClassName(JsonSchemaInfo schema)
{
return options.ToClassName(schema, UriToClassIdentifier(schema.EffectiveSchema.Metadata.Id));
}
private static readonly Regex HttpSuccessRegex = new Regex("2[0-9]{2}");
private static bool Is2xx(int statusCode) => statusCode is >= 200 and < 300;
public string UriToClassIdentifier(Uri uri)
{
IReadOnlyList<JsonDocumentNodeContext> remaining = documentRegistry.GetNodesTo(uri);
if (remaining.Count == 0)
return string.Join(" ", JsonPointer.Parse(uri.Fragment).Segments.Select(s => s.Value));
IEnumerable<string> parts = Enumerable.Empty<string>();
while (remaining.Count > 0)
{
(var newParts, remaining) = Simplify(remaining);
parts = parts.Concat(newParts).ToArray();
}
return string.Join(" ", parts);
(IEnumerable<string> parts, IReadOnlyList<JsonDocumentNodeContext> remaining) Simplify(IReadOnlyList<JsonDocumentNodeContext> context)
{
switch (context[0])
{
case (["paths", var path], OpenApiPath) when context.Count >= 2:
switch (context[1])
{
case ([var method], OpenApiOperation { OperationId: null }):
return ([$"{method} ${path}"], context.Skip(2).ToArray());
case (_, OpenApiOperation { OperationId: string opId }):
return ([opId], context.Skip(2).ToArray());
default:
throw new NotImplementedException();
}
case ([], OpenApiDocument) when context.Count >= 2 && context[1] is (["components", _, string componentName], JsonSchema):
return ([componentName], context.Skip(2).ToArray());
case ([], OpenApiDocument):
return (Enumerable.Empty<string>(), context.Skip(1).ToArray());
case (["callbacks", var callbackName, _], OpenApiPath):
switch (context[1])
{
case ([var method], OpenApiOperation { OperationId: null }):
return ([$"{method} {callbackName}"], context.Skip(2).ToArray());
case (_, OpenApiOperation { OperationId: string opId }):
return ([opId], context.Skip(2).ToArray());
default:
throw new NotImplementedException();
}
case (_, OpenApiPath):
return (Enumerable.Empty<string>(), context.Skip(1).ToArray());
case (["responses"], OpenApiResponses responses) when context.Count >= 4:
{
if (context[1] is not ([var statusCode], OpenApiResponse response)) throw new NotImplementedException();
if (context[3] is not (["schema"], _)) throw new NotImplementedException();
var responseName = statusCode switch
{
"default" when responses.StatusCodeResponses.Count == 0 => "",
"default" => "other",
_ when responses.StatusCodeResponses.Count == 1 && responses.Default == null
=> "",
_ when HttpSuccessRegex.IsMatch(statusCode) && responses.StatusCodeResponses.Keys.Count(Is2xx) == 1
=> "",
_ when int.TryParse(statusCode, out var numeric) && HttpStatusCodes.StatusCodeNames.TryGetValue(numeric, out var statusCodeName)
=> statusCodeName,
_ => statusCode,
};
var (qualifierName, typeName) = context[2] switch
{
(["content", _], _) when response.Content!.Count == 1 => ("", "response"),
(["content", var mimeType], _) => (mimeType, "response"),
(["headers", var headerNam], _) => (headerNam, "header"),
_ => throw new NotImplementedException()
};
return ([responseName, qualifierName, typeName], context.Skip(4).ToArray());
}
case (["components", "responses", var responseName], OpenApiResponse response) when context.Count >= 3:
{
if (context[2] is not (["schema"], _)) throw new NotImplementedException();
var (qualifierName, typeName) = context[1] switch
{
(["content", _], _) when response.Content!.Count == 1 => ("", "response"),
(["content", var mimeType], _) => (mimeType, "response"),
(["headers", var headerNam], _) => (headerNam, "header"),
_ => throw new NotImplementedException()
};
return ([responseName, qualifierName, typeName], context.Skip(3).ToArray());
}
case (["components", "requestBodies", var requestName], OpenApiRequestBody requestBody) when context.Count >= 3:
{
if (context[1] is not (["content", var mimeType], _)) throw new NotImplementedException();
if (context[2] is not (["schema"], _)) throw new NotImplementedException();
return ([requestName, requestBody.Content!.Count == 1 ? "" : mimeType, "request"], context.Skip(3).ToArray());
}
case (["components", "parameters", var paramName], OpenApiParameter) when context.Count >= 1:
if (context[1] is not (["schema"], _)) throw new NotImplementedException();
return ([paramName], context.Skip(2).ToArray());
case (["requestBody"], OpenApiRequestBody requestBody) when context.Count >= 3:
{
if (context[1] is not (["content", var mimeType], _)) throw new NotImplementedException();
if (context[2] is not (["schema"], _)) throw new NotImplementedException();
return ([requestBody.Content!.Count == 1 ? "" : mimeType, "request"], context.Skip(3).ToArray());
}
case (["parameters", _], OpenApiParameter { Name: string paramName }) when context.Count >= 1:
if (context[1] is not (["schema"], _)) throw new NotImplementedException();
return ([paramName], context.Skip(2).ToArray());
case (["items"], JsonSchema):
return (["Item"], context.Skip(1).ToArray());
case (["properties", var propName], JsonSchema):
return ([propName], context.Skip(1).ToArray());
case (["additionalProperties"], JsonSchema):
return (["AdditionalProperty"], context.Skip(1).ToArray());
case (var parts, JsonSchema):
return (parts, context.Skip(1).ToArray());
case (var parts, var t):
throw new NotImplementedException($"{string.Join(", ", parts)} {t.GetType().FullName}");
default:
throw new NotImplementedException();
};
}
}
}