-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathJsonNodeSchemaExtensions.cs
512 lines (485 loc) · 27.2 KB
/
JsonNodeSchemaExtensions.cs
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Schema;
using System.Text.Json.Serialization.Metadata;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Constraints;
using Microsoft.OpenApi.Models;
namespace Microsoft.AspNetCore.OpenApi;
/// <summary>
/// Provides a set of extension methods for modifying the opaque JSON Schema type
/// that is provided by the underlying schema generator in System.Text.Json.
/// </summary>
internal static class JsonNodeSchemaExtensions
{
private static readonly Dictionary<Type, OpenApiSchema> _simpleTypeToOpenApiSchema = new()
{
[typeof(bool)] = new() { Type = JsonSchemaType.Boolean },
[typeof(byte)] = new() { Type = JsonSchemaType.Integer, Format = "uint8" },
[typeof(byte[])] = new() { Type = JsonSchemaType.String, Format = "byte" },
[typeof(int)] = new() { Type = JsonSchemaType.Integer, Format = "int32" },
[typeof(uint)] = new() { Type = JsonSchemaType.Integer, Format = "uint32" },
[typeof(long)] = new() { Type = JsonSchemaType.Integer, Format = "int64" },
[typeof(ulong)] = new() { Type = JsonSchemaType.Integer, Format = "uint64" },
[typeof(short)] = new() { Type = JsonSchemaType.Integer, Format = "int16" },
[typeof(ushort)] = new() { Type = JsonSchemaType.Integer, Format = "uint16" },
[typeof(float)] = new() { Type = JsonSchemaType.Number, Format = "float" },
[typeof(double)] = new() { Type = JsonSchemaType.Number, Format = "double" },
[typeof(decimal)] = new() { Type = JsonSchemaType.Number, Format = "double" },
[typeof(DateTime)] = new() { Type = JsonSchemaType.String, Format = "date-time" },
[typeof(DateTimeOffset)] = new() { Type = JsonSchemaType.String, Format = "date-time" },
[typeof(Guid)] = new() { Type = JsonSchemaType.String, Format = "uuid" },
[typeof(char)] = new() { Type = JsonSchemaType.String, Format = "char" },
[typeof(Uri)] = new() { Type = JsonSchemaType.String, Format = "uri" },
[typeof(string)] = new() { Type = JsonSchemaType.String },
[typeof(TimeOnly)] = new() { Type = JsonSchemaType.String, Format = "time" },
[typeof(DateOnly)] = new() { Type = JsonSchemaType.String, Format = "date" },
};
/// <summary>
/// Maps the given validation attributes to the target schema.
/// </summary>
/// <remarks>
/// OpenApi schema v3 supports the validation vocabulary supported by JSON Schema. Because the underlying
/// schema generator does not handle validation attributes to the validation vocabulary, we apply that mapping here.
///
/// Note that this method targets <see cref="JsonNode"/> and not <see cref="OpenApiSchema"/> because it is
/// designed to be invoked via the `OnGenerated` callback provided by the underlying schema generator
/// so that attributes can be mapped to the properties associated with inputs and outputs to a given request.
///
/// This implementation only supports mapping validation attributes that have an associated keyword in the
/// validation vocabulary.
///
/// Validation attributes are applied in a last-wins-order. For example, the following set of attributes:
///
/// [Range(1, 10), Min(5)]
///
/// will result in the schema having a minimum value of 5 and a maximum value of 10. This rule applies even
/// though the model binding layer in MVC applies all validation attributes on an argument. The following
/// set of attributes:
///
/// [Base64String]
/// [Url]
/// public string Url { get; }
///
/// will result in the schema having a type of "string" and a format of "uri" even though the model binding
/// layer will validate the string against *both* constraints.
/// </remarks>
/// <param name="schema">The <see cref="JsonNode"/> produced by the underlying schema generator.</param>
/// <param name="validationAttributes">A list of the validation attributes to apply.</param>
internal static void ApplyValidationAttributes(this JsonNode schema, IEnumerable<Attribute> validationAttributes)
{
foreach (var attribute in validationAttributes)
{
if (attribute is Base64StringAttribute)
{
schema[OpenApiSchemaKeywords.TypeKeyword] = JsonSchemaType.String.ToString();
schema[OpenApiSchemaKeywords.FormatKeyword] = "byte";
}
else if (attribute is RangeAttribute rangeAttribute)
{
// Use InvariantCulture if explicitly requested or if the range has been set via the
// RangeAttribute(double, double) or RangeAttribute(int, int) constructors.
var targetCulture = rangeAttribute.ParseLimitsInInvariantCulture || rangeAttribute.Minimum is double || rangeAttribute.Maximum is int
? CultureInfo.InvariantCulture
: CultureInfo.CurrentCulture;
var minString = rangeAttribute.Minimum.ToString();
var maxString = rangeAttribute.Maximum.ToString();
if (decimal.TryParse(minString, NumberStyles.Any, targetCulture, out var minDecimal))
{
schema[OpenApiSchemaKeywords.MinimumKeyword] = minDecimal;
}
if (decimal.TryParse(maxString, NumberStyles.Any, targetCulture, out var maxDecimal))
{
schema[OpenApiSchemaKeywords.MaximumKeyword] = maxDecimal;
}
}
else if (attribute is RegularExpressionAttribute regularExpressionAttribute)
{
schema[OpenApiSchemaKeywords.PatternKeyword] = regularExpressionAttribute.Pattern;
}
else if (attribute is MaxLengthAttribute maxLengthAttribute)
{
var isArray = MapJsonNodeToSchemaType(schema[OpenApiSchemaKeywords.TypeKeyword]) is { } schemaTypes && schemaTypes.HasFlag(JsonSchemaType.Array);
var key = isArray ? OpenApiSchemaKeywords.MaxItemsKeyword : OpenApiSchemaKeywords.MaxLengthKeyword;
schema[key] = maxLengthAttribute.Length;
}
else if (attribute is MinLengthAttribute minLengthAttribute)
{
var isArray = MapJsonNodeToSchemaType(schema[OpenApiSchemaKeywords.TypeKeyword]) is { } schemaTypes && schemaTypes.HasFlag(JsonSchemaType.Array);
var key = isArray ? OpenApiSchemaKeywords.MinItemsKeyword : OpenApiSchemaKeywords.MinLengthKeyword;
schema[key] = minLengthAttribute.Length;
}
else if (attribute is LengthAttribute lengthAttribute)
{
var isArray = MapJsonNodeToSchemaType(schema[OpenApiSchemaKeywords.TypeKeyword]) is { } schemaTypes && schemaTypes.HasFlag(JsonSchemaType.Array);
var targetKeySuffix = isArray ? "Items" : "Length";
schema[$"min{targetKeySuffix}"] = lengthAttribute.MinimumLength;
schema[$"max{targetKeySuffix}"] = lengthAttribute.MaximumLength;
}
else if (attribute is UrlAttribute)
{
schema[OpenApiSchemaKeywords.TypeKeyword] = JsonSchemaType.String.ToString();
schema[OpenApiSchemaKeywords.FormatKeyword] = "uri";
}
else if (attribute is StringLengthAttribute stringLengthAttribute)
{
schema[OpenApiSchemaKeywords.MinLengthKeyword] = stringLengthAttribute.MinimumLength;
schema[OpenApiSchemaKeywords.MaxLengthKeyword] = stringLengthAttribute.MaximumLength;
}
}
}
/// <summary>
/// Populate the default value into the current schema.
/// </summary>
/// <param name="schema">The <see cref="JsonNode"/> produced by the underlying schema generator.</param>
/// <param name="defaultValue">An object representing the <see cref="object"/> associated with the default value.</param>
/// <param name="jsonTypeInfo">The <see cref="JsonTypeInfo"/> associated with the target type.</param>
internal static void ApplyDefaultValue(this JsonNode schema, object? defaultValue, JsonTypeInfo? jsonTypeInfo)
{
if (jsonTypeInfo is null)
{
return;
}
if (defaultValue is null)
{
schema[OpenApiSchemaKeywords.DefaultKeyword] = null;
}
else
{
schema[OpenApiSchemaKeywords.DefaultKeyword] = JsonSerializer.SerializeToNode(defaultValue, jsonTypeInfo);
}
}
/// <summary>
/// Applies the primitive types and formats to the schema based on the type.
/// </summary>
/// <remarks>
/// OpenAPI v3 requires support for the format keyword in generated types. Because the
/// underlying schema generator does not support this, we need to manually apply the
/// supported formats to the schemas associated with the generated type.
///
/// Whereas JsonSchema represents nullable types via `type: ["string", "null"]`, OpenAPI
/// v3 exposes a nullable property on the schema. This method will set the nullable property
/// based on whether the underlying schema generator returned an array type containing "null" to
/// represent a nullable type or if the type was denoted as nullable from our lookup cache.
///
/// Note that this method targets <see cref="JsonNode"/> and not <see cref="OpenApiSchema"/> because
/// it is is designed to be invoked via the `OnGenerated` callback in the underlying schema generator as
/// opposed to after the generated schemas have been mapped to OpenAPI schemas.
/// </remarks>
/// <param name="schema">The <see cref="JsonNode"/> produced by the underlying schema generator.</param>
/// <param name="context">The <see cref="JsonSchemaExporterContext"/> associated with the <see paramref="schema"/>.</param>
/// <param name="createSchemaReferenceId">A delegate that generates the reference ID to create for a type.</param>
internal static void ApplyPrimitiveTypesAndFormats(this JsonNode schema, JsonSchemaExporterContext context, Func<JsonTypeInfo, string?> createSchemaReferenceId)
{
var type = context.TypeInfo.Type;
var underlyingType = Nullable.GetUnderlyingType(type);
if (_simpleTypeToOpenApiSchema.TryGetValue(underlyingType ?? type, out var openApiSchema))
{
if (underlyingType != null && MapJsonNodeToSchemaType(schema[OpenApiSchemaKeywords.TypeKeyword]) is { } schemaTypes &&
!schemaTypes.HasFlag(JsonSchemaType.Null))
{
schema[OpenApiSchemaKeywords.TypeKeyword] = (schemaTypes | JsonSchemaType.Null).ToString();
}
schema[OpenApiSchemaKeywords.FormatKeyword] = openApiSchema.Format;
schema[OpenApiConstants.SchemaId] = createSchemaReferenceId(context.TypeInfo);
}
}
/// <summary>
/// Applies route constraints to the target schema.
/// </summary>
/// <param name="schema">The <see cref="JsonNode"/> produced by the underlying schema generator.</param>
/// <param name="constraints">The list of <see cref="IRouteConstraint"/>s associated with the route parameter.</param>
internal static void ApplyRouteConstraints(this JsonNode schema, IEnumerable<IRouteConstraint> constraints)
{
// Apply constraints in reverse order because when it comes to the routing
// layer the first constraint that is violated causes routing to short circuit.
foreach (var constraint in Enumerable.Reverse(constraints))
{
if (constraint is MinRouteConstraint minRouteConstraint)
{
schema[OpenApiSchemaKeywords.MinimumKeyword] = minRouteConstraint.Min;
}
else if (constraint is MaxRouteConstraint maxRouteConstraint)
{
schema[OpenApiSchemaKeywords.MaximumKeyword] = maxRouteConstraint.Max;
}
else if (constraint is MinLengthRouteConstraint minLengthRouteConstraint)
{
schema[OpenApiSchemaKeywords.MinLengthKeyword] = minLengthRouteConstraint.MinLength;
}
else if (constraint is MaxLengthRouteConstraint maxLengthRouteConstraint)
{
schema[OpenApiSchemaKeywords.MaxLengthKeyword] = maxLengthRouteConstraint.MaxLength;
}
else if (constraint is RangeRouteConstraint rangeRouteConstraint)
{
schema[OpenApiSchemaKeywords.MinimumKeyword] = rangeRouteConstraint.Min;
schema[OpenApiSchemaKeywords.MaximumKeyword] = rangeRouteConstraint.Max;
}
else if (constraint is RegexRouteConstraint regexRouteConstraint)
{
schema[OpenApiSchemaKeywords.TypeKeyword] = JsonSchemaType.String.ToString();
schema[OpenApiSchemaKeywords.FormatKeyword] = null;
schema[OpenApiSchemaKeywords.PatternKeyword] = regexRouteConstraint.Constraint.ToString();
}
else if (constraint is LengthRouteConstraint lengthRouteConstraint)
{
schema[OpenApiSchemaKeywords.MinLengthKeyword] = lengthRouteConstraint.MinLength;
schema[OpenApiSchemaKeywords.MaxLengthKeyword] = lengthRouteConstraint.MaxLength;
}
else if (constraint is FloatRouteConstraint or DecimalRouteConstraint or DoubleRouteConstraint)
{
schema[OpenApiSchemaKeywords.TypeKeyword] = JsonSchemaType.Number.ToString();
schema[OpenApiSchemaKeywords.FormatKeyword] = constraint is FloatRouteConstraint ? "float" : "double";
}
else if (constraint is LongRouteConstraint or IntRouteConstraint)
{
schema[OpenApiSchemaKeywords.TypeKeyword] = JsonSchemaType.Integer.ToString();
schema[OpenApiSchemaKeywords.FormatKeyword] = constraint is LongRouteConstraint ? "int64" : "int32";
}
else if (constraint is GuidRouteConstraint or StringRouteConstraint)
{
schema[OpenApiSchemaKeywords.TypeKeyword] = JsonSchemaType.String.ToString();
schema[OpenApiSchemaKeywords.FormatKeyword] = constraint is GuidRouteConstraint ? "uuid" : null;
}
else if (constraint is BoolRouteConstraint)
{
schema[OpenApiSchemaKeywords.TypeKeyword] = JsonSchemaType.Boolean.ToString();
schema[OpenApiSchemaKeywords.FormatKeyword] = null;
}
else if (constraint is AlphaRouteConstraint)
{
schema[OpenApiSchemaKeywords.TypeKeyword] = JsonSchemaType.String.ToString();
schema[OpenApiSchemaKeywords.FormatKeyword] = null;
}
else if (constraint is DateTimeRouteConstraint)
{
schema[OpenApiSchemaKeywords.TypeKeyword] = JsonSchemaType.String.ToString();
schema[OpenApiSchemaKeywords.FormatKeyword] = "date-time";
}
}
}
/// <summary>
/// Applies parameter-specific customizations to the target schema.
/// </summary>
/// <param name="schema">The <see cref="JsonNode"/> produced by the underlying schema generator.</param>
/// <param name="parameterDescription">The <see cref="ApiParameterDescription"/> associated with the <see paramref="schema"/>.</param>
/// <param name="jsonTypeInfo">The <see cref="JsonTypeInfo"/> associated with the <see paramref="schema"/>.</param>
internal static void ApplyParameterInfo(this JsonNode schema, ApiParameterDescription parameterDescription, JsonTypeInfo? jsonTypeInfo)
{
// This is special handling for parameters that are not bound from the body but represented in a complex type.
// For example:
//
// public class MyArgs
// {
// [Required]
// [Range(1, 10)]
// [FromQuery]
// public string Name { get; set; }
// }
//
// public IActionResult(MyArgs myArgs) { }
//
// In this case, the `ApiParameterDescription` object that we received will represent the `Name` property
// based on our model binding heuristics. In that case, to access the validation attributes that the
// model binder will respect we will need to get the property from the container type and map the
// attributes on it to the schema.
if (parameterDescription.ModelMetadata is { PropertyName: { }, ContainerType: { }, HasValidators: true, ValidatorMetadata: { } validations })
{
var attributes = validations.OfType<ValidationAttribute>();
schema.ApplyValidationAttributes(attributes);
}
if (parameterDescription.ParameterDescriptor is IParameterInfoParameterDescriptor { ParameterInfo: { } parameterInfo })
{
if (parameterInfo.HasDefaultValue)
{
schema.ApplyDefaultValue(parameterInfo.DefaultValue, jsonTypeInfo);
}
else if (parameterInfo.GetCustomAttributes<DefaultValueAttribute>().LastOrDefault() is { } defaultValueAttribute)
{
schema.ApplyDefaultValue(defaultValueAttribute.Value, jsonTypeInfo);
}
if (parameterInfo.GetCustomAttributes().OfType<ValidationAttribute>() is { } validationAttributes)
{
schema.ApplyValidationAttributes(validationAttributes);
}
schema.ApplyNullabilityContextInfo(parameterInfo);
}
// Route constraints are only defined on parameters that are sourced from the path. Since
// they are encoded in the route template, and not in the type information based to the underlying
// schema generator we have to handle them separately here.
if (parameterDescription.RouteInfo?.Constraints is { } constraints)
{
schema.ApplyRouteConstraints(constraints);
}
if (parameterDescription.Source is { } bindingSource
&& SupportsNullableProperty(bindingSource)
&& MapJsonNodeToSchemaType(schema[OpenApiSchemaKeywords.TypeKeyword]) is { } schemaTypes &&
schemaTypes.HasFlag(JsonSchemaType.Null))
{
schema[OpenApiSchemaKeywords.TypeKeyword] = (schemaTypes & ~JsonSchemaType.Null).ToString();
}
// Parameters sourced from the header, query, route, and/or form cannot be nullable based on our binding
// rules but can be optional.
static bool SupportsNullableProperty(BindingSource bindingSource) => bindingSource == BindingSource.Header
|| bindingSource == BindingSource.Query
|| bindingSource == BindingSource.Path
|| bindingSource == BindingSource.Form
|| bindingSource == BindingSource.FormFile;
}
/// <summary>
/// Applies the polymorphism options defined by System.Text.Json to the target schema following OpenAPI v3's
/// conventions for the discriminator property.
/// </summary>
/// <param name="schema">The <see cref="JsonNode"/> produced by the underlying schema generator.</param>
/// <param name="context">The <see cref="JsonSchemaExporterContext"/> associated with the current type.</param>
/// <param name="createSchemaReferenceId">A delegate that generates the reference ID to create for a type.</param>
internal static void MapPolymorphismOptionsToDiscriminator(this JsonNode schema, JsonSchemaExporterContext context, Func<JsonTypeInfo, string?> createSchemaReferenceId)
{
// The `context.BaseTypeInfo == null` check is used to ensure that we only apply the polymorphism options
// to the top-level schema and not to any nested schemas that are generated.
if (context.TypeInfo.PolymorphismOptions is { } polymorphismOptions && context.BaseTypeInfo == null)
{
// System.Text.Json supports serializing to a non-abstract base class if no discriminator is provided.
// OpenAPI requires that all polymorphic sub-schemas have an associated discriminator. If the base type
// doesn't declare itself as its own derived type via [JsonDerived], then it can't have a discriminator,
// which OpenAPI requires. In that case, we exit early to avoid mapping the polymorphism options
// to the `discriminator` property and return an un-discriminated `anyOf` schema instead.
if (IsNonAbstractTypeWithoutDerivedTypeReference(context))
{
return;
}
var mappings = new JsonObject();
foreach (var derivedType in polymorphismOptions.DerivedTypes)
{
if (derivedType.TypeDiscriminator is { } discriminator)
{
var jsonDerivedType = context.TypeInfo.Options.GetTypeInfo(derivedType.DerivedType);
// Discriminator mappings are only supported in OpenAPI v3+ so we can safely assume that
// the generated reference mappings will support the OpenAPI v3 schema reference format
// that we hardcode here. We could use `OpenApiReference` to construct the reference and
// serialize it but we use a hardcoded string here to avoid allocating a new object and
// working around Microsoft.OpenApi's serialization libraries.
mappings[$"{discriminator}"] = $"{createSchemaReferenceId(context.TypeInfo)}{createSchemaReferenceId(jsonDerivedType)}";
}
}
schema[OpenApiSchemaKeywords.DiscriminatorKeyword] = polymorphismOptions.TypeDiscriminatorPropertyName;
schema[OpenApiSchemaKeywords.DiscriminatorMappingKeyword] = mappings;
}
}
/// <summary>
/// Set the x-schema-id property on the schema to the identifier associated with the type.
/// </summary>
/// <param name="schema">The <see cref="JsonNode"/> produced by the underlying schema generator.</param>
/// <param name="context">The <see cref="JsonSchemaExporterContext"/> associated with the current type.</param>
/// <param name="createSchemaReferenceId">A delegate that generates the reference ID to create for a type.</param>
internal static void ApplySchemaReferenceId(this JsonNode schema, JsonSchemaExporterContext context, Func<JsonTypeInfo, string?> createSchemaReferenceId)
{
if (createSchemaReferenceId(context.TypeInfo) is { } schemaReferenceId)
{
schema[OpenApiConstants.SchemaId] = schemaReferenceId;
}
// If the type is a non-abstract base class that is not one of the derived types then mark it as a base schema.
if (context.BaseTypeInfo == context.TypeInfo &&
IsNonAbstractTypeWithoutDerivedTypeReference(context))
{
schema[OpenApiConstants.SchemaId] = "Base";
}
}
/// <summary>
/// Returns <langword ref="true" /> if the current type is a non-abstract base class that is not defined as its
/// own derived type.
/// </summary>
/// <param name="context">The <see cref="JsonSchemaExporterContext"/> associated with the current type.</param>
private static bool IsNonAbstractTypeWithoutDerivedTypeReference(JsonSchemaExporterContext context)
{
return !context.TypeInfo.Type.IsAbstract
&& context.TypeInfo.PolymorphismOptions is { } polymorphismOptions
&& !polymorphismOptions.DerivedTypes.Any(type => type.DerivedType == context.TypeInfo.Type);
}
/// <summary>
/// Support applying nullability status for reference types provided as a parameter.
/// </summary>
/// <param name="schema">The <see cref="JsonNode"/> produced by the underlying schema generator.</param>
/// <param name="parameterInfo">The <see cref="ParameterInfo" /> associated with the schema.</param>
internal static void ApplyNullabilityContextInfo(this JsonNode schema, ParameterInfo parameterInfo)
{
if (parameterInfo.ParameterType.IsValueType)
{
return;
}
var nullabilityInfoContext = new NullabilityInfoContext();
var nullabilityInfo = nullabilityInfoContext.Create(parameterInfo);
if (nullabilityInfo.WriteState == NullabilityState.Nullable
&& MapJsonNodeToSchemaType(schema[OpenApiSchemaKeywords.TypeKeyword]) is { } schemaTypes
&& !schemaTypes.HasFlag(JsonSchemaType.Null))
{
schema[OpenApiSchemaKeywords.TypeKeyword] = (schemaTypes | JsonSchemaType.Null).ToString();
}
}
/// <summary>
/// Support applying nullability status for reference types provided as a property or field.
/// </summary>
/// <param name="schema">The <see cref="JsonNode"/> produced by the underlying schema generator.</param>
/// <param name="propertyInfo">The <see cref="JsonPropertyInfo" /> associated with the schema.</param>
internal static void ApplyNullabilityContextInfo(this JsonNode schema, JsonPropertyInfo propertyInfo)
{
// Avoid setting explicit nullability annotations for `object` types so they continue to match on the catch
// all schema (no type, no format, no constraints).
if (propertyInfo.PropertyType != typeof(object) && (propertyInfo.IsGetNullable || propertyInfo.IsSetNullable))
{
if (MapJsonNodeToSchemaType(schema[OpenApiSchemaKeywords.TypeKeyword]) is { } schemaTypes &&
!schemaTypes.HasFlag(JsonSchemaType.Null))
{
schema[OpenApiSchemaKeywords.TypeKeyword] = (schemaTypes | JsonSchemaType.Null).ToString();
}
}
}
private static JsonSchemaType? MapJsonNodeToSchemaType(JsonNode? jsonNode)
{
if (jsonNode is not JsonArray jsonArray)
{
if (Enum.TryParse<JsonSchemaType>(jsonNode?.GetValue<string>(), true, out var openApiSchemaType))
{
return openApiSchemaType;
}
return jsonNode is JsonValue jsonValue && jsonValue.TryGetValue<string>(out var identifier)
? ToSchemaType(identifier)
: null;
}
JsonSchemaType? schemaType = null;
foreach (var node in jsonArray)
{
if (node is JsonValue jsonValue && jsonValue.TryGetValue<string>(out var identifier))
{
var type = ToSchemaType(identifier);
schemaType = schemaType.HasValue ? (schemaType | type) : type;
}
}
return schemaType;
static JsonSchemaType ToSchemaType(string identifier)
{
return identifier.ToLowerInvariant() switch
{
"null" => JsonSchemaType.Null,
"boolean" => JsonSchemaType.Boolean,
"integer" => JsonSchemaType.Integer,
"number" => JsonSchemaType.Number,
"string" => JsonSchemaType.String,
"array" => JsonSchemaType.Array,
"object" => JsonSchemaType.Object,
_ => throw new InvalidOperationException($"Unknown schema type: {identifier}"),
};
}
}
}