vweb: prohibit invalid parameter types in vweb app methods (#10631)

pull/10668/head
shadowninja55 2021-07-04 11:37:09 -04:00 committed by GitHub
parent f246d73d6e
commit 8c43d2450f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 0 deletions

View File

@ -7570,6 +7570,16 @@ fn (mut c Checker) verify_vweb_params_for_method(m ast.Fn) (bool, int, int) {
// allow non custom routed methods, with 1:1 mapping // allow non custom routed methods, with 1:1 mapping
return true, -1, margs return true, -1, margs
} }
if m.params.len > 1 {
for param in m.params[1..] {
param_sym := c.table.get_final_type_symbol(param.typ)
if !(param_sym.is_string() || param_sym.is_number() || param_sym.is_float()
|| param_sym.kind == .bool) {
c.error('invalid type `$param_sym.name` for parameter `$param.name` in vweb app method `$m.name`',
param.pos)
}
}
}
mut route_attributes := 0 mut route_attributes := 0
for a in m.attrs { for a in m.attrs {
if a.name.starts_with('/') { if a.name.starts_with('/') {

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/invalid_vweb_param_type.vv:8:24: error: invalid type `[]bool` for parameter `list` in vweb app method `index`
6 |
7 | ['/:list'; get]
8 | fn (mut app App) index(list []bool) vweb.Result {
| ~~~~
9 | return app.text('')
10 | }

View File

@ -0,0 +1,12 @@
import vweb
struct App {
vweb.Context
}
['/:list'; get]
fn (mut app App) index(list []bool) vweb.Result {
return app.text('')
}
vweb.run(&App{}, 5000)