|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Text.Json.Nodes; |
| 7 | + |
| 8 | +namespace System.Text.Json.Schema |
| 9 | +{ |
| 10 | + internal sealed class JsonSchema |
| 11 | + { |
| 12 | + internal const string RefPropertyName = "$ref"; |
| 13 | + internal const string CommentPropertyName = "$comment"; |
| 14 | + internal const string TypePropertyName = "type"; |
| 15 | + internal const string FormatPropertyName = "format"; |
| 16 | + internal const string PatternPropertyName = "pattern"; |
| 17 | + internal const string PropertiesPropertyName = "properties"; |
| 18 | + internal const string RequiredPropertyName = "required"; |
| 19 | + internal const string ItemsPropertyName = "items"; |
| 20 | + internal const string AdditionalPropertiesPropertyName = "additionalProperties"; |
| 21 | + internal const string EnumPropertyName = "enum"; |
| 22 | + internal const string NotPropertyName = "not"; |
| 23 | + internal const string AnyOfPropertyName = "anyOf"; |
| 24 | + internal const string ConstPropertyName = "const"; |
| 25 | + internal const string DefaultPropertyName = "default"; |
| 26 | + internal const string MinLengthPropertyName = "minLength"; |
| 27 | + internal const string MaxLengthPropertyName = "maxLength"; |
| 28 | + |
| 29 | + public static JsonSchema False { get; } = new(false); |
| 30 | + public static JsonSchema True { get; } = new(true); |
| 31 | + |
| 32 | + public JsonSchema() { } |
| 33 | + private JsonSchema(bool trueOrFalse) { _trueOrFalse = trueOrFalse; } |
| 34 | + |
| 35 | + public bool IsTrue => _trueOrFalse is true; |
| 36 | + public bool IsFalse => _trueOrFalse is false; |
| 37 | + private readonly bool? _trueOrFalse; |
| 38 | + |
| 39 | + public string? Ref { get => _ref; set { VerifyMutable(); _ref = value; } } |
| 40 | + private string? _ref; |
| 41 | + |
| 42 | + public string? Comment { get => _comment; set { VerifyMutable(); _comment = value; } } |
| 43 | + private string? _comment; |
| 44 | + |
| 45 | + public JsonSchemaType Type { get => _type; set { VerifyMutable(); _type = value; } } |
| 46 | + private JsonSchemaType _type = JsonSchemaType.Any; |
| 47 | + |
| 48 | + public string? Format { get => _format; set { VerifyMutable(); _format = value; } } |
| 49 | + private string? _format; |
| 50 | + |
| 51 | + public string? Pattern { get => _pattern; set { VerifyMutable(); _pattern = value; } } |
| 52 | + private string? _pattern; |
| 53 | + |
| 54 | + public JsonNode? Constant { get => _constant; set { VerifyMutable(); _constant = value; } } |
| 55 | + private JsonNode? _constant; |
| 56 | + |
| 57 | + public List<KeyValuePair<string, JsonSchema>>? Properties { get => _properties; set { VerifyMutable(); _properties = value; } } |
| 58 | + private List<KeyValuePair<string, JsonSchema>>? _properties; |
| 59 | + |
| 60 | + public List<string>? Required { get => _required; set { VerifyMutable(); _required = value; } } |
| 61 | + private List<string>? _required; |
| 62 | + |
| 63 | + public JsonSchema? Items { get => _items; set { VerifyMutable(); _items = value; } } |
| 64 | + private JsonSchema? _items; |
| 65 | + |
| 66 | + public JsonSchema? AdditionalProperties { get => _additionalProperties; set { VerifyMutable(); _additionalProperties = value; } } |
| 67 | + private JsonSchema? _additionalProperties; |
| 68 | + |
| 69 | + public JsonArray? Enum { get => _enum; set { VerifyMutable(); _enum = value; } } |
| 70 | + private JsonArray? _enum; |
| 71 | + |
| 72 | + public JsonSchema? Not { get => _not; set { VerifyMutable(); _not = value; } } |
| 73 | + private JsonSchema? _not; |
| 74 | + |
| 75 | + public List<JsonSchema>? AnyOf { get => _anyOf; set { VerifyMutable(); _anyOf = value; } } |
| 76 | + private List<JsonSchema>? _anyOf; |
| 77 | + |
| 78 | + public bool HasDefaultValue { get => _hasDefaultValue; set { VerifyMutable(); _hasDefaultValue = value; } } |
| 79 | + private bool _hasDefaultValue; |
| 80 | + |
| 81 | + public JsonNode? DefaultValue { get => _defaultValue; set { VerifyMutable(); _defaultValue = value; } } |
| 82 | + private JsonNode? _defaultValue; |
| 83 | + |
| 84 | + public int? MinLength { get => _minLength; set { VerifyMutable(); _minLength = value; } } |
| 85 | + private int? _minLength; |
| 86 | + |
| 87 | + public int? MaxLength { get => _maxLength; set { VerifyMutable(); _maxLength = value; } } |
| 88 | + private int? _maxLength; |
| 89 | + |
| 90 | + public JsonSchemaExporterContext? ExporterContext { get; set; } |
| 91 | + |
| 92 | + public int KeywordCount |
| 93 | + { |
| 94 | + get |
| 95 | + { |
| 96 | + if (_trueOrFalse != null) |
| 97 | + { |
| 98 | + return 0; |
| 99 | + } |
| 100 | + |
| 101 | + int count = 0; |
| 102 | + Count(Ref != null); |
| 103 | + Count(Comment != null); |
| 104 | + Count(Type != JsonSchemaType.Any); |
| 105 | + Count(Format != null); |
| 106 | + Count(Pattern != null); |
| 107 | + Count(Constant != null); |
| 108 | + Count(Properties != null); |
| 109 | + Count(Required != null); |
| 110 | + Count(Items != null); |
| 111 | + Count(AdditionalProperties != null); |
| 112 | + Count(Enum != null); |
| 113 | + Count(Not != null); |
| 114 | + Count(AnyOf != null); |
| 115 | + Count(HasDefaultValue); |
| 116 | + Count(MinLength != null); |
| 117 | + Count(MaxLength != null); |
| 118 | + |
| 119 | + return count; |
| 120 | + |
| 121 | + void Count(bool isKeywordSpecified) |
| 122 | + { |
| 123 | + count += isKeywordSpecified ? 1 : 0; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public void MakeNullable() |
| 129 | + { |
| 130 | + if (_trueOrFalse != null) |
| 131 | + { |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + if (Type != JsonSchemaType.Any) |
| 136 | + { |
| 137 | + Type |= JsonSchemaType.Null; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + public JsonNode ToJsonNode(JsonSchemaExporterOptions options) |
| 142 | + { |
| 143 | + if (_trueOrFalse is { } boolSchema) |
| 144 | + { |
| 145 | + return CompleteSchema((JsonNode)boolSchema); |
| 146 | + } |
| 147 | + |
| 148 | + var objSchema = new JsonObject(); |
| 149 | + |
| 150 | + if (Ref != null) |
| 151 | + { |
| 152 | + objSchema.Add(RefPropertyName, Ref); |
| 153 | + } |
| 154 | + |
| 155 | + if (Comment != null) |
| 156 | + { |
| 157 | + objSchema.Add(CommentPropertyName, Comment); |
| 158 | + } |
| 159 | + |
| 160 | + if (MapSchemaType(Type) is JsonNode type) |
| 161 | + { |
| 162 | + objSchema.Add(TypePropertyName, type); |
| 163 | + } |
| 164 | + |
| 165 | + if (Format != null) |
| 166 | + { |
| 167 | + objSchema.Add(FormatPropertyName, Format); |
| 168 | + } |
| 169 | + |
| 170 | + if (Pattern != null) |
| 171 | + { |
| 172 | + objSchema.Add(PatternPropertyName, Pattern); |
| 173 | + } |
| 174 | + |
| 175 | + if (Constant != null) |
| 176 | + { |
| 177 | + objSchema.Add(ConstPropertyName, Constant); |
| 178 | + } |
| 179 | + |
| 180 | + if (Properties != null) |
| 181 | + { |
| 182 | + var properties = new JsonObject(); |
| 183 | + foreach (KeyValuePair<string, JsonSchema> property in Properties) |
| 184 | + { |
| 185 | + properties.Add(property.Key, property.Value.ToJsonNode(options)); |
| 186 | + } |
| 187 | + |
| 188 | + objSchema.Add(PropertiesPropertyName, properties); |
| 189 | + } |
| 190 | + |
| 191 | + if (Required != null) |
| 192 | + { |
| 193 | + var requiredArray = new JsonArray(); |
| 194 | + foreach (string requiredProperty in Required) |
| 195 | + { |
| 196 | + requiredArray.Add((JsonNode)requiredProperty); |
| 197 | + } |
| 198 | + |
| 199 | + objSchema.Add(RequiredPropertyName, requiredArray); |
| 200 | + } |
| 201 | + |
| 202 | + if (Items != null) |
| 203 | + { |
| 204 | + objSchema.Add(ItemsPropertyName, Items.ToJsonNode(options)); |
| 205 | + } |
| 206 | + |
| 207 | + if (AdditionalProperties != null) |
| 208 | + { |
| 209 | + objSchema.Add(AdditionalPropertiesPropertyName, AdditionalProperties.ToJsonNode(options)); |
| 210 | + } |
| 211 | + |
| 212 | + if (Enum != null) |
| 213 | + { |
| 214 | + objSchema.Add(EnumPropertyName, Enum); |
| 215 | + } |
| 216 | + |
| 217 | + if (Not != null) |
| 218 | + { |
| 219 | + objSchema.Add(NotPropertyName, Not.ToJsonNode(options)); |
| 220 | + } |
| 221 | + |
| 222 | + if (AnyOf != null) |
| 223 | + { |
| 224 | + JsonArray anyOfArray = []; |
| 225 | + foreach (JsonSchema schema in AnyOf) |
| 226 | + { |
| 227 | + anyOfArray.Add(schema.ToJsonNode(options)); |
| 228 | + } |
| 229 | + |
| 230 | + objSchema.Add(AnyOfPropertyName, anyOfArray); |
| 231 | + } |
| 232 | + |
| 233 | + if (HasDefaultValue) |
| 234 | + { |
| 235 | + objSchema.Add(DefaultPropertyName, DefaultValue); |
| 236 | + } |
| 237 | + |
| 238 | + if (MinLength is int minLength) |
| 239 | + { |
| 240 | + objSchema.Add(MinLengthPropertyName, (JsonNode)minLength); |
| 241 | + } |
| 242 | + |
| 243 | + if (MaxLength is int maxLength) |
| 244 | + { |
| 245 | + objSchema.Add(MaxLengthPropertyName, (JsonNode)maxLength); |
| 246 | + } |
| 247 | + |
| 248 | + return CompleteSchema(objSchema); |
| 249 | + |
| 250 | + JsonNode CompleteSchema(JsonNode schema) |
| 251 | + { |
| 252 | + if (ExporterContext is { } context) |
| 253 | + { |
| 254 | + Debug.Assert(options.TransformSchemaNode != null, "context should only be populated if a callback is present."); |
| 255 | + // Apply any user-defined transformations to the schema. |
| 256 | + return options.TransformSchemaNode(context, schema); |
| 257 | + } |
| 258 | + |
| 259 | + return schema; |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + private static ReadOnlySpan<JsonSchemaType> s_schemaValues => |
| 264 | + [ |
| 265 | + // NB the order of these values influences order of types in the rendered schema |
| 266 | + JsonSchemaType.String, |
| 267 | + JsonSchemaType.Integer, |
| 268 | + JsonSchemaType.Number, |
| 269 | + JsonSchemaType.Boolean, |
| 270 | + JsonSchemaType.Array, |
| 271 | + JsonSchemaType.Object, |
| 272 | + JsonSchemaType.Null, |
| 273 | + ]; |
| 274 | + |
| 275 | + private void VerifyMutable() |
| 276 | + { |
| 277 | + Debug.Assert(_trueOrFalse is null, "Schema is not mutable"); |
| 278 | + if (_trueOrFalse is not null) |
| 279 | + { |
| 280 | + Throw(); |
| 281 | + static void Throw() => throw new InvalidOperationException(); |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + public static JsonNode? MapSchemaType(JsonSchemaType schemaType) |
| 286 | + { |
| 287 | + if (schemaType is JsonSchemaType.Any) |
| 288 | + { |
| 289 | + return null; |
| 290 | + } |
| 291 | + |
| 292 | + if (ToIdentifier(schemaType) is string identifier) |
| 293 | + { |
| 294 | + return identifier; |
| 295 | + } |
| 296 | + |
| 297 | + var array = new JsonArray(); |
| 298 | + foreach (JsonSchemaType type in s_schemaValues) |
| 299 | + { |
| 300 | + if ((schemaType & type) != 0) |
| 301 | + { |
| 302 | + array.Add((JsonNode)ToIdentifier(type)!); |
| 303 | + } |
| 304 | + } |
| 305 | + |
| 306 | + return array; |
| 307 | + |
| 308 | + static string? ToIdentifier(JsonSchemaType schemaType) |
| 309 | + { |
| 310 | + return schemaType switch |
| 311 | + { |
| 312 | + JsonSchemaType.Null => "null", |
| 313 | + JsonSchemaType.Boolean => "boolean", |
| 314 | + JsonSchemaType.Integer => "integer", |
| 315 | + JsonSchemaType.Number => "number", |
| 316 | + JsonSchemaType.String => "string", |
| 317 | + JsonSchemaType.Array => "array", |
| 318 | + JsonSchemaType.Object => "object", |
| 319 | + _ => null, |
| 320 | + }; |
| 321 | + } |
| 322 | + } |
| 323 | + } |
| 324 | +} |
0 commit comments