| | | 1 | | using System.Globalization; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | |
| | | 4 | | /// <summary> |
| | | 5 | | /// Attribute to specify OpenAPI vendor extensions (x-*) on an API operation. |
| | | 6 | | /// </summary> |
| | | 7 | | [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = false, AllowMultiple = true)] |
| | | 8 | | public sealed class OpenApiExtensionAttribute : KestrunAnnotation |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// The extension name. Must start with "x-". |
| | | 12 | | /// </summary> |
| | 9 | 13 | | public string Name { get; set; } |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Raw JSON value for the extension. |
| | | 17 | | /// </summary> |
| | 6 | 18 | | public string Json { get; set; } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Creates an OpenAPI extension with no value. |
| | | 22 | | /// </summary> |
| | 0 | 23 | | public OpenApiExtensionAttribute() |
| | | 24 | | { |
| | | 25 | | // Default constructor |
| | 0 | 26 | | Name = string.Empty; |
| | 0 | 27 | | Json = "null"; |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Creates an OpenAPI extension. |
| | | 32 | | /// If <paramref name="value"/> is valid JSON, it is used as-is. |
| | | 33 | | /// Otherwise, it is treated as a string literal and JSON-encoded. |
| | | 34 | | /// </summary> |
| | 3 | 35 | | public OpenApiExtensionAttribute(string name, string value) |
| | | 36 | | { |
| | 3 | 37 | | ValidateName(name); |
| | 3 | 38 | | Name = name; |
| | | 39 | | |
| | 3 | 40 | | if (value is null) |
| | | 41 | | { |
| | 1 | 42 | | Json = "null"; |
| | 1 | 43 | | return; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | // Treat as string literal when not valid JSON |
| | 2 | 47 | | Json = IsValidJson(value) |
| | 2 | 48 | | ? value |
| | 2 | 49 | | : JsonSerializer.Serialize(value); |
| | 2 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Creates an OpenAPI extension with a boolean value. |
| | | 54 | | /// </summary> |
| | 0 | 55 | | public OpenApiExtensionAttribute(string name, bool value) |
| | | 56 | | { |
| | 0 | 57 | | ValidateName(name); |
| | 0 | 58 | | Name = name; |
| | 0 | 59 | | Json = value ? "true" : "false"; |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Creates an OpenAPI extension with an integer value. |
| | | 64 | | /// </summary> |
| | 0 | 65 | | public OpenApiExtensionAttribute(string name, int value) |
| | | 66 | | { |
| | 0 | 67 | | ValidateName(name); |
| | 0 | 68 | | Name = name; |
| | 0 | 69 | | Json = value.ToString(CultureInfo.InvariantCulture); |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Creates an OpenAPI extension with a numeric value. |
| | | 74 | | /// </summary> |
| | 0 | 75 | | public OpenApiExtensionAttribute(string name, double value) |
| | | 76 | | { |
| | 0 | 77 | | ValidateName(name); |
| | 0 | 78 | | Name = name; |
| | 0 | 79 | | Json = value.ToString(CultureInfo.InvariantCulture); |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Validates the extension name. |
| | | 84 | | /// </summary> |
| | | 85 | | /// <param name="name">The extension name to validate.</param> |
| | | 86 | | /// <exception cref="ArgumentException">Thrown when the extension name is null, empty, or does not start with "x-".< |
| | | 87 | | private static void ValidateName(string name) |
| | | 88 | | { |
| | 3 | 89 | | if (string.IsNullOrWhiteSpace(name)) |
| | | 90 | | { |
| | 0 | 91 | | throw new ArgumentException("Extension name cannot be null or empty.", nameof(name)); |
| | | 92 | | } |
| | | 93 | | |
| | 3 | 94 | | if (!name.StartsWith("x-", StringComparison.Ordinal)) |
| | | 95 | | { |
| | 0 | 96 | | throw new ArgumentException( |
| | 0 | 97 | | $"OpenAPI extension '{name}' is invalid. Extension names must start with 'x-'.", |
| | 0 | 98 | | nameof(name)); |
| | | 99 | | } |
| | 3 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Validates whether a string is valid JSON. |
| | | 104 | | /// </summary> |
| | | 105 | | /// <param name="value"> The string to validate as JSON. </param> |
| | | 106 | | /// <returns> True if the string is valid JSON; otherwise, false. </returns> |
| | | 107 | | private static bool IsValidJson(string value) |
| | | 108 | | { |
| | | 109 | | try |
| | | 110 | | { |
| | 2 | 111 | | using var _ = JsonDocument.Parse(value); |
| | 1 | 112 | | return true; |
| | | 113 | | } |
| | 1 | 114 | | catch (JsonException) |
| | | 115 | | { |
| | 1 | 116 | | return false; |
| | | 117 | | } |
| | 2 | 118 | | } |
| | | 119 | | } |
| | | 120 | | |