| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | using Microsoft.OpenApi; |
| | | 3 | | |
| | | 4 | | namespace Kestrun.OpenApi; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Utility class to generate operation IDs for OpenAPI callbacks. |
| | | 8 | | /// </summary> |
| | | 9 | | internal static partial class CallbackOperationId |
| | | 10 | | { |
| | 0 | 11 | | private static readonly Regex NonIdChars = NonAlphanumericCharsRegex(); |
| | 0 | 12 | | private static readonly Regex MultiUnderscore = MultipleUnderscoresRegex(); |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Generates a standardized operation ID for an OpenAPI callback based on the callback name, HTTP verb, and route p |
| | | 16 | | /// </summary> |
| | | 17 | | /// <param name="callbackName">The name of the callback.</param> |
| | | 18 | | /// <param name="httpVerb">The HTTP verb associated with the callback.</param> |
| | | 19 | | /// <param name="pattern">The route pattern for the callback.</param> |
| | | 20 | | /// <returns>A standardized operation ID string.</returns> |
| | | 21 | | /// <exception cref="ArgumentException">Thrown when any of the input parameters are null or whitespace.</exception> |
| | | 22 | | internal static string From(string callbackName, string httpVerb, string pattern) |
| | | 23 | | { |
| | 0 | 24 | | if (string.IsNullOrWhiteSpace(callbackName)) |
| | | 25 | | { |
| | 0 | 26 | | throw new ArgumentException("callbackName is required", nameof(callbackName)); |
| | | 27 | | } |
| | | 28 | | |
| | 0 | 29 | | if (string.IsNullOrWhiteSpace(httpVerb)) |
| | | 30 | | { |
| | 0 | 31 | | throw new ArgumentException("httpVerb is required", nameof(httpVerb)); |
| | | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | if (string.IsNullOrWhiteSpace(pattern)) |
| | | 35 | | { |
| | 0 | 36 | | throw new ArgumentException("pattern is required", nameof(pattern)); |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | var verb = httpVerb.Trim().ToLowerInvariant(); |
| | | 40 | | |
| | | 41 | | // Example: "/order/status/{orderId}" -> "order_status_orderId" |
| | 0 | 42 | | var suffix = pattern.Trim(); |
| | 0 | 43 | | suffix = suffix.StartsWith('/') ? suffix[1..] : suffix; |
| | 0 | 44 | | suffix = suffix.Replace('/', '_') |
| | 0 | 45 | | .Replace("{", "") |
| | 0 | 46 | | .Replace("}", ""); |
| | | 47 | | |
| | 0 | 48 | | suffix = NonIdChars.Replace(suffix, "_"); |
| | 0 | 49 | | suffix = MultiUnderscore.Replace(suffix, "_").Trim('_'); |
| | | 50 | | |
| | | 51 | | // If you want *exactly* "...__status" for "/order/status/{orderId}", |
| | | 52 | | // you can optionally take a specific segment (see helper below). |
| | 0 | 53 | | return $"{callbackName}__{verb}__{suffix}"; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Generates a standardized operation ID for an OpenAPI callback based on the callback name, HTTP verb, and route p |
| | | 58 | | /// using only the last non-parameter segment of the route pattern. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <param name="callbackName">The name of the callback.</param> |
| | | 61 | | /// <param name="httpVerb">The HTTP verb associated with the callback.</param> |
| | | 62 | | /// <param name="pattern">The route pattern for the callback.</param> |
| | | 63 | | /// <returns>A standardized operation ID string.</returns> |
| | | 64 | | internal static string FromLastSegment(string callbackName, string httpVerb, string pattern) |
| | | 65 | | { |
| | 0 | 66 | | if (string.IsNullOrWhiteSpace(callbackName)) |
| | | 67 | | { |
| | 0 | 68 | | throw new ArgumentException("callbackName is required", nameof(callbackName)); |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | if (string.IsNullOrWhiteSpace(httpVerb)) |
| | | 72 | | { |
| | 0 | 73 | | throw new ArgumentException("httpVerb is required", nameof(httpVerb)); |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | if (string.IsNullOrWhiteSpace(pattern)) |
| | | 77 | | { |
| | 0 | 78 | | throw new ArgumentException("pattern is required", nameof(pattern)); |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | var verb = httpVerb.Trim().ToLowerInvariant(); |
| | 0 | 82 | | var parts = pattern.Trim('/').Split('/', StringSplitOptions.RemoveEmptyEntries); |
| | | 83 | | |
| | 0 | 84 | | var last = "op"; |
| | 0 | 85 | | if (parts.Length > 0) |
| | | 86 | | { |
| | 0 | 87 | | last = parts[^1]; |
| | 0 | 88 | | for (var i = parts.Length - 1; i >= 0; i--) |
| | | 89 | | { |
| | 0 | 90 | | if (!parts[i].StartsWith('{')) |
| | | 91 | | { |
| | 0 | 92 | | last = parts[i]; |
| | 0 | 93 | | break; |
| | | 94 | | } |
| | | 95 | | } |
| | | 96 | | } |
| | | 97 | | |
| | 0 | 98 | | last = last.Trim('{', '}'); |
| | 0 | 99 | | last = NonIdChars.Replace(last, "_"); |
| | 0 | 100 | | last = MultiUnderscore.Replace(last, "_").Trim('_'); |
| | | 101 | | |
| | 0 | 102 | | return $"{callbackName}__{verb}__{last}"; |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Builds the callback key from the runtime expression and the pattern. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <param name="expression">The runtime expression for the callback.</param> |
| | | 109 | | /// <param name="pattern">The route pattern for the callback.</param> |
| | | 110 | | /// <returns>A combined callback key as a RuntimeExpression.</returns> |
| | | 111 | | /// <exception cref="ArgumentException">Thrown when any of the input parameters are null or whitespace.</exception> |
| | | 112 | | internal static RuntimeExpression BuildCallbackKey(string expression, string pattern) |
| | | 113 | | { |
| | 0 | 114 | | if (string.IsNullOrWhiteSpace(expression)) |
| | | 115 | | { |
| | 0 | 116 | | throw new ArgumentException("expression required", nameof(expression)); |
| | | 117 | | } |
| | | 118 | | |
| | 0 | 119 | | if (string.IsNullOrWhiteSpace(pattern)) |
| | | 120 | | { |
| | 0 | 121 | | throw new ArgumentException("pattern required", nameof(pattern)); |
| | | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | expression = expression.Trim(); |
| | 0 | 125 | | pattern = pattern.Trim(); |
| | | 126 | | |
| | | 127 | | // Normalize expression into braced form: {$request...} |
| | 0 | 128 | | if (expression.StartsWith('{')) |
| | | 129 | | { |
| | | 130 | | // If user gave "{...}", ensure the inner begins with '$' |
| | | 131 | | // Example: "{request.body#/x}" -> "{$request.body#/x}" |
| | 0 | 132 | | if (expression.Length >= 2 && expression[1] != '$') |
| | | 133 | | { |
| | | 134 | | // Prefer to explicitly preserve the closing brace, if present. |
| | | 135 | | // Well-formed case: "{inner}" -> "{$inner}" |
| | 0 | 136 | | if (expression[^1] == '}') |
| | | 137 | | { |
| | 0 | 138 | | var inner = expression.Length > 2 ? expression[1..^1] : string.Empty; |
| | 0 | 139 | | expression = "{$" + inner + "}"; |
| | | 140 | | } |
| | | 141 | | else |
| | | 142 | | { |
| | | 143 | | // Malformed case without trailing '}' – fall back to prior behavior |
| | 0 | 144 | | expression = "{$" + expression[1..]; |
| | | 145 | | } |
| | | 146 | | } |
| | | 147 | | } |
| | | 148 | | else |
| | | 149 | | { |
| | | 150 | | // If user gave "$request..." or "request...", normalize to "$request..." |
| | 0 | 151 | | if (!expression.StartsWith('$')) |
| | | 152 | | { |
| | 0 | 153 | | expression = "$" + expression; |
| | | 154 | | } |
| | | 155 | | |
| | 0 | 156 | | expression = "{" + expression + "}"; |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | // Ensure pattern starts with '/' |
| | 0 | 160 | | if (!pattern.StartsWith('/')) |
| | | 161 | | { |
| | 0 | 162 | | pattern = "/" + pattern; |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | // If expression ends with '/', avoid "//" |
| | 0 | 166 | | if (expression.EndsWith('/') && pattern.Length > 0 && pattern[0] == '/') |
| | | 167 | | { |
| | 0 | 168 | | pattern = pattern[1..]; |
| | | 169 | | } |
| | | 170 | | |
| | 0 | 171 | | return RuntimeExpression.Build(expression + pattern); |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | [GeneratedRegex(@"[^A-Za-z0-9_]+", RegexOptions.Compiled)] |
| | | 175 | | private static partial Regex NonAlphanumericCharsRegex(); |
| | | 176 | | [GeneratedRegex(@"_+", RegexOptions.Compiled)] |
| | | 177 | | private static partial Regex MultipleUnderscoresRegex(); |
| | | 178 | | } |