Skip to content

Commit 8a8831d

Browse files
cursoragentlovasoa
andcommitted
Fix: fetch(null) and fetch_with_meta(null) return null
Co-authored-by: contact <[email protected]>
1 parent 95f228f commit 8a8831d

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- `curl -H "Accept: application/json" http://example.com/page.sql`: returns a json array
66
- `curl -H "Accept: application/x-ndjson" http://example.com/page.sql`: returns one json object per line.
77
- Fixed a bug in `sqlpage.link`: a link with no path (link to the current page) and no url parameter now works as expected. It used to keep the existing url parameters instead of removing them. `sqlpage.link('', '{}')` now returns `'?'` instead of the empty string.
8+
- `sqlpage.fetch(null)` and `sqlpage.fetch_with_meta(null)` now return `null` instead of throwing an error.
89
- **New Function**: `sqlpage.set_variable(name, value)`
910
- Returns a URL with the specified variable set to the given value, preserving other existing variables.
1011
- This is a shorthand for `sqlpage.link(sqlpage.path(), json_patch(sqlpage.variables('get'), json_object(name, value)))`.

src/webserver/database/sqlpage_functions/functions.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::function_traits::BorrowFromStr;
12
use super::{ExecutionContext, RequestInfo};
23
use crate::webserver::{
34
database::{
@@ -26,8 +27,8 @@ super::function_definition_macro::sqlpage_functions! {
2627
environment_variable(name: Cow<str>);
2728
exec((&RequestInfo), program_name: Cow<str>, args: Vec<Cow<str>>);
2829

29-
fetch((&RequestInfo), http_request: SqlPageFunctionParam<super::http_fetch_request::HttpFetchRequest<'_>>);
30-
fetch_with_meta((&RequestInfo), http_request: SqlPageFunctionParam<super::http_fetch_request::HttpFetchRequest<'_>>);
30+
fetch((&RequestInfo), http_request: Option<Cow<str>>);
31+
fetch_with_meta((&RequestInfo), http_request: Option<Cow<str>>);
3132

3233
hash_password(password: Option<String>);
3334
header((&RequestInfo), name: Cow<str>);
@@ -185,8 +186,14 @@ fn prepare_request_body(
185186

186187
async fn fetch(
187188
request: &RequestInfo,
188-
http_request: super::http_fetch_request::HttpFetchRequest<'_>,
189-
) -> anyhow::Result<String> {
189+
http_request: Option<Cow<'_, str>>,
190+
) -> anyhow::Result<Option<String>> {
191+
let Some(http_request_str) = http_request else {
192+
return Ok(None);
193+
};
194+
let http_request =
195+
super::http_fetch_request::HttpFetchRequest::borrow_from_str(http_request_str)
196+
.with_context(|| "Invalid http fetch request")?;
190197
let client = make_http_client(&request.app_state.config)
191198
.with_context(|| "Unable to create an HTTP client")?;
192199
let req = build_request(&client, &http_request)?;
@@ -219,7 +226,7 @@ async fn fetch(
219226
.to_vec();
220227
let response_str = decode_response(body, http_request.response_encoding.as_deref())?;
221228
log::debug!("Fetch response: {response_str}");
222-
Ok(response_str)
229+
Ok(Some(response_str))
223230
}
224231

225232
fn decode_response(response: Vec<u8>, encoding: Option<&str>) -> anyhow::Result<String> {
@@ -259,9 +266,15 @@ fn decode_response(response: Vec<u8>, encoding: Option<&str>) -> anyhow::Result<
259266

260267
async fn fetch_with_meta(
261268
request: &RequestInfo,
262-
http_request: super::http_fetch_request::HttpFetchRequest<'_>,
263-
) -> anyhow::Result<String> {
269+
http_request: Option<Cow<'_, str>>,
270+
) -> anyhow::Result<Option<String>> {
264271
use serde::{ser::SerializeMap, Serializer};
272+
let Some(http_request_str) = http_request else {
273+
return Ok(None);
274+
};
275+
let http_request =
276+
super::http_fetch_request::HttpFetchRequest::borrow_from_str(http_request_str)
277+
.with_context(|| "Invalid http fetch request")?;
265278

266279
let client = make_http_client(&request.app_state.config)
267280
.with_context(|| "Unable to create an HTTP client")?;
@@ -337,7 +350,7 @@ async fn fetch_with_meta(
337350

338351
obj.end()?;
339352
let return_value = String::from_utf8(resp_str)?;
340-
Ok(return_value)
353+
Ok(Some(return_value))
341354
}
342355

343356
pub(crate) async fn hash_password(password: Option<String>) -> anyhow::Result<Option<String>> {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
select null as expected, sqlpage.fetch(null) as actual;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
select null as expected, sqlpage.fetch_with_meta(null) as actual;

0 commit comments

Comments
 (0)