Skip to content

Commit c8fdf82

Browse files
committed
better warning for $var and :var conflicts
Add a specific warning when a URL parameter and a form field have the same name. The previous general warning about referencing form fields with the `$var` syntax was confusing in that case. see #1001
1 parent c03c3d4 commit c8fdf82

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- All [standard web encodings](https://encoding.spec.whatwg.org/#concept-encoding-get) are supported.
66
- Additionally, `base64` can be specified to decode binary data as base64 (compatible with [data URI](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs))
77
- By default, the old behavior of the `fetch_with_meta` function is preserved: the response body is decoded as `utf-8` if possible, otherwise the response is encoded in `base64`.
8+
- Added a specific warning when a URL parameter and a form field have the same name. The previous general warning about referencing form fields with the `$var` syntax was confusing in that case.
89

910
## v0.36.1
1011
- Fix regression introduced in v0.36.0: PostgreSQL money values showed as 0.0

src/webserver/database/syntax_tree.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -158,26 +158,43 @@ pub(super) async fn extract_req_param<'a>(
158158
// sync functions
159159
StmtParam::Get(x) => request.get_variables.get(x).map(SingleOrVec::as_json_str),
160160
StmtParam::Post(x) => request.post_variables.get(x).map(SingleOrVec::as_json_str),
161-
StmtParam::PostOrGet(x) => if let Some(v) = request.post_variables.get(x) {
162-
log::warn!("Deprecation warning! ${x} was used to reference a form field value (a POST variable) instead of a URL parameter. This will stop working soon. Please use :{x} instead.");
163-
Some(v)
164-
} else {
165-
request.get_variables.get(x)
161+
StmtParam::PostOrGet(x) => {
162+
let post_val = request.post_variables.get(x);
163+
let get_val = request.get_variables.get(x);
164+
if let Some(v) = post_val {
165+
if let Some(get_val) = get_val {
166+
log::warn!(
167+
"Deprecation warning! There is both a URL parameter named '{x}' with value '{get_val}' and a form field named '{x}' with value '{v}'. \
168+
SQLPage is using the value from the form submission, but this is ambiguous, can lead to unexpected behavior, and will stop working in a future version of SQLPage. \
169+
To fix this, please rename the URL parameter to something else, and reference the form field with :{x}."
170+
);
171+
} else {
172+
log::warn!("Deprecation warning! ${x} was used to reference a form field value (a POST variable) instead of a URL parameter. This will stop working soon. Please use :{x} instead.");
173+
}
174+
Some(v.as_json_str())
175+
} else {
176+
get_val.map(SingleOrVec::as_json_str)
177+
}
166178
}
167-
.map(SingleOrVec::as_json_str),
168179
StmtParam::Error(x) => anyhow::bail!("{}", x),
169180
StmtParam::Literal(x) => Some(Cow::Owned(x.to_string())),
170181
StmtParam::Null => None,
171182
StmtParam::Concat(args) => concat_params(&args[..], request, db_connection).await?,
172-
StmtParam::JsonObject(args) => json_object_params(&args[..], request, db_connection).await?,
183+
StmtParam::JsonObject(args) => {
184+
json_object_params(&args[..], request, db_connection).await?
185+
}
173186
StmtParam::JsonArray(args) => json_array_params(&args[..], request, db_connection).await?,
174187
StmtParam::Coalesce(args) => coalesce_params(&args[..], request, db_connection).await?,
175-
StmtParam::FunctionCall(func) => func.evaluate(request, db_connection).await.with_context(|| {
176-
format!(
177-
"Error in function call {func}.\nExpected {:#}",
178-
func.function
179-
)
180-
})?,
188+
StmtParam::FunctionCall(func) => {
189+
func.evaluate(request, db_connection)
190+
.await
191+
.with_context(|| {
192+
format!(
193+
"Error in function call {func}.\nExpected {:#}",
194+
func.function
195+
)
196+
})?
197+
}
181198
})
182199
}
183200

src/webserver/http.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,25 @@ pub enum SingleOrVec {
275275
Vec(Vec<String>),
276276
}
277277

278+
impl std::fmt::Display for SingleOrVec {
279+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
280+
match self {
281+
SingleOrVec::Single(x) => write!(f, "{x}"),
282+
SingleOrVec::Vec(v) => {
283+
write!(f, "[")?;
284+
let mut it = v.iter();
285+
if let Some(first) = it.next() {
286+
write!(f, "{first}")?;
287+
}
288+
for item in it {
289+
write!(f, ", {item}")?;
290+
}
291+
write!(f, "]")
292+
}
293+
}
294+
}
295+
}
296+
278297
impl SingleOrVec {
279298
pub(crate) fn merge(&mut self, other: Self) {
280299
match (self, other) {

0 commit comments

Comments
 (0)