| | | 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.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.Runtime.InteropServices; |
| | | 9 | | using System.Text.Json.Nodes; |
| | | 10 | | using System.Text.Json.Serialization; |
| | | 11 | | using System.Text.Json.Serialization.Metadata; |
| | | 12 | | |
| | | 13 | | namespace System.Text.Json.Schema |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Functionality for exporting JSON schema from serialization contracts defined in <see cref="JsonTypeInfo"/>. |
| | | 17 | | /// </summary> |
| | | 18 | | public static class JsonSchemaExporter |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Gets the JSON schema for <paramref name="type"/> as a <see cref="JsonNode"/> document. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="options">The options declaring the contract for the type.</param> |
| | | 24 | | /// <param name="type">The type for which to resolve a schema.</param> |
| | | 25 | | /// <param name="exporterOptions">The options object governing the export operation.</param> |
| | | 26 | | /// <returns>A JSON object containing the schema for <paramref name="type"/>.</returns> |
| | | 27 | | public static JsonNode GetJsonSchemaAsNode(this JsonSerializerOptions options, Type type, JsonSchemaExporterOpti |
| | 0 | 28 | | { |
| | 0 | 29 | | ArgumentNullException.ThrowIfNull(options); |
| | 0 | 30 | | ArgumentNullException.ThrowIfNull(type); |
| | | 31 | | |
| | 0 | 32 | | ValidateOptions(options); |
| | 0 | 33 | | JsonTypeInfo typeInfo = options.GetTypeInfoInternal(type); |
| | 0 | 34 | | return typeInfo.GetJsonSchemaAsNode(exporterOptions); |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets the JSON schema for <paramref name="typeInfo"/> as a <see cref="JsonNode"/> document. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="typeInfo">The contract from which to resolve the JSON schema.</param> |
| | | 41 | | /// <param name="exporterOptions">The options object governing the export operation.</param> |
| | | 42 | | /// <returns>A JSON object containing the schema for <paramref name="typeInfo"/>.</returns> |
| | | 43 | | public static JsonNode GetJsonSchemaAsNode(this JsonTypeInfo typeInfo, JsonSchemaExporterOptions? exporterOption |
| | 0 | 44 | | { |
| | 0 | 45 | | ArgumentNullException.ThrowIfNull(typeInfo); |
| | | 46 | | |
| | 0 | 47 | | ValidateOptions(typeInfo.Options); |
| | 0 | 48 | | exporterOptions ??= JsonSchemaExporterOptions.Default; |
| | | 49 | | |
| | 0 | 50 | | typeInfo.EnsureConfigured(); |
| | 0 | 51 | | GenerationState state = new(typeInfo.Options, exporterOptions); |
| | 0 | 52 | | JsonSchema schema = MapJsonSchemaCore(ref state, typeInfo); |
| | 0 | 53 | | return schema.ToJsonNode(exporterOptions); |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | private static JsonSchema MapJsonSchemaCore( |
| | | 57 | | ref GenerationState state, |
| | | 58 | | JsonTypeInfo typeInfo, |
| | | 59 | | JsonPropertyInfo? propertyInfo = null, |
| | | 60 | | JsonConverter? customConverter = null, |
| | | 61 | | JsonNumberHandling? customNumberHandling = null, |
| | | 62 | | JsonTypeInfo? parentPolymorphicTypeInfo = null, |
| | | 63 | | bool parentPolymorphicTypeContainsTypesWithoutDiscriminator = false, |
| | | 64 | | bool parentPolymorphicTypeIsNonNullable = false, |
| | | 65 | | KeyValuePair<string, JsonSchema>? typeDiscriminator = null, |
| | | 66 | | bool cacheResult = true) |
| | 0 | 67 | | { |
| | 0 | 68 | | Debug.Assert(typeInfo.IsConfigured); |
| | | 69 | | |
| | 0 | 70 | | JsonSchemaExporterContext exporterContext = state.CreateContext(typeInfo, propertyInfo, parentPolymorphicTyp |
| | | 71 | | |
| | 0 | 72 | | if (cacheResult && typeInfo.Kind is not JsonTypeInfoKind.None && |
| | 0 | 73 | | state.TryGetExistingJsonPointer(exporterContext, out string? existingJsonPointer)) |
| | 0 | 74 | | { |
| | | 75 | | // The schema context has already been generated in the schema document, return a reference to it. |
| | 0 | 76 | | return CompleteSchema(ref state, new JsonSchema { Ref = existingJsonPointer }); |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | JsonConverter effectiveConverter = customConverter ?? typeInfo.Converter; |
| | 0 | 80 | | JsonNumberHandling effectiveNumberHandling = customNumberHandling ?? typeInfo.NumberHandling ?? typeInfo.Opt |
| | 0 | 81 | | if (effectiveConverter.GetSchema(effectiveNumberHandling) is { } schema) |
| | 0 | 82 | | { |
| | | 83 | | // A schema has been provided by the converter. |
| | 0 | 84 | | return CompleteSchema(ref state, schema); |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | if (parentPolymorphicTypeInfo is null && typeInfo.PolymorphismOptions is { DerivedTypes.Count: > 0 } polyOpt |
| | 0 | 88 | | { |
| | | 89 | | // This is the base type of a polymorphic type hierarchy. The schema for this type |
| | | 90 | | // will include an "anyOf" property with the schemas for all derived types. |
| | 0 | 91 | | string typeDiscriminatorKey = polyOptions.TypeDiscriminatorPropertyName; |
| | 0 | 92 | | List<JsonDerivedType> derivedTypes = new(polyOptions.DerivedTypes); |
| | | 93 | | |
| | 0 | 94 | | if (!typeInfo.Type.IsAbstract && !IsPolymorphicTypeThatSpecifiesItselfAsDerivedType(typeInfo)) |
| | 0 | 95 | | { |
| | | 96 | | // For non-abstract base types that haven't been explicitly configured, |
| | | 97 | | // add a trivial schema to the derived types since we should support it. |
| | 0 | 98 | | derivedTypes.Add(new JsonDerivedType(typeInfo.Type)); |
| | 0 | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | bool containsTypesWithoutDiscriminator = derivedTypes.Exists(static derivedTypes => derivedTypes.TypeDis |
| | 0 | 102 | | JsonSchemaType schemaType = JsonSchemaType.Any; |
| | 0 | 103 | | List<JsonSchema>? anyOf = new(derivedTypes.Count); |
| | | 104 | | |
| | 0 | 105 | | state.PushSchemaNode(JsonSchema.AnyOfPropertyName); |
| | | 106 | | |
| | 0 | 107 | | foreach (JsonDerivedType derivedType in derivedTypes) |
| | 0 | 108 | | { |
| | 0 | 109 | | Debug.Assert(derivedType.TypeDiscriminator is null or int or string); |
| | | 110 | | |
| | 0 | 111 | | KeyValuePair<string, JsonSchema>? derivedTypeDiscriminator = null; |
| | 0 | 112 | | if (derivedType.TypeDiscriminator is { } discriminatorValue) |
| | 0 | 113 | | { |
| | 0 | 114 | | JsonNode discriminatorNode = discriminatorValue switch |
| | 0 | 115 | | { |
| | 0 | 116 | | string stringId => (JsonNode)stringId, |
| | 0 | 117 | | _ => (JsonNode)(int)discriminatorValue, |
| | 0 | 118 | | }; |
| | | 119 | | |
| | 0 | 120 | | JsonSchema discriminatorSchema = new() { Constant = discriminatorNode }; |
| | 0 | 121 | | derivedTypeDiscriminator = new(typeDiscriminatorKey, discriminatorSchema); |
| | 0 | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | JsonTypeInfo derivedTypeInfo = typeInfo.Options.GetTypeInfoInternal(derivedType.DerivedType); |
| | | 125 | | |
| | 0 | 126 | | state.PushSchemaNode(anyOf.Count.ToString(CultureInfo.InvariantCulture)); |
| | 0 | 127 | | JsonSchema derivedSchema = MapJsonSchemaCore( |
| | 0 | 128 | | ref state, |
| | 0 | 129 | | derivedTypeInfo, |
| | 0 | 130 | | parentPolymorphicTypeInfo: typeInfo, |
| | 0 | 131 | | typeDiscriminator: derivedTypeDiscriminator, |
| | 0 | 132 | | parentPolymorphicTypeContainsTypesWithoutDiscriminator: containsTypesWithoutDiscriminator, |
| | 0 | 133 | | parentPolymorphicTypeIsNonNullable: propertyInfo is { IsGetNullable: false, IsSetNullable: false |
| | 0 | 134 | | cacheResult: false); |
| | | 135 | | |
| | 0 | 136 | | state.PopSchemaNode(); |
| | | 137 | | |
| | | 138 | | // Determine if all derived schemas have the same type. |
| | 0 | 139 | | if (anyOf.Count == 0) |
| | 0 | 140 | | { |
| | 0 | 141 | | schemaType = derivedSchema.Type; |
| | 0 | 142 | | } |
| | 0 | 143 | | else if (schemaType != derivedSchema.Type) |
| | 0 | 144 | | { |
| | 0 | 145 | | schemaType = JsonSchemaType.Any; |
| | 0 | 146 | | } |
| | | 147 | | |
| | 0 | 148 | | anyOf.Add(derivedSchema); |
| | 0 | 149 | | } |
| | | 150 | | |
| | 0 | 151 | | state.PopSchemaNode(); |
| | | 152 | | |
| | 0 | 153 | | if (schemaType is not JsonSchemaType.Any) |
| | 0 | 154 | | { |
| | | 155 | | // If all derived types have the same schema type, we can simplify the schema |
| | | 156 | | // by moving the type keyword to the base schema and removing it from the derived schemas. |
| | 0 | 157 | | foreach (JsonSchema derivedSchema in anyOf) |
| | 0 | 158 | | { |
| | 0 | 159 | | derivedSchema.Type = JsonSchemaType.Any; |
| | | 160 | | |
| | 0 | 161 | | if (derivedSchema.KeywordCount == 0) |
| | 0 | 162 | | { |
| | | 163 | | // if removing the type results in an empty schema, |
| | | 164 | | // remove the anyOf array entirely since it's always true. |
| | 0 | 165 | | anyOf = null; |
| | 0 | 166 | | break; |
| | | 167 | | } |
| | 0 | 168 | | } |
| | 0 | 169 | | } |
| | | 170 | | |
| | 0 | 171 | | return CompleteSchema(ref state, new() |
| | 0 | 172 | | { |
| | 0 | 173 | | Type = schemaType, |
| | 0 | 174 | | AnyOf = anyOf, |
| | 0 | 175 | | // If all derived types have a discriminator, we can require it in the base schema. |
| | 0 | 176 | | Required = containsTypesWithoutDiscriminator ? null : [typeDiscriminatorKey] |
| | 0 | 177 | | }); |
| | | 178 | | } |
| | | 179 | | |
| | 0 | 180 | | if (effectiveConverter.NullableElementConverter is { } elementConverter) |
| | 0 | 181 | | { |
| | 0 | 182 | | JsonTypeInfo elementTypeInfo = typeInfo.Options.GetTypeInfo(elementConverter.Type!); |
| | 0 | 183 | | schema = MapJsonSchemaCore(ref state, elementTypeInfo, customConverter: elementConverter, cacheResult: f |
| | | 184 | | |
| | 0 | 185 | | if (schema.Enum != null) |
| | 0 | 186 | | { |
| | 0 | 187 | | Debug.Assert(elementTypeInfo.Type.IsEnum, "The enum keyword should only be populated by schemas for |
| | 0 | 188 | | schema.Enum.Add(null); // Append null to the enum array. |
| | 0 | 189 | | } |
| | | 190 | | |
| | 0 | 191 | | return CompleteSchema(ref state, schema); |
| | | 192 | | } |
| | | 193 | | |
| | 0 | 194 | | switch (typeInfo.Kind) |
| | | 195 | | { |
| | | 196 | | case JsonTypeInfoKind.Object: |
| | 0 | 197 | | List<KeyValuePair<string, JsonSchema>>? properties = null; |
| | 0 | 198 | | List<string>? required = null; |
| | 0 | 199 | | JsonSchema? additionalProperties = null; |
| | | 200 | | |
| | 0 | 201 | | JsonUnmappedMemberHandling effectiveUnmappedMemberHandling = typeInfo.UnmappedMemberHandling ?? type |
| | 0 | 202 | | if (effectiveUnmappedMemberHandling is JsonUnmappedMemberHandling.Disallow) |
| | 0 | 203 | | { |
| | 0 | 204 | | additionalProperties = JsonSchema.CreateFalseSchema(); |
| | 0 | 205 | | } |
| | | 206 | | |
| | 0 | 207 | | if (typeDiscriminator is { } typeDiscriminatorPair) |
| | 0 | 208 | | { |
| | 0 | 209 | | (properties ??= []).Add(typeDiscriminatorPair); |
| | 0 | 210 | | if (parentPolymorphicTypeContainsTypesWithoutDiscriminator) |
| | 0 | 211 | | { |
| | | 212 | | // Require the discriminator here since it's not common to all derived types. |
| | 0 | 213 | | (required ??= []).Add(typeDiscriminatorPair.Key); |
| | 0 | 214 | | } |
| | 0 | 215 | | } |
| | | 216 | | |
| | 0 | 217 | | state.PushSchemaNode(JsonSchema.PropertiesPropertyName); |
| | 0 | 218 | | foreach (JsonPropertyInfo property in typeInfo.Properties) |
| | 0 | 219 | | { |
| | 0 | 220 | | if (property is { Get: null, Set: null } or { IsExtensionData: true }) |
| | 0 | 221 | | { |
| | 0 | 222 | | continue; // Skip JsonIgnored properties and extension data |
| | | 223 | | } |
| | | 224 | | |
| | 0 | 225 | | state.PushSchemaNode(property.Name); |
| | 0 | 226 | | JsonSchema propertySchema = MapJsonSchemaCore( |
| | 0 | 227 | | ref state, |
| | 0 | 228 | | property.JsonTypeInfo, |
| | 0 | 229 | | propertyInfo: property, |
| | 0 | 230 | | customConverter: property.EffectiveConverter, |
| | 0 | 231 | | customNumberHandling: property.EffectiveNumberHandling); |
| | | 232 | | |
| | 0 | 233 | | state.PopSchemaNode(); |
| | | 234 | | |
| | 0 | 235 | | if (property.AssociatedParameter is { HasDefaultValue: true } parameterInfo) |
| | 0 | 236 | | { |
| | 0 | 237 | | JsonSchema.EnsureMutable(ref propertySchema); |
| | 0 | 238 | | propertySchema.DefaultValue = JsonSerializer.SerializeToNode(parameterInfo.DefaultValue, pro |
| | 0 | 239 | | propertySchema.HasDefaultValue = true; |
| | 0 | 240 | | } |
| | | 241 | | |
| | 0 | 242 | | (properties ??= []).Add(new(property.Name, propertySchema)); |
| | | 243 | | |
| | | 244 | | // Mark as required if either the property is required or the associated constructor parameter i |
| | | 245 | | // While the latter implies the former in cases where the JsonSerializerOptions.RespectRequiredC |
| | | 246 | | // setting has been enabled, for the case of the schema exporter we always mark non-optional con |
| | 0 | 247 | | if (property is { IsRequired: true } or { AssociatedParameter.IsRequiredParameter: true }) |
| | 0 | 248 | | { |
| | 0 | 249 | | (required ??= []).Add(property.Name); |
| | 0 | 250 | | } |
| | 0 | 251 | | } |
| | | 252 | | |
| | 0 | 253 | | state.PopSchemaNode(); |
| | 0 | 254 | | return CompleteSchema(ref state, new() |
| | 0 | 255 | | { |
| | 0 | 256 | | Type = JsonSchemaType.Object, |
| | 0 | 257 | | Properties = properties, |
| | 0 | 258 | | Required = required, |
| | 0 | 259 | | AdditionalProperties = additionalProperties, |
| | 0 | 260 | | }); |
| | | 261 | | |
| | | 262 | | case JsonTypeInfoKind.Enumerable: |
| | 0 | 263 | | Debug.Assert(typeInfo.ElementTypeInfo != null); |
| | | 264 | | |
| | 0 | 265 | | if (typeDiscriminator is null) |
| | 0 | 266 | | { |
| | 0 | 267 | | state.PushSchemaNode(JsonSchema.ItemsPropertyName); |
| | 0 | 268 | | JsonSchema items = MapJsonSchemaCore(ref state, typeInfo.ElementTypeInfo, customNumberHandling: |
| | 0 | 269 | | state.PopSchemaNode(); |
| | | 270 | | |
| | 0 | 271 | | return CompleteSchema(ref state, new() |
| | 0 | 272 | | { |
| | 0 | 273 | | Type = JsonSchemaType.Array, |
| | 0 | 274 | | Items = items.IsTrue ? null : items, |
| | 0 | 275 | | }); |
| | | 276 | | } |
| | | 277 | | else |
| | 0 | 278 | | { |
| | | 279 | | // Polymorphic enumerable types are represented using a wrapping object: |
| | | 280 | | // { "$type" : "discriminator", "$values" : [element1, element2, ...] } |
| | | 281 | | // Which corresponds to the schema |
| | | 282 | | // { "properties" : { "$type" : { "const" : "discriminator" }, "$values" : { "type" : "array", " |
| | | 283 | | const string ValuesKeyword = JsonSerializer.ValuesPropertyName; |
| | | 284 | | |
| | 0 | 285 | | state.PushSchemaNode(JsonSchema.PropertiesPropertyName); |
| | 0 | 286 | | state.PushSchemaNode(ValuesKeyword); |
| | 0 | 287 | | state.PushSchemaNode(JsonSchema.ItemsPropertyName); |
| | | 288 | | |
| | 0 | 289 | | JsonSchema items = MapJsonSchemaCore(ref state, typeInfo.ElementTypeInfo, customNumberHandling: |
| | | 290 | | |
| | 0 | 291 | | state.PopSchemaNode(); |
| | 0 | 292 | | state.PopSchemaNode(); |
| | 0 | 293 | | state.PopSchemaNode(); |
| | | 294 | | |
| | 0 | 295 | | return CompleteSchema(ref state, new() |
| | 0 | 296 | | { |
| | 0 | 297 | | Type = JsonSchemaType.Object, |
| | 0 | 298 | | Properties = |
| | 0 | 299 | | [ |
| | 0 | 300 | | typeDiscriminator.Value, |
| | 0 | 301 | | new(ValuesKeyword, |
| | 0 | 302 | | new JsonSchema() |
| | 0 | 303 | | { |
| | 0 | 304 | | Type = JsonSchemaType.Array, |
| | 0 | 305 | | Items = items.IsTrue ? null : items, |
| | 0 | 306 | | }), |
| | 0 | 307 | | ], |
| | 0 | 308 | | Required = parentPolymorphicTypeContainsTypesWithoutDiscriminator ? [typeDiscriminator.Value |
| | 0 | 309 | | }); |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | case JsonTypeInfoKind.Dictionary: |
| | 0 | 313 | | Debug.Assert(typeInfo.ElementTypeInfo != null); |
| | | 314 | | |
| | 0 | 315 | | List<KeyValuePair<string, JsonSchema>>? dictProps = null; |
| | 0 | 316 | | List<string>? dictRequired = null; |
| | | 317 | | |
| | 0 | 318 | | if (typeDiscriminator is { } dictDiscriminator) |
| | 0 | 319 | | { |
| | 0 | 320 | | dictProps = [dictDiscriminator]; |
| | 0 | 321 | | if (parentPolymorphicTypeContainsTypesWithoutDiscriminator) |
| | 0 | 322 | | { |
| | | 323 | | // Require the discriminator here since it's not common to all derived types. |
| | 0 | 324 | | dictRequired = [dictDiscriminator.Key]; |
| | 0 | 325 | | } |
| | 0 | 326 | | } |
| | | 327 | | |
| | 0 | 328 | | state.PushSchemaNode(JsonSchema.AdditionalPropertiesPropertyName); |
| | 0 | 329 | | JsonSchema valueSchema = MapJsonSchemaCore(ref state, typeInfo.ElementTypeInfo, customNumberHandling |
| | 0 | 330 | | state.PopSchemaNode(); |
| | | 331 | | |
| | 0 | 332 | | return CompleteSchema(ref state, new() |
| | 0 | 333 | | { |
| | 0 | 334 | | Type = JsonSchemaType.Object, |
| | 0 | 335 | | Properties = dictProps, |
| | 0 | 336 | | Required = dictRequired, |
| | 0 | 337 | | AdditionalProperties = valueSchema.IsTrue ? null : valueSchema, |
| | 0 | 338 | | }); |
| | | 339 | | |
| | | 340 | | default: |
| | 0 | 341 | | Debug.Assert(typeInfo.Kind is JsonTypeInfoKind.None); |
| | | 342 | | // Return a `true` schema for types with user-defined converters. |
| | 0 | 343 | | return CompleteSchema(ref state, JsonSchema.CreateTrueSchema()); |
| | | 344 | | } |
| | | 345 | | |
| | | 346 | | JsonSchema CompleteSchema(ref GenerationState state, JsonSchema schema) |
| | 0 | 347 | | { |
| | 0 | 348 | | if (schema.Ref is null) |
| | 0 | 349 | | { |
| | 0 | 350 | | if (IsNullableSchema(state.ExporterOptions)) |
| | 0 | 351 | | { |
| | 0 | 352 | | schema.MakeNullable(); |
| | 0 | 353 | | } |
| | | 354 | | |
| | | 355 | | bool IsNullableSchema(JsonSchemaExporterOptions options) |
| | 0 | 356 | | { |
| | | 357 | | // A schema is marked as nullable if either: |
| | | 358 | | // 1. We have a schema for a property where either the getter or setter are marked as nullable. |
| | | 359 | | // 2. We have a schema for a Nullable<T> type. |
| | | 360 | | // 3. We have a schema for a reference type, unless we're explicitly treating null-oblivious typ |
| | | 361 | | |
| | 0 | 362 | | if (propertyInfo is not null) |
| | 0 | 363 | | { |
| | 0 | 364 | | return propertyInfo.IsGetNullable || propertyInfo.IsSetNullable; |
| | | 365 | | } |
| | | 366 | | |
| | 0 | 367 | | if (typeInfo.IsNullable) |
| | 0 | 368 | | { |
| | 0 | 369 | | return true; |
| | | 370 | | } |
| | | 371 | | |
| | 0 | 372 | | return !typeInfo.Type.IsValueType && !parentPolymorphicTypeIsNonNullable && !options.TreatNullOb |
| | 0 | 373 | | } |
| | 0 | 374 | | } |
| | | 375 | | |
| | 0 | 376 | | if (state.ExporterOptions.TransformSchemaNode != null) |
| | 0 | 377 | | { |
| | | 378 | | // Prime the schema for invocation by the JsonNode transformer. |
| | 0 | 379 | | schema.ExporterContext = exporterContext; |
| | 0 | 380 | | } |
| | | 381 | | |
| | 0 | 382 | | return schema; |
| | 0 | 383 | | } |
| | 0 | 384 | | } |
| | | 385 | | |
| | | 386 | | private static void ValidateOptions(JsonSerializerOptions options) |
| | 0 | 387 | | { |
| | 0 | 388 | | if (options.ReferenceHandler == ReferenceHandler.Preserve) |
| | 0 | 389 | | { |
| | 0 | 390 | | ThrowHelper.ThrowNotSupportedException_JsonSchemaExporterDoesNotSupportReferenceHandlerPreserve(); |
| | | 391 | | } |
| | | 392 | | |
| | 0 | 393 | | options.MakeReadOnly(); |
| | 0 | 394 | | } |
| | | 395 | | |
| | | 396 | | private static bool IsPolymorphicTypeThatSpecifiesItselfAsDerivedType(JsonTypeInfo typeInfo) |
| | 0 | 397 | | { |
| | 0 | 398 | | Debug.Assert(typeInfo.PolymorphismOptions is not null); |
| | | 399 | | |
| | 0 | 400 | | foreach (JsonDerivedType derivedType in typeInfo.PolymorphismOptions.DerivedTypes) |
| | 0 | 401 | | { |
| | 0 | 402 | | if (derivedType.DerivedType == typeInfo.Type) |
| | 0 | 403 | | { |
| | 0 | 404 | | return true; |
| | | 405 | | } |
| | 0 | 406 | | } |
| | | 407 | | |
| | 0 | 408 | | return false; |
| | 0 | 409 | | } |
| | | 410 | | |
| | | 411 | | private readonly ref struct GenerationState(JsonSerializerOptions options, JsonSchemaExporterOptions exporterOpt |
| | | 412 | | { |
| | 0 | 413 | | private readonly List<string> _currentPath = []; |
| | 0 | 414 | | private readonly Dictionary<(JsonTypeInfo, JsonPropertyInfo?), string[]> _generated = new(); |
| | | 415 | | |
| | 0 | 416 | | public int CurrentDepth => _currentPath.Count; |
| | 0 | 417 | | public JsonSerializerOptions Options { get; } = options; |
| | 0 | 418 | | public JsonSchemaExporterOptions ExporterOptions { get; } = exporterOptions; |
| | | 419 | | |
| | | 420 | | public void PushSchemaNode(string nodeId) |
| | 0 | 421 | | { |
| | 0 | 422 | | if (CurrentDepth == Options.EffectiveMaxDepth) |
| | 0 | 423 | | { |
| | 0 | 424 | | ThrowHelper.ThrowInvalidOperationException_JsonSchemaExporterDepthTooLarge(); |
| | 0 | 425 | | } |
| | | 426 | | |
| | 0 | 427 | | _currentPath.Add(nodeId); |
| | 0 | 428 | | } |
| | | 429 | | |
| | | 430 | | public void PopSchemaNode() |
| | 0 | 431 | | { |
| | 0 | 432 | | Debug.Assert(CurrentDepth > 0); |
| | 0 | 433 | | _currentPath.RemoveAt(_currentPath.Count - 1); |
| | 0 | 434 | | } |
| | | 435 | | |
| | | 436 | | /// <summary> |
| | | 437 | | /// Registers the current schema node generation context; if it has already been generated return a JSON poi |
| | | 438 | | /// </summary> |
| | | 439 | | public bool TryGetExistingJsonPointer(in JsonSchemaExporterContext context, [NotNullWhen(true)] out string? |
| | 0 | 440 | | { |
| | 0 | 441 | | (JsonTypeInfo TypeInfo, JsonPropertyInfo? PropertyInfo) key = (context.TypeInfo, context.PropertyInfo); |
| | | 442 | | #if NET |
| | 0 | 443 | | ref string[]? pathToSchema = ref CollectionsMarshal.GetValueRefOrAddDefault(_generated, key, out bool ex |
| | | 444 | | #else |
| | | 445 | | bool exists = _generated.TryGetValue(key, out string[]? pathToSchema); |
| | | 446 | | #endif |
| | 0 | 447 | | if (exists) |
| | 0 | 448 | | { |
| | 0 | 449 | | existingJsonPointer = FormatJsonPointer(pathToSchema); |
| | 0 | 450 | | return true; |
| | | 451 | | } |
| | | 452 | | #if NET |
| | 0 | 453 | | pathToSchema = context._path; |
| | | 454 | | #else |
| | | 455 | | _generated[key] = context._path; |
| | | 456 | | #endif |
| | 0 | 457 | | existingJsonPointer = null; |
| | 0 | 458 | | return false; |
| | 0 | 459 | | } |
| | | 460 | | |
| | | 461 | | public JsonSchemaExporterContext CreateContext(JsonTypeInfo typeInfo, JsonPropertyInfo? propertyInfo, JsonTy |
| | 0 | 462 | | { |
| | 0 | 463 | | return new JsonSchemaExporterContext(typeInfo, propertyInfo, baseTypeInfo, [.. _currentPath]); |
| | 0 | 464 | | } |
| | | 465 | | |
| | | 466 | | private static string FormatJsonPointer(ReadOnlySpan<string> path) |
| | 0 | 467 | | { |
| | 0 | 468 | | if (path.IsEmpty) |
| | 0 | 469 | | { |
| | 0 | 470 | | return "#"; |
| | | 471 | | } |
| | | 472 | | |
| | 0 | 473 | | using ValueStringBuilder sb = new(initialCapacity: path.Length * 10); |
| | 0 | 474 | | sb.Append('#'); |
| | | 475 | | |
| | 0 | 476 | | foreach (string segment in path) |
| | 0 | 477 | | { |
| | 0 | 478 | | ReadOnlySpan<char> span = segment.AsSpan(); |
| | 0 | 479 | | sb.Append('/'); |
| | | 480 | | |
| | | 481 | | do |
| | 0 | 482 | | { |
| | | 483 | | // Per RFC 6901 the characters '~' and '/' must be escaped. |
| | 0 | 484 | | int pos = span.IndexOfAny('~', '/'); |
| | 0 | 485 | | if (pos < 0) |
| | 0 | 486 | | { |
| | 0 | 487 | | sb.Append(span); |
| | 0 | 488 | | break; |
| | | 489 | | } |
| | | 490 | | |
| | 0 | 491 | | sb.Append(span.Slice(0, pos)); |
| | | 492 | | |
| | 0 | 493 | | if (span[pos] == '~') |
| | 0 | 494 | | { |
| | 0 | 495 | | sb.Append("~0"); |
| | 0 | 496 | | } |
| | | 497 | | else |
| | 0 | 498 | | { |
| | 0 | 499 | | Debug.Assert(span[pos] == '/'); |
| | 0 | 500 | | sb.Append("~1"); |
| | 0 | 501 | | } |
| | | 502 | | |
| | 0 | 503 | | span = span.Slice(pos + 1); |
| | 0 | 504 | | } |
| | 0 | 505 | | while (!span.IsEmpty); |
| | 0 | 506 | | } |
| | | 507 | | |
| | 0 | 508 | | return sb.ToString(); |
| | 0 | 509 | | } |
| | | 510 | | } |
| | | 511 | | } |
| | | 512 | | } |