| | | 1 | | using Microsoft.OpenApi; |
| | | 2 | | |
| | | 3 | | namespace Kestrun.Callback; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents a plan for executing a callback operation. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <param name="CallbackId">The identifier for the callback</param> |
| | | 9 | | /// <param name="UrlTemplate">The URL template for the callback</param> |
| | | 10 | | /// <param name="Method"> The HTTP method for the callback</param> |
| | | 11 | | /// <param name="OperationId"> The operation identifier for the callback</param> |
| | | 12 | | /// <param name="PathParams"> The list of path parameters for the callback</param> |
| | | 13 | | /// <param name="Body">The body plan for the callback</param> |
| | | 14 | | public sealed record CallbackPlan( |
| | | 15 | | string CallbackId, // "paymentStatus" |
| | | 16 | | string UrlTemplate, // "{$request.body#/callbackUrls/status}/v1/payments/{paymentId}/status" |
| | | 17 | | HttpMethod Method, // POST |
| | | 18 | | string OperationId, // paymentStatusCallback__post__status |
| | | 19 | | IReadOnlyList<CallbackParamPlan> PathParams, |
| | | 20 | | CallbackBodyPlan? Body // schema info (ref) + media type |
| | | 21 | | ); |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Represents an execution plan for a callback, including resolved parameters. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="CallbackId">The identifier for the callback</param> |
| | | 27 | | /// <param name="Plan">The callback plan</param> |
| | | 28 | | /// <param name="BodyParameterName">The name of the body parameter, if any</param> |
| | | 29 | | /// <param name="Parameters">The resolved parameters for the callback</param> |
| | | 30 | | public sealed record CallBackExecutionPlan( |
| | | 31 | | string CallbackId, |
| | | 32 | | CallbackPlan Plan, |
| | | 33 | | string? BodyParameterName, |
| | | 34 | | Dictionary<string, object?> Parameters |
| | | 35 | | ); |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Represents a plan for a callback parameter. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="Name">The Parameter name</param> |
| | | 41 | | /// <param name="Location">The location of the parameter (e.g., "path")</param> |
| | | 42 | | public sealed record CallbackParamPlan( |
| | | 43 | | string Name, // "paymentId" |
| | | 44 | | string Location // "path" (keep string; or enum) |
| | | 45 | | |
| | | 46 | | ); |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Represents a plan for the body of a callback operation. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="MediaType">The media type of the callback body</param> |
| | 4 | 52 | | public sealed record CallbackBodyPlan( |
| | 3 | 53 | | string MediaType // "application/json" |
| | 4 | 54 | | ); |
| | | 55 | | internal static class CallbackPlanCompiler |
| | | 56 | | { |
| | | 57 | | internal static IReadOnlyList<CallbackPlan> Compile(OpenApiCallback callback, string callbackId) |
| | | 58 | | { |
| | | 59 | | ArgumentNullException.ThrowIfNull(callback); |
| | | 60 | | ArgumentNullException.ThrowIfNull(callbackId); |
| | | 61 | | |
| | | 62 | | var plans = new List<CallbackPlan>(); |
| | | 63 | | |
| | | 64 | | if (callback.PathItems is null || callback.PathItems.Count == 0) |
| | | 65 | | { |
| | | 66 | | return plans; |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | foreach (var (expr, pathItemIntf) in callback.PathItems) |
| | | 70 | | { |
| | | 71 | | if (pathItemIntf is not OpenApiPathItem pathItem || pathItem.Operations is null) |
| | | 72 | | { |
| | | 73 | | continue; |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | foreach (var (method, op) in pathItem.Operations) |
| | | 77 | | { |
| | | 78 | | if (op is null) |
| | | 79 | | { |
| | | 80 | | continue; |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | var pathParams = ExtractPathParams(op); |
| | | 84 | | var bodyPlan = ExtractBodyPlan(op); |
| | | 85 | | |
| | | 86 | | plans.Add(new CallbackPlan( |
| | | 87 | | CallbackId: callbackId, |
| | | 88 | | UrlTemplate: expr.Expression, |
| | | 89 | | Method: method, |
| | | 90 | | OperationId: op.OperationId ?? $"{callbackId}__{method.Method.ToLowerInvariant()}", |
| | | 91 | | PathParams: pathParams, |
| | | 92 | | Body: bodyPlan |
| | | 93 | | )); |
| | | 94 | | } |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | return plans; |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | private static IReadOnlyList<CallbackParamPlan> ExtractPathParams(OpenApiOperation op) |
| | | 101 | | { |
| | | 102 | | return op.Parameters is null || op.Parameters.Count == 0 |
| | | 103 | | ? [] |
| | | 104 | | : [.. op.Parameters |
| | | 105 | | .Where(p => p is not null && string.Equals(p.In?.ToString(), "path", StringComparison.OrdinalIgnoreCase)) |
| | | 106 | | .Select(p => new CallbackParamPlan( |
| | | 107 | | Name: p.Name ?? "", |
| | | 108 | | Location: "path" |
| | | 109 | | |
| | | 110 | | )) |
| | | 111 | | .Where(p => !string.IsNullOrWhiteSpace(p.Name))]; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | private static CallbackBodyPlan? ExtractBodyPlan(OpenApiOperation op) |
| | | 115 | | { |
| | | 116 | | var rb = op.RequestBody; |
| | | 117 | | if (rb?.Content is null || rb.Content.Count == 0) |
| | | 118 | | { |
| | | 119 | | return null; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | // Prefer application/json if present |
| | | 123 | | var kv = rb.Content.FirstOrDefault(c => string.Equals(c.Key, "application/json", StringComparison.OrdinalIgnoreC |
| | | 124 | | if (string.IsNullOrEmpty(kv.Key)) |
| | | 125 | | { |
| | | 126 | | // otherwise take first |
| | | 127 | | kv = rb.Content.First(); |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | var mediaType = kv.Key; |
| | | 131 | | return new CallbackBodyPlan(MediaType: mediaType); |
| | | 132 | | } |
| | | 133 | | } |