| | | 1 | | using System.Text; |
| | | 2 | | |
| | | 3 | | namespace Kestrun.OpenApi; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents the result of mapping an RFC6570 path template to a Kestrel route pattern. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <param name="OpenApiPattern">The OpenAPI path pattern (query expressions removed).</param> |
| | | 9 | | /// <param name="KestrelPattern">The Kestrel route pattern with multi-segment parameters expanded.</param> |
| | | 10 | | /// <param name="QueryParameters">Query parameter names extracted from RFC6570 query expressions.</param> |
| | 10 | 11 | | public sealed record Rfc6570PathMapping( |
| | 6 | 12 | | string OpenApiPattern, |
| | 6 | 13 | | string KestrelPattern, |
| | 15 | 14 | | IReadOnlyList<string> QueryParameters); |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Maps OpenAPI 3.2 RFC6570 path templates to Kestrel-compatible route patterns. |
| | | 18 | | /// </summary> |
| | | 19 | | public static class Rfc6570PathTemplateMapper |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Attempts to map an RFC6570 path template to a Kestrel route pattern. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="openApiTemplate">The OpenAPI path template (RFC6570).</param> |
| | | 25 | | /// <param name="mapping">The resulting mapping for OpenAPI and Kestrel patterns.</param> |
| | | 26 | | /// <param name="error">The error message when mapping fails.</param> |
| | | 27 | | /// <returns>True when mapping succeeds; otherwise false.</returns> |
| | | 28 | | public static bool TryMapToKestrelRoute( |
| | | 29 | | string openApiTemplate, |
| | | 30 | | out Rfc6570PathMapping mapping, |
| | | 31 | | out string? error) |
| | | 32 | | { |
| | | 33 | | mapping = new Rfc6570PathMapping(string.Empty, string.Empty, []); |
| | | 34 | | error = null; |
| | | 35 | | |
| | | 36 | | if (string.IsNullOrWhiteSpace(openApiTemplate)) |
| | | 37 | | { |
| | | 38 | | error = "OpenAPI path template is null or empty."; |
| | | 39 | | return false; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | var openApiBuilder = new StringBuilder(openApiTemplate.Length); |
| | | 43 | | var kestrelBuilder = new StringBuilder(openApiTemplate.Length); |
| | | 44 | | var queryParameters = new List<string>(); |
| | | 45 | | |
| | | 46 | | for (var i = 0; i < openApiTemplate.Length; i++) |
| | | 47 | | { |
| | | 48 | | var ch = openApiTemplate[i]; |
| | | 49 | | if (ch != '{') |
| | | 50 | | { |
| | | 51 | | _ = openApiBuilder.Append(ch); |
| | | 52 | | _ = kestrelBuilder.Append(ch); |
| | | 53 | | continue; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | var close = openApiTemplate.IndexOf('}', i + 1); |
| | | 57 | | if (close < 0) |
| | | 58 | | { |
| | | 59 | | error = "Unterminated RFC6570 expression: missing '}'."; |
| | | 60 | | return false; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | var expression = openApiTemplate.Substring(i + 1, close - i - 1).Trim(); |
| | | 64 | | if (expression.Length == 0) |
| | | 65 | | { |
| | | 66 | | error = "Empty RFC6570 expression '{}' is not supported."; |
| | | 67 | | return false; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | if (!TryHandleExpression(expression, openApiBuilder, kestrelBuilder, queryParameters, out error)) |
| | | 71 | | { |
| | | 72 | | return false; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | i = close; |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | var openApiPattern = openApiBuilder.ToString(); |
| | | 79 | | if (string.IsNullOrWhiteSpace(openApiPattern)) |
| | | 80 | | { |
| | | 81 | | error = "OpenAPI path template resolved to an empty path."; |
| | | 82 | | return false; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | mapping = new Rfc6570PathMapping(openApiPattern, kestrelBuilder.ToString(), queryParameters); |
| | | 86 | | return true; |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Processes a single RFC6570 expression and appends to the OpenAPI and Kestrel builders. |
| | | 91 | | /// </summary> |
| | | 92 | | /// <param name="expression">The RFC6570 expression content (without braces).</param> |
| | | 93 | | /// <param name="openApiBuilder">Builder for OpenAPI path pattern.</param> |
| | | 94 | | /// <param name="kestrelBuilder">Builder for Kestrel route pattern.</param> |
| | | 95 | | /// <param name="queryParameters">List to collect query parameter names.</param> |
| | | 96 | | /// <param name="error">Error message when processing fails.</param> |
| | | 97 | | /// <returns>True on success; otherwise false.</returns> |
| | | 98 | | private static bool TryHandleExpression( |
| | | 99 | | string expression, |
| | | 100 | | StringBuilder openApiBuilder, |
| | | 101 | | StringBuilder kestrelBuilder, |
| | | 102 | | List<string> queryParameters, |
| | | 103 | | out string? error) |
| | | 104 | | { |
| | | 105 | | if (expression[0] == '#') |
| | | 106 | | { |
| | | 107 | | error = "RFC6570 fragment expressions ('#') are not supported in OpenAPI path templates."; |
| | | 108 | | return false; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | if (expression[0] is '?' or '&') |
| | | 112 | | { |
| | | 113 | | var queryExpr = expression[1..]; |
| | | 114 | | return TryParseQueryExpression(queryExpr, queryParameters, out error); |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | if (!TryParsePathExpression(expression, out var parsed, out error)) |
| | | 118 | | { |
| | | 119 | | return false; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | _ = openApiBuilder.Append(BuildOpenApiExpression(parsed)); |
| | | 123 | | _ = kestrelBuilder.Append(BuildKestrelExpression(parsed)); |
| | | 124 | | return true; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Parses a query expression (e.g. "id,filter") and appends parameter names. |
| | | 129 | | /// </summary> |
| | | 130 | | /// <param name="expression">The query expression content.</param> |
| | | 131 | | /// <param name="queryParameters">List to collect parameter names.</param> |
| | | 132 | | /// <param name="error">Error message when parsing fails.</param> |
| | | 133 | | /// <returns>True on success; otherwise false.</returns> |
| | | 134 | | private static bool TryParseQueryExpression( |
| | | 135 | | string expression, |
| | | 136 | | List<string> queryParameters, |
| | | 137 | | out string? error) |
| | | 138 | | { |
| | | 139 | | error = null; |
| | | 140 | | |
| | | 141 | | if (string.IsNullOrWhiteSpace(expression)) |
| | | 142 | | { |
| | | 143 | | error = "RFC6570 query expression must include at least one variable."; |
| | | 144 | | return false; |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | foreach (var raw in expression.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries |
| | | 148 | | { |
| | | 149 | | var name = raw.EndsWith('*') ? raw[..^1] : raw; |
| | | 150 | | if (!IsValidVarName(name)) |
| | | 151 | | { |
| | | 152 | | error = $"Invalid RFC6570 variable name '{name}' in query expression."; |
| | | 153 | | return false; |
| | | 154 | | } |
| | | 155 | | queryParameters.Add(name); |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | return true; |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Parses a path expression like {id}, {+path}, {path*}. |
| | | 163 | | /// </summary> |
| | | 164 | | /// <param name="expression">The expression content.</param> |
| | | 165 | | /// <param name="parsed">The parsed expression details.</param> |
| | | 166 | | /// <param name="error">Error message when parsing fails.</param> |
| | | 167 | | /// <returns>True on success; otherwise false.</returns> |
| | | 168 | | private static bool TryParsePathExpression(string expression, out PathExpression parsed, out string? error) |
| | | 169 | | { |
| | | 170 | | parsed = new PathExpression(string.Empty, false, false); |
| | | 171 | | error = null; |
| | | 172 | | |
| | | 173 | | if (expression.Contains(':', StringComparison.Ordinal)) |
| | | 174 | | { |
| | | 175 | | error = "Regex/constraint syntax (':') is not supported in OpenAPI 3.2 RFC6570 templates."; |
| | | 176 | | return false; |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | if (expression.Contains(',', StringComparison.Ordinal)) |
| | | 180 | | { |
| | | 181 | | error = "Multiple variables in a single RFC6570 expression are not supported."; |
| | | 182 | | return false; |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | var isReserved = expression[0] == '+'; |
| | | 186 | | if (isReserved) |
| | | 187 | | { |
| | | 188 | | expression = expression[1..]; |
| | | 189 | | if (expression.Length == 0) |
| | | 190 | | { |
| | | 191 | | error = "Reserved RFC6570 expression '{+}' is not valid."; |
| | | 192 | | return false; |
| | | 193 | | } |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | var isExplode = expression.EndsWith('*'); |
| | | 197 | | if (isExplode) |
| | | 198 | | { |
| | | 199 | | expression = expression[..^1]; |
| | | 200 | | if (expression.Length == 0) |
| | | 201 | | { |
| | | 202 | | error = "Explode RFC6570 expression '{*}' is not valid."; |
| | | 203 | | return false; |
| | | 204 | | } |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | if (!IsValidVarName(expression)) |
| | | 208 | | { |
| | | 209 | | error = $"Invalid RFC6570 variable name '{expression}'."; |
| | | 210 | | return false; |
| | | 211 | | } |
| | | 212 | | |
| | | 213 | | parsed = new PathExpression(expression, isReserved, isExplode); |
| | | 214 | | return true; |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | /// <summary> |
| | | 218 | | /// Builds the OpenAPI path expression for a parsed variable. |
| | | 219 | | /// </summary> |
| | | 220 | | /// <param name="parsed">The parsed expression details.</param> |
| | | 221 | | /// <returns>The RFC6570 expression for OpenAPI.</returns> |
| | | 222 | | private static string BuildOpenApiExpression(PathExpression parsed) |
| | | 223 | | { |
| | | 224 | | var builder = new StringBuilder(); |
| | | 225 | | _ = builder.Append('{'); |
| | | 226 | | if (parsed.IsReserved) |
| | | 227 | | { |
| | | 228 | | _ = builder.Append('+'); |
| | | 229 | | } |
| | | 230 | | _ = builder.Append(parsed.Name); |
| | | 231 | | if (parsed.IsExplode) |
| | | 232 | | { |
| | | 233 | | _ = builder.Append('*'); |
| | | 234 | | } |
| | | 235 | | _ = builder.Append('}'); |
| | | 236 | | return builder.ToString(); |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | /// <summary> |
| | | 240 | | /// Builds the Kestrel route expression for a parsed variable. |
| | | 241 | | /// </summary> |
| | | 242 | | /// <param name="parsed">The parsed expression details.</param> |
| | | 243 | | /// <returns>The Kestrel route expression.</returns> |
| | | 244 | | private static string BuildKestrelExpression(PathExpression parsed) => parsed.IsMultiSegment ? $"{{**{parsed.Name}}} |
| | | 245 | | |
| | | 246 | | /// <summary> |
| | | 247 | | /// Validates RFC6570 variable names for the supported subset. |
| | | 248 | | /// </summary> |
| | | 249 | | /// <param name="name">The variable name.</param> |
| | | 250 | | /// <returns>True if the name is valid; otherwise false.</returns> |
| | | 251 | | private static bool IsValidVarName(string name) |
| | | 252 | | { |
| | | 253 | | if (string.IsNullOrWhiteSpace(name)) |
| | | 254 | | { |
| | | 255 | | return false; |
| | | 256 | | } |
| | | 257 | | |
| | | 258 | | foreach (var c in name) |
| | | 259 | | { |
| | | 260 | | var ok = char.IsLetterOrDigit(c) || c == '_' || c == '.' || c == '-'; |
| | | 261 | | if (!ok) |
| | | 262 | | { |
| | | 263 | | return false; |
| | | 264 | | } |
| | | 265 | | } |
| | | 266 | | |
| | | 267 | | return true; |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | /// <summary> |
| | | 271 | | /// Represents a parsed RFC6570 path expression. |
| | | 272 | | /// </summary> |
| | | 273 | | /// <param name="Name">The variable name.</param> |
| | | 274 | | /// <param name="IsReserved">True when the expression is reserved expansion.</param> |
| | | 275 | | /// <param name="IsExplode">True when the expression uses explode modifier.</param> |
| | | 276 | | private sealed record PathExpression(string Name, bool IsReserved, bool IsExplode) |
| | | 277 | | { |
| | | 278 | | /// <summary> |
| | | 279 | | /// Gets a value indicating whether the expression represents a multi-segment variable. |
| | | 280 | | /// </summary> |
| | | 281 | | public bool IsMultiSegment => IsReserved || IsExplode; |
| | | 282 | | } |
| | | 283 | | } |