|
| 1 | +namespace NpgsqlRestTests |
| 2 | +{ |
| 3 | + public static partial class Database |
| 4 | + { |
| 5 | + public static void TsClientBodyParamGetTests() |
| 6 | + { |
| 7 | + // GET endpoint with @body_parameter_name targeting a defaulted parameter (so it carries the |
| 8 | + // TS optional "?" suffix in the interface). Regression for three TsClient bugs: |
| 9 | + // 1) the body expression emitted "request.payload?" (trailing "?" — a syntax error), |
| 10 | + // 2) the query-exclusion key was ["payload?"] (with "?") so the param was NOT stripped, |
| 11 | + // 3) a fetch body was emitted for a GET (fetch forbids a body on GET). |
| 12 | + script.Append(""" |
| 13 | +create schema if not exists tsclient_test; |
| 14 | +create function tsclient_test.bodyparam_get(_keyword text, _payload text default null) |
| 15 | +returns text |
| 16 | +language sql |
| 17 | +as $$ |
| 18 | +select coalesce(_keyword, '') || coalesce(_payload, ''); |
| 19 | +$$; |
| 20 | +comment on function tsclient_test.bodyparam_get(text, text) is ' |
| 21 | +HTTP GET |
| 22 | +tsclient_module=bodyparam_get |
| 23 | +body_parameter_name payload'; |
| 24 | +
|
| 25 | +-- POST endpoint with an HTTP Custom Type whose body field is targeted by @body_parameter_name using |
| 26 | +-- its EXPANDED signature name (_response_body). The generator must match that name (via ExpandedName) |
| 27 | +-- the same way the server does, emit the body with the bare converted name (request.responseBody), |
| 28 | +-- and exclude it from the query string. Regression for the generator only matching converted/actual |
| 29 | +-- names. The HTTP type points at an unused port; it never fires during code generation. |
| 30 | +create type tsclient_test.tsc_http_probe as ( |
| 31 | + body text, |
| 32 | + status_code int, |
| 33 | + success boolean, |
| 34 | + error_message text |
| 35 | +); |
| 36 | +comment on type tsclient_test.tsc_http_probe is 'GET http://localhost:1/tsc-dummy'; |
| 37 | +
|
| 38 | +create function tsclient_test.bodyparam_expanded(_response tsclient_test.tsc_http_probe default null) |
| 39 | +returns text |
| 40 | +language plpgsql |
| 41 | +as $$ begin return ''; end; $$; |
| 42 | +comment on function tsclient_test.bodyparam_expanded(tsclient_test.tsc_http_probe) is ' |
| 43 | +HTTP POST |
| 44 | +tsclient_module=bodyparam_expanded |
| 45 | +body_parameter_name _response_body'; |
| 46 | +"""); |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +namespace NpgsqlRestTests.TsClientTests |
| 52 | +{ |
| 53 | + [Collection("TestFixture")] |
| 54 | + public class BodyParamGetTests |
| 55 | + { |
| 56 | + private const string Expected = """ |
| 57 | +const baseUrl = ""; |
| 58 | +const parseQuery = (query: Record<any, any>) => "?" + Object.keys(query ? query : {}) |
| 59 | + .map(key => { |
| 60 | + const value = (query[key] != null ? query[key] : "") as string; |
| 61 | + if (Array.isArray(value)) { |
| 62 | + return value.map((s: string) => s ? `${key}=${encodeURIComponent(s)}` : `${key}=`).join("&"); |
| 63 | + } |
| 64 | + return `${key}=${encodeURIComponent(value)}`; |
| 65 | + }) |
| 66 | + .join("&"); |
| 67 | +
|
| 68 | +interface ITsclientTestBodyparamGetRequest { |
| 69 | + keyword: string | null; |
| 70 | + payload?: string | null; |
| 71 | +} |
| 72 | +
|
| 73 | +
|
| 74 | +/** |
| 75 | +* function tsclient_test.bodyparam_get( |
| 76 | +* _keyword text, |
| 77 | +* _payload text DEFAULT NULL::text |
| 78 | +* ) |
| 79 | +* returns text |
| 80 | +* |
| 81 | +* @remarks |
| 82 | +* comment on function tsclient_test.bodyparam_get is 'HTTP GET |
| 83 | +* tsclient_module=bodyparam_get |
| 84 | +* body_parameter_name payload'; |
| 85 | +* |
| 86 | +* @param request - Object containing request parameters. |
| 87 | +* @returns {string} |
| 88 | +* |
| 89 | +* @see FUNCTION tsclient_test.bodyparam_get |
| 90 | +*/ |
| 91 | +export async function tsclientTestBodyparamGet( |
| 92 | + request: ITsclientTestBodyparamGetRequest |
| 93 | +) : Promise<string> { |
| 94 | + const response = await fetch(baseUrl + "/api/tsclient-test/bodyparam-get" + parseQuery((({ ["payload"]: _1, ...rest }) => rest)(request)), { |
| 95 | + method: "GET", |
| 96 | + }); |
| 97 | + return await response.text(); |
| 98 | +} |
| 99 | +
|
| 100 | +
|
| 101 | +"""; |
| 102 | + |
| 103 | + private const string ExpectedExpanded = """ |
| 104 | +const baseUrl = ""; |
| 105 | +const parseQuery = (query: Record<any, any>) => "?" + Object.keys(query ? query : {}) |
| 106 | + .map(key => { |
| 107 | + const value = (query[key] != null ? query[key] : "") as string; |
| 108 | + if (Array.isArray(value)) { |
| 109 | + return value.map((s: string) => s ? `${key}=${encodeURIComponent(s)}` : `${key}=`).join("&"); |
| 110 | + } |
| 111 | + return `${key}=${encodeURIComponent(value)}`; |
| 112 | + }) |
| 113 | + .join("&"); |
| 114 | +
|
| 115 | +interface ITsclientTestBodyparamExpandedRequest { |
| 116 | + responseBody?: string | null; |
| 117 | + responseStatusCode?: number | null; |
| 118 | + responseSuccess?: boolean | null; |
| 119 | + responseErrorMessage?: string | null; |
| 120 | +} |
| 121 | +
|
| 122 | +
|
| 123 | +/** |
| 124 | +* function tsclient_test.bodyparam_expanded( |
| 125 | +* _response_body text DEFAULT NULL::tsclient_test.tsc_http_probe, |
| 126 | +* _response_status_code integer, |
| 127 | +* _response_success boolean, |
| 128 | +* _response_error_message text |
| 129 | +* ) |
| 130 | +* returns text |
| 131 | +* |
| 132 | +* @remarks |
| 133 | +* comment on function tsclient_test.bodyparam_expanded is 'HTTP POST |
| 134 | +* tsclient_module=bodyparam_expanded |
| 135 | +* body_parameter_name _response_body'; |
| 136 | +* |
| 137 | +* @param request - Object containing request parameters. |
| 138 | +* @returns {string} |
| 139 | +* |
| 140 | +* @see FUNCTION tsclient_test.bodyparam_expanded |
| 141 | +*/ |
| 142 | +export async function tsclientTestBodyparamExpanded( |
| 143 | + request: ITsclientTestBodyparamExpandedRequest |
| 144 | +) : Promise<string> { |
| 145 | + const response = await fetch(baseUrl + "/api/tsclient-test/bodyparam-expanded" + parseQuery((({ ["responseBody"]: _1, ...rest }) => rest)(request)), { |
| 146 | + method: "POST", |
| 147 | + body: request.responseBody |
| 148 | + }); |
| 149 | + return await response.text(); |
| 150 | +} |
| 151 | +
|
| 152 | +
|
| 153 | +"""; |
| 154 | + |
| 155 | + [Fact] |
| 156 | + public void Test_BodyParamGet_GeneratedFile() |
| 157 | + { |
| 158 | + var filePath = Path.Combine(Setup.Program.TsClientOutputPath, "bodyparam_get.ts"); |
| 159 | + File.Exists(filePath).Should().BeTrue($"Expected file at {filePath}"); |
| 160 | + |
| 161 | + var content = File.ReadAllText(filePath); |
| 162 | + |
| 163 | + // The fixed generator must NOT emit the optional "?" suffix in the runtime name, and must |
| 164 | + // not attach a body to a GET request. |
| 165 | + content.Should().NotContain("request.payload?", "the body expression must not carry the TS optional suffix"); |
| 166 | + content.Should().NotContain("[\"payload?\"]", "the query-exclusion key must be the bare property name"); |
| 167 | + content.Should().NotContain("body:", "a GET request must not emit a fetch body"); |
| 168 | + |
| 169 | + // Full-content match. Normalize per-line trailing whitespace and line endings: the generator |
| 170 | + // emits trailing spaces on blank comment lines, which are easily mangled when transcribed. |
| 171 | + Normalize(content).Should().Be(Normalize(Expected)); |
| 172 | + } |
| 173 | + |
| 174 | + [Fact] |
| 175 | + public void Test_BodyParamExpanded_GeneratedFile() |
| 176 | + { |
| 177 | + var filePath = Path.Combine(Setup.Program.TsClientOutputPath, "bodyparam_expanded.ts"); |
| 178 | + File.Exists(filePath).Should().BeTrue($"Expected file at {filePath}"); |
| 179 | + |
| 180 | + var content = File.ReadAllText(filePath); |
| 181 | + |
| 182 | + // @body_parameter_name targeted the expanded signature name (_response_body); the generator |
| 183 | + // must resolve it to the body field and emit the body with the bare converted name. |
| 184 | + content.Should().Contain("body: request.responseBody", "the expanded name must resolve to the body field, emitted by its converted name"); |
| 185 | + content.Should().Contain("[\"responseBody\"]", "the body parameter must be excluded from the query string"); |
| 186 | + content.Should().NotContain("parseQuery(request)", "the body parameter must not be left in the unfiltered query"); |
| 187 | + content.Should().NotContain("request.responseBody?", "no trailing optional suffix in the runtime name"); |
| 188 | + |
| 189 | + Normalize(content).Should().Be(Normalize(ExpectedExpanded)); |
| 190 | + } |
| 191 | + |
| 192 | + private static string Normalize(string s) |
| 193 | + { |
| 194 | + var lines = s.Replace("\r\n", "\n").Split('\n'); |
| 195 | + for (var i = 0; i < lines.Length; i++) |
| 196 | + { |
| 197 | + lines[i] = lines[i].TrimEnd(); |
| 198 | + } |
| 199 | + return string.Join("\n", lines); |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments