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