Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x.vweb: accept query params as method arguments #21201

Merged
merged 2 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions vlib/x/vweb/tests/vweb_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ fn test_login_with_multipart_form_data_send_by_fetch() {
assert x.body == 'username: xmyusernamex | password: xmypassword123x'
}

fn test_query_params_are_passed_as_arguments() {
x := http.get('http://${localserver}/query_echo?c=3&a="test"&b=20')!
spytheman marked this conversation as resolved.
Show resolved Hide resolved
assert x.status() == .ok
assert x.body == 'a: x"test"x | b: x20x'
}

fn test_host() {
mut req := http.Request{
url: 'http://${localserver}/with_host'
Expand Down
5 changes: 5 additions & 0 deletions vlib/x/vweb/tests/vweb_test_server.v
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ pub fn (mut app ServerApp) file_echo(mut ctx ServerContext) vweb.Result {
return ctx.text(ctx.files['file'][0].data)
}

@['/query_echo']
pub fn (mut app ServerApp) query_echo(mut ctx ServerContext, a string, b int) vweb.Result {
return ctx.text('a: x${a}x | b: x${b}x')
}

// Make sure [post] works without the path
@[post]
pub fn (mut app ServerApp) json(mut ctx ServerContext) vweb.Result {
Expand Down
34 changes: 30 additions & 4 deletions vlib/x/vweb/vweb.v
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,8 @@ fn handle_route[A, X](mut app A, mut user_context X, url urllib.URL, host string

// Skip if the host does not match or is empty
if route.host == '' || route.host == host {
can_have_data_args := user_context.Context.req.method == .post
|| user_context.Context.req.method == .get
// Route immediate matches first
// For example URL `/register` matches route `/:user`, but `fn register()`
// should be called first.
Expand All @@ -836,12 +838,19 @@ fn handle_route[A, X](mut app A, mut user_context X, url urllib.URL, host string
}
}

if user_context.Context.req.method == .post && method.args.len > 1 {
// Populate method args with form values
if method.args.len > 1 && can_have_data_args {
// Populate method args with form or query values
mut args := []string{cap: method.args.len + 1}
data := if user_context.Context.req.method == .get {
user_context.Context.query
} else {
user_context.Context.form
}

for param in method.args[1..] {
args << user_context.Context.form[param.name]
args << data[param.name]
}

app.$method(mut user_context, args)
} else {
app.$method(mut user_context)
Expand All @@ -857,7 +866,24 @@ fn handle_route[A, X](mut app A, mut user_context X, url urllib.URL, host string
}
}

app.$method(mut user_context)
if method.args.len > 1 && can_have_data_args {
// Populate method args with form or query values
mut args := []string{cap: method.args.len + 1}

data := if user_context.Context.req.method == .get {
user_context.Context.query
} else {
user_context.Context.form
}

for param in method.args[1..] {
args << data[param.name]
}

app.$method(mut user_context, args)
} else {
app.$method(mut user_context)
}
return
}

Expand Down