| | | 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 | | |
| | 0 | 29 | | public static JsonSchema CreateFalseSchema() => new(false); |
| | 0 | 30 | | public static JsonSchema CreateTrueSchema() => new(true); |
| | | 31 | | |
| | 0 | 32 | | public JsonSchema() { } |
| | 0 | 33 | | private JsonSchema(bool trueOrFalse) { _trueOrFalse = trueOrFalse; } |
| | | 34 | | |
| | 0 | 35 | | public bool IsTrue => _trueOrFalse is true; |
| | | 36 | | public bool IsFalse => _trueOrFalse is false; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Per the JSON schema core specification section 4.3 |
| | | 40 | | /// (https://json-schema.org/draft/2020-12/json-schema-core#name-json-schema-documents) |
| | | 41 | | /// A JSON schema must either be an object or a boolean. |
| | | 42 | | /// We represent false and true schemas using this flag. |
| | | 43 | | /// It is not possible to specify keywords in boolean schemas. |
| | | 44 | | /// </summary> |
| | | 45 | | private readonly bool? _trueOrFalse; |
| | | 46 | | |
| | 0 | 47 | | public string? Ref { get => _ref; set { VerifyMutable(); _ref = value; } } |
| | | 48 | | private string? _ref; |
| | | 49 | | |
| | 0 | 50 | | public string? Comment { get => _comment; set { VerifyMutable(); _comment = value; } } |
| | | 51 | | private string? _comment; |
| | | 52 | | |
| | 0 | 53 | | public JsonSchemaType Type { get => _type; set { VerifyMutable(); _type = value; } } |
| | 0 | 54 | | private JsonSchemaType _type = JsonSchemaType.Any; |
| | | 55 | | |
| | 0 | 56 | | public string? Format { get => _format; set { VerifyMutable(); _format = value; } } |
| | | 57 | | private string? _format; |
| | | 58 | | |
| | 0 | 59 | | public string? Pattern { get => _pattern; set { VerifyMutable(); _pattern = value; } } |
| | | 60 | | private string? _pattern; |
| | | 61 | | |
| | 0 | 62 | | public JsonNode? Constant { get => _constant; set { VerifyMutable(); _constant = value; } } |
| | | 63 | | private JsonNode? _constant; |
| | | 64 | | |
| | 0 | 65 | | public List<KeyValuePair<string, JsonSchema>>? Properties { get => _properties; set { VerifyMutable(); _properti |
| | | 66 | | private List<KeyValuePair<string, JsonSchema>>? _properties; |
| | | 67 | | |
| | 0 | 68 | | public List<string>? Required { get => _required; set { VerifyMutable(); _required = value; } } |
| | | 69 | | private List<string>? _required; |
| | | 70 | | |
| | 0 | 71 | | public JsonSchema? Items { get => _items; set { VerifyMutable(); _items = value; } } |
| | | 72 | | private JsonSchema? _items; |
| | | 73 | | |
| | 0 | 74 | | public JsonSchema? AdditionalProperties { get => _additionalProperties; set { VerifyMutable(); _additionalProper |
| | | 75 | | private JsonSchema? _additionalProperties; |
| | | 76 | | |
| | 0 | 77 | | public JsonArray? Enum { get => _enum; set { VerifyMutable(); _enum = value; } } |
| | | 78 | | private JsonArray? _enum; |
| | | 79 | | |
| | 0 | 80 | | public JsonSchema? Not { get => _not; set { VerifyMutable(); _not = value; } } |
| | | 81 | | private JsonSchema? _not; |
| | | 82 | | |
| | 0 | 83 | | public List<JsonSchema>? AnyOf { get => _anyOf; set { VerifyMutable(); _anyOf = value; } } |
| | | 84 | | private List<JsonSchema>? _anyOf; |
| | | 85 | | |
| | 0 | 86 | | public bool HasDefaultValue { get => _hasDefaultValue; set { VerifyMutable(); _hasDefaultValue = value; } } |
| | | 87 | | private bool _hasDefaultValue; |
| | | 88 | | |
| | 0 | 89 | | public JsonNode? DefaultValue { get => _defaultValue; set { VerifyMutable(); _defaultValue = value; } } |
| | | 90 | | private JsonNode? _defaultValue; |
| | | 91 | | |
| | 0 | 92 | | public int? MinLength { get => _minLength; set { VerifyMutable(); _minLength = value; } } |
| | | 93 | | private int? _minLength; |
| | | 94 | | |
| | 0 | 95 | | public int? MaxLength { get => _maxLength; set { VerifyMutable(); _maxLength = value; } } |
| | | 96 | | private int? _maxLength; |
| | | 97 | | |
| | 0 | 98 | | public JsonSchemaExporterContext? ExporterContext { get; set; } |
| | | 99 | | |
| | | 100 | | public int KeywordCount |
| | | 101 | | { |
| | | 102 | | get |
| | 0 | 103 | | { |
| | 0 | 104 | | if (_trueOrFalse != null) |
| | 0 | 105 | | { |
| | | 106 | | // Boolean schemas admit no keywords |
| | 0 | 107 | | return 0; |
| | | 108 | | } |
| | | 109 | | |
| | 0 | 110 | | int count = 0; |
| | 0 | 111 | | Count(Ref != null); |
| | 0 | 112 | | Count(Comment != null); |
| | 0 | 113 | | Count(Type != JsonSchemaType.Any); |
| | 0 | 114 | | Count(Format != null); |
| | 0 | 115 | | Count(Pattern != null); |
| | 0 | 116 | | Count(Constant != null); |
| | 0 | 117 | | Count(Properties != null); |
| | 0 | 118 | | Count(Required != null); |
| | 0 | 119 | | Count(Items != null); |
| | 0 | 120 | | Count(AdditionalProperties != null); |
| | 0 | 121 | | Count(Enum != null); |
| | 0 | 122 | | Count(Not != null); |
| | 0 | 123 | | Count(AnyOf != null); |
| | 0 | 124 | | Count(HasDefaultValue); |
| | 0 | 125 | | Count(MinLength != null); |
| | 0 | 126 | | Count(MaxLength != null); |
| | | 127 | | |
| | 0 | 128 | | return count; |
| | | 129 | | |
| | | 130 | | void Count(bool isKeywordSpecified) |
| | 0 | 131 | | { |
| | 0 | 132 | | count += isKeywordSpecified ? 1 : 0; |
| | 0 | 133 | | } |
| | 0 | 134 | | } |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | public void MakeNullable() |
| | 0 | 138 | | { |
| | 0 | 139 | | if (_trueOrFalse != null) |
| | 0 | 140 | | { |
| | | 141 | | // boolean schemas do not admit type keywords. |
| | 0 | 142 | | return; |
| | | 143 | | } |
| | | 144 | | |
| | 0 | 145 | | if (Type != JsonSchemaType.Any) |
| | 0 | 146 | | { |
| | 0 | 147 | | Type |= JsonSchemaType.Null; |
| | 0 | 148 | | } |
| | 0 | 149 | | } |
| | | 150 | | |
| | | 151 | | public JsonNode ToJsonNode(JsonSchemaExporterOptions options) |
| | 0 | 152 | | { |
| | 0 | 153 | | if (_trueOrFalse is { } boolSchema) |
| | 0 | 154 | | { |
| | 0 | 155 | | return CompleteSchema((JsonNode)boolSchema); |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | var objSchema = new JsonObject(); |
| | | 159 | | |
| | 0 | 160 | | if (Ref != null) |
| | 0 | 161 | | { |
| | 0 | 162 | | objSchema.Add(RefPropertyName, Ref); |
| | 0 | 163 | | } |
| | | 164 | | |
| | 0 | 165 | | if (Comment != null) |
| | 0 | 166 | | { |
| | 0 | 167 | | objSchema.Add(CommentPropertyName, Comment); |
| | 0 | 168 | | } |
| | | 169 | | |
| | 0 | 170 | | if (MapSchemaType(Type) is JsonNode type) |
| | 0 | 171 | | { |
| | 0 | 172 | | objSchema.Add(TypePropertyName, type); |
| | 0 | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | if (Format != null) |
| | 0 | 176 | | { |
| | 0 | 177 | | objSchema.Add(FormatPropertyName, Format); |
| | 0 | 178 | | } |
| | | 179 | | |
| | 0 | 180 | | if (Pattern != null) |
| | 0 | 181 | | { |
| | 0 | 182 | | objSchema.Add(PatternPropertyName, Pattern); |
| | 0 | 183 | | } |
| | | 184 | | |
| | 0 | 185 | | if (Constant != null) |
| | 0 | 186 | | { |
| | 0 | 187 | | objSchema.Add(ConstPropertyName, Constant); |
| | 0 | 188 | | } |
| | | 189 | | |
| | 0 | 190 | | if (Properties != null) |
| | 0 | 191 | | { |
| | 0 | 192 | | var properties = new JsonObject(); |
| | 0 | 193 | | foreach (KeyValuePair<string, JsonSchema> property in Properties) |
| | 0 | 194 | | { |
| | 0 | 195 | | properties.Add(property.Key, property.Value.ToJsonNode(options)); |
| | 0 | 196 | | } |
| | | 197 | | |
| | 0 | 198 | | objSchema.Add(PropertiesPropertyName, properties); |
| | 0 | 199 | | } |
| | | 200 | | |
| | 0 | 201 | | if (Required != null) |
| | 0 | 202 | | { |
| | 0 | 203 | | var requiredArray = new JsonArray(); |
| | 0 | 204 | | foreach (string requiredProperty in Required) |
| | 0 | 205 | | { |
| | 0 | 206 | | requiredArray.Add((JsonNode)requiredProperty); |
| | 0 | 207 | | } |
| | | 208 | | |
| | 0 | 209 | | objSchema.Add(RequiredPropertyName, requiredArray); |
| | 0 | 210 | | } |
| | | 211 | | |
| | 0 | 212 | | if (Items != null) |
| | 0 | 213 | | { |
| | 0 | 214 | | objSchema.Add(ItemsPropertyName, Items.ToJsonNode(options)); |
| | 0 | 215 | | } |
| | | 216 | | |
| | 0 | 217 | | if (AdditionalProperties != null) |
| | 0 | 218 | | { |
| | 0 | 219 | | objSchema.Add(AdditionalPropertiesPropertyName, AdditionalProperties.ToJsonNode(options)); |
| | 0 | 220 | | } |
| | | 221 | | |
| | 0 | 222 | | if (Enum != null) |
| | 0 | 223 | | { |
| | 0 | 224 | | objSchema.Add(EnumPropertyName, Enum); |
| | 0 | 225 | | } |
| | | 226 | | |
| | 0 | 227 | | if (Not != null) |
| | 0 | 228 | | { |
| | 0 | 229 | | objSchema.Add(NotPropertyName, Not.ToJsonNode(options)); |
| | 0 | 230 | | } |
| | | 231 | | |
| | 0 | 232 | | if (AnyOf != null) |
| | 0 | 233 | | { |
| | 0 | 234 | | JsonArray anyOfArray = []; |
| | 0 | 235 | | foreach (JsonSchema schema in AnyOf) |
| | 0 | 236 | | { |
| | 0 | 237 | | anyOfArray.Add(schema.ToJsonNode(options)); |
| | 0 | 238 | | } |
| | | 239 | | |
| | 0 | 240 | | objSchema.Add(AnyOfPropertyName, anyOfArray); |
| | 0 | 241 | | } |
| | | 242 | | |
| | 0 | 243 | | if (HasDefaultValue) |
| | 0 | 244 | | { |
| | 0 | 245 | | objSchema.Add(DefaultPropertyName, DefaultValue); |
| | 0 | 246 | | } |
| | | 247 | | |
| | 0 | 248 | | if (MinLength is int minLength) |
| | 0 | 249 | | { |
| | 0 | 250 | | objSchema.Add(MinLengthPropertyName, (JsonNode)minLength); |
| | 0 | 251 | | } |
| | | 252 | | |
| | 0 | 253 | | if (MaxLength is int maxLength) |
| | 0 | 254 | | { |
| | 0 | 255 | | objSchema.Add(MaxLengthPropertyName, (JsonNode)maxLength); |
| | 0 | 256 | | } |
| | | 257 | | |
| | 0 | 258 | | return CompleteSchema(objSchema); |
| | | 259 | | |
| | | 260 | | JsonNode CompleteSchema(JsonNode schema) |
| | 0 | 261 | | { |
| | 0 | 262 | | if (ExporterContext is { } context) |
| | 0 | 263 | | { |
| | 0 | 264 | | Debug.Assert(options.TransformSchemaNode != null, "context should only be populated if a callback is |
| | | 265 | | // Apply any user-defined transformations to the schema. |
| | 0 | 266 | | return options.TransformSchemaNode(context, schema); |
| | | 267 | | } |
| | | 268 | | |
| | 0 | 269 | | return schema; |
| | 0 | 270 | | } |
| | 0 | 271 | | } |
| | | 272 | | |
| | | 273 | | /// <summary> |
| | | 274 | | /// If the schema is boolean, replaces it with a semantically |
| | | 275 | | /// equivalent object schema that allows appending keywords. |
| | | 276 | | /// </summary> |
| | | 277 | | public static void EnsureMutable(ref JsonSchema schema) |
| | 0 | 278 | | { |
| | 0 | 279 | | switch (schema._trueOrFalse) |
| | | 280 | | { |
| | | 281 | | case false: |
| | 0 | 282 | | schema = new JsonSchema { Not = CreateTrueSchema() }; |
| | 0 | 283 | | break; |
| | | 284 | | case true: |
| | 0 | 285 | | schema = new JsonSchema(); |
| | 0 | 286 | | break; |
| | | 287 | | } |
| | 0 | 288 | | } |
| | | 289 | | |
| | | 290 | | private static ReadOnlySpan<JsonSchemaType> s_schemaValues => |
| | 0 | 291 | | [ |
| | 0 | 292 | | // NB the order of these values influences order of types in the rendered schema |
| | 0 | 293 | | JsonSchemaType.String, |
| | 0 | 294 | | JsonSchemaType.Integer, |
| | 0 | 295 | | JsonSchemaType.Number, |
| | 0 | 296 | | JsonSchemaType.Boolean, |
| | 0 | 297 | | JsonSchemaType.Array, |
| | 0 | 298 | | JsonSchemaType.Object, |
| | 0 | 299 | | JsonSchemaType.Null, |
| | 0 | 300 | | ]; |
| | | 301 | | |
| | | 302 | | private void VerifyMutable() |
| | 0 | 303 | | { |
| | 0 | 304 | | Debug.Assert(_trueOrFalse is null, "Schema is not mutable"); |
| | 0 | 305 | | if (_trueOrFalse is not null) |
| | 0 | 306 | | { |
| | 0 | 307 | | Throw(); |
| | 0 | 308 | | static void Throw() => throw new InvalidOperationException(); |
| | 0 | 309 | | } |
| | 0 | 310 | | } |
| | | 311 | | |
| | | 312 | | public static JsonNode? MapSchemaType(JsonSchemaType schemaType) |
| | 0 | 313 | | { |
| | 0 | 314 | | if (schemaType is JsonSchemaType.Any) |
| | 0 | 315 | | { |
| | 0 | 316 | | return null; |
| | | 317 | | } |
| | | 318 | | |
| | 0 | 319 | | if (ToIdentifier(schemaType) is string identifier) |
| | 0 | 320 | | { |
| | 0 | 321 | | return identifier; |
| | | 322 | | } |
| | | 323 | | |
| | 0 | 324 | | var array = new JsonArray(); |
| | 0 | 325 | | foreach (JsonSchemaType type in s_schemaValues) |
| | 0 | 326 | | { |
| | 0 | 327 | | if ((schemaType & type) != 0) |
| | 0 | 328 | | { |
| | 0 | 329 | | array.Add((JsonNode)ToIdentifier(type)!); |
| | 0 | 330 | | } |
| | 0 | 331 | | } |
| | | 332 | | |
| | 0 | 333 | | return array; |
| | | 334 | | |
| | | 335 | | static string? ToIdentifier(JsonSchemaType schemaType) |
| | 0 | 336 | | { |
| | 0 | 337 | | return schemaType switch |
| | 0 | 338 | | { |
| | 0 | 339 | | JsonSchemaType.Null => "null", |
| | 0 | 340 | | JsonSchemaType.Boolean => "boolean", |
| | 0 | 341 | | JsonSchemaType.Integer => "integer", |
| | 0 | 342 | | JsonSchemaType.Number => "number", |
| | 0 | 343 | | JsonSchemaType.String => "string", |
| | 0 | 344 | | JsonSchemaType.Array => "array", |
| | 0 | 345 | | JsonSchemaType.Object => "object", |
| | 0 | 346 | | _ => null, |
| | 0 | 347 | | }; |
| | 0 | 348 | | } |
| | 0 | 349 | | } |
| | | 350 | | } |
| | | 351 | | } |