|
| 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.IO.Pipelines; |
| 5 | +using System.Linq; |
| 6 | +using System.Text.Json; |
| 7 | +using System.Text.Json.Serialization.Metadata; |
| 8 | +using Microsoft.AspNetCore.Http; |
| 9 | + |
| 10 | +namespace Microsoft.AspNetCore.OpenApi; |
| 11 | + |
| 12 | +internal static class JsonTypeInfoExtensions |
| 13 | +{ |
| 14 | + private static readonly Dictionary<Type, string> _simpleTypeToName = new() |
| 15 | + { |
| 16 | + [typeof(bool)] = "boolean", |
| 17 | + [typeof(byte)] = "byte", |
| 18 | + [typeof(int)] = "int", |
| 19 | + [typeof(uint)] = "uint", |
| 20 | + [typeof(long)] = "long", |
| 21 | + [typeof(ulong)] = "ulong", |
| 22 | + [typeof(short)] = "short", |
| 23 | + [typeof(ushort)] = "ushort", |
| 24 | + [typeof(float)] = "float", |
| 25 | + [typeof(double)] = "double", |
| 26 | + [typeof(decimal)] = "decimal", |
| 27 | + [typeof(DateTime)] = "DateTime", |
| 28 | + [typeof(DateTimeOffset)] = "DateTimeOffset", |
| 29 | + [typeof(Guid)] = "Guid", |
| 30 | + [typeof(char)] = "char", |
| 31 | + [typeof(Uri)] = "Uri", |
| 32 | + [typeof(string)] = "string", |
| 33 | + [typeof(IFormFile)] = "IFormFile", |
| 34 | + [typeof(IFormFileCollection)] = "IFormFileCollection", |
| 35 | + [typeof(PipeReader)] = "PipeReader", |
| 36 | + [typeof(Stream)] = "Stream" |
| 37 | + }; |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// The following method maps a JSON type to a schema reference ID that will eventually be used as the |
| 41 | + /// schema reference name in the OpenAPI document. These schema reference names are considered URL fragments |
| 42 | + /// in the context of JSON Schema's $ref keyword and must comply with the character restrictions of URL fragments. |
| 43 | + /// In particular, the generated strings can contain alphanumeric characters and a subset of special symbols. This |
| 44 | + /// means that certain symbols that appear commonly in .NET type names like ">" are not permitted in the |
| 45 | + /// generated reference ID. |
| 46 | + /// </summary> |
| 47 | + /// <param name="jsonTypeInfo">The <see cref="JsonTypeInfo"/> associated with the target schema.</param> |
| 48 | + /// <returns>The schema reference ID represented as a string name.</returns> |
| 49 | + internal static string GetSchemaReferenceId(this JsonTypeInfo jsonTypeInfo) |
| 50 | + { |
| 51 | + var type = jsonTypeInfo.Type; |
| 52 | + // Short-hand if the type we're generating a schema reference ID for is |
| 53 | + // one of the simple types defined above. |
| 54 | + if (_simpleTypeToName.TryGetValue(type, out var simpleName)) |
| 55 | + { |
| 56 | + return simpleName; |
| 57 | + } |
| 58 | + |
| 59 | + if (jsonTypeInfo is JsonTypeInfo { Kind: JsonTypeInfoKind.Enumerable, ElementType: { } elementType }) |
| 60 | + { |
| 61 | + var elementTypeInfo = jsonTypeInfo.Options.GetTypeInfo(elementType); |
| 62 | + return $"ArrayOf{elementTypeInfo.GetSchemaReferenceId()}"; |
| 63 | + } |
| 64 | + |
| 65 | + if (jsonTypeInfo is JsonTypeInfo { Kind: JsonTypeInfoKind.Dictionary, KeyType: { } keyType, ElementType: { } valueType }) |
| 66 | + { |
| 67 | + var keyTypeInfo = jsonTypeInfo.Options.GetTypeInfo(keyType); |
| 68 | + var valueTypeInfo = jsonTypeInfo.Options.GetTypeInfo(valueType); |
| 69 | + return $"DictionaryOf{keyTypeInfo.GetSchemaReferenceId()}And{valueTypeInfo.GetSchemaReferenceId()}"; |
| 70 | + } |
| 71 | + |
| 72 | + return type.GetSchemaReferenceId(jsonTypeInfo.Options); |
| 73 | + } |
| 74 | + |
| 75 | + internal static string GetSchemaReferenceId(this Type type, JsonSerializerOptions options) |
| 76 | + { |
| 77 | + // Check the simple types map first to account for the element types |
| 78 | + // of enumerables that have been processed above. |
| 79 | + if (_simpleTypeToName.TryGetValue(type, out var simpleName)) |
| 80 | + { |
| 81 | + return simpleName; |
| 82 | + } |
| 83 | + |
| 84 | + // Although arrays are enumerable types they are not encoded correctly |
| 85 | + // with JsonTypeInfoKind.Enumerable so we handle that here |
| 86 | + if (type.IsArray && type.GetElementType() is { } elementType) |
| 87 | + { |
| 88 | + var elementTypeInfo = options.GetTypeInfo(elementType); |
| 89 | + return $"ArrayOf{elementTypeInfo.GetSchemaReferenceId()}"; |
| 90 | + } |
| 91 | + |
| 92 | + // Special handling for anonymous types |
| 93 | + if (type.Name.StartsWith("<>f", StringComparison.Ordinal)) |
| 94 | + { |
| 95 | + var typeName = "AnonymousType"; |
| 96 | + var anonymousTypeProperties = type.GetGenericArguments(); |
| 97 | + var propertyNames = string.Join("And", anonymousTypeProperties.Select(p => p.GetSchemaReferenceId(options))); |
| 98 | + return $"{typeName}Of{propertyNames}"; |
| 99 | + } |
| 100 | + |
| 101 | + if (type.IsGenericType) |
| 102 | + { |
| 103 | + // Nullable types are suffixed with `?` (e.g. `Todo?`) |
| 104 | + if (type.GetGenericTypeDefinition() == typeof(Nullable<>) |
| 105 | + && Nullable.GetUnderlyingType(type) is { } underlyingType) |
| 106 | + { |
| 107 | + return $"{underlyingType.GetSchemaReferenceId(options)}?"; |
| 108 | + } |
| 109 | + // Special handling for generic types that are collections |
| 110 | + // Generic types become a concatenation of the generic type name and the type arguments |
| 111 | + else |
| 112 | + { |
| 113 | + var genericTypeName = type.Name[..type.Name.LastIndexOf('`')]; |
| 114 | + var genericArguments = type.GetGenericArguments(); |
| 115 | + var argumentNames = string.Join("And", genericArguments.Select(arg => arg.GetSchemaReferenceId(options))); |
| 116 | + return $"{genericTypeName}Of{argumentNames}"; |
| 117 | + } |
| 118 | + } |
| 119 | + return type.Name; |
| 120 | + } |
| 121 | +} |
0 commit comments