enforce 'name' for capture or query parameters

This commit is contained in:
Adrien Marquès 2020-03-29 14:18:05 +02:00
parent b15bb578ce
commit ca2be1415d
Signed by: xdrm-brackets
GPG Key ID: D75243CA236D825E
3 changed files with 72 additions and 20 deletions

View File

@ -481,6 +481,46 @@ func TestParseParameters(t *testing.T) {
]`, ]`,
nil, nil,
}, },
// missing rename
{
`[
{
"method": "GET",
"path": "/{uri}",
"info": "info",
"in": {
"{uri}": { "info": "valid", "type": "any" }
}
}
]`,
ErrMandatoryRename,
},
{
`[
{
"method": "GET",
"path": "/",
"info": "info",
"in": {
"GET@abc": { "info": "valid", "type": "any" }
}
}
]`,
ErrMandatoryRename,
},
{
`[
{
"method": "GET",
"path": "/",
"info": "info",
"in": {
"GET@abc": { "info": "valid", "type": "any", "name": "abc" }
}
}
]`,
nil,
},
{ // URI parameter { // URI parameter
`[ `[
@ -616,7 +656,7 @@ func TestServiceCollision(t *testing.T) {
}, },
{ "method": "GET", "path": "/a/{c}", { "method": "GET", "path": "/a/{c}",
"info": "info", "in": { "info": "info", "in": {
"{c}": { "info":"info", "type": "string" } "{c}": { "info":"info", "type": "string", "name": "c" }
} }
} }
]`, ]`,
@ -629,7 +669,7 @@ func TestServiceCollision(t *testing.T) {
}, },
{ "method": "GET", "path": "/a/{c}", { "method": "GET", "path": "/a/{c}",
"info": "info", "in": { "info": "info", "in": {
"{c}": { "info":"info", "type": "uint" } "{c}": { "info":"info", "type": "uint", "name": "c" }
} }
} }
]`, ]`,
@ -642,7 +682,7 @@ func TestServiceCollision(t *testing.T) {
}, },
{ "method": "GET", "path": "/a/{c}/d", { "method": "GET", "path": "/a/{c}/d",
"info": "info", "in": { "info": "info", "in": {
"{c}": { "info":"info", "type": "string" } "{c}": { "info":"info", "type": "string", "name": "c" }
} }
} }
]`, ]`,
@ -655,7 +695,7 @@ func TestServiceCollision(t *testing.T) {
}, },
{ "method": "GET", "path": "/a/{c}", { "method": "GET", "path": "/a/{c}",
"info": "info", "in": { "info": "info", "in": {
"{c}": { "info":"info", "type": "string" } "{c}": { "info":"info", "type": "string", "name": "c" }
} }
} }
]`, ]`,
@ -668,7 +708,7 @@ func TestServiceCollision(t *testing.T) {
}, },
{ "method": "GET", "path": "/a/{c}", { "method": "GET", "path": "/a/{c}",
"info": "info", "in": { "info": "info", "in": {
"{c}": { "info":"info", "type": "string" } "{c}": { "info":"info", "type": "string", "name": "c" }
} }
} }
]`, ]`,
@ -681,7 +721,7 @@ func TestServiceCollision(t *testing.T) {
}, },
{ "method": "GET", "path": "/a/{c}/d", { "method": "GET", "path": "/a/{c}/d",
"info": "info", "in": { "info": "info", "in": {
"{c}": { "info":"info", "type": "string" } "{c}": { "info":"info", "type": "string", "name": "c" }
} }
} }
]`, ]`,
@ -694,7 +734,7 @@ func TestServiceCollision(t *testing.T) {
}, },
{ "method": "GET", "path": "/a/{c}", { "method": "GET", "path": "/a/{c}",
"info": "info", "in": { "info": "info", "in": {
"{c}": { "info":"info", "type": "uint" } "{c}": { "info":"info", "type": "uint", "name": "c" }
} }
} }
]`, ]`,
@ -707,7 +747,7 @@ func TestServiceCollision(t *testing.T) {
}, },
{ "method": "GET", "path": "/a/{c}", { "method": "GET", "path": "/a/{c}",
"info": "info", "in": { "info": "info", "in": {
"{c}": { "info":"info", "type": "uint" } "{c}": { "info":"info", "type": "uint", "name": "c" }
} }
} }
]`, ]`,
@ -720,7 +760,7 @@ func TestServiceCollision(t *testing.T) {
}, },
{ "method": "GET", "path": "/a/{c}/d", { "method": "GET", "path": "/a/{c}/d",
"info": "info", "in": { "info": "info", "in": {
"{c}": { "info":"info", "type": "uint" } "{c}": { "info":"info", "type": "uint", "name": "c" }
} }
} }
]`, ]`,
@ -733,7 +773,7 @@ func TestServiceCollision(t *testing.T) {
}, },
{ "method": "GET", "path": "/a/{c}/d", { "method": "GET", "path": "/a/{c}/d",
"info": "info", "in": { "info": "info", "in": {
"{c}": { "info":"info", "type": "uint" } "{c}": { "info":"info", "type": "uint", "name": "c" }
} }
} }
]`, ]`,
@ -743,12 +783,12 @@ func TestServiceCollision(t *testing.T) {
`[ `[
{ "method": "GET", "path": "/a/{b}", { "method": "GET", "path": "/a/{b}",
"info": "info", "in": { "info": "info", "in": {
"{b}": { "info":"info", "type": "uint" } "{b}": { "info":"info", "type": "uint", "name": "b" }
} }
}, },
{ "method": "GET", "path": "/a/{c}", { "method": "GET", "path": "/a/{c}",
"info": "info", "in": { "info": "info", "in": {
"{c}": { "info":"info", "type": "uint" } "{c}": { "info":"info", "type": "uint", "name": "c" }
} }
} }
]`, ]`,
@ -758,12 +798,12 @@ func TestServiceCollision(t *testing.T) {
`[ `[
{ "method": "GET", "path": "/a/{b}", { "method": "GET", "path": "/a/{b}",
"info": "info", "in": { "info": "info", "in": {
"{b}": { "info":"info", "type": "uint" } "{b}": { "info":"info", "type": "uint", "name": "b" }
} }
}, },
{ "method": "PUT", "path": "/a/{c}", { "method": "PUT", "path": "/a/{c}",
"info": "info", "in": { "info": "info", "in": {
"{c}": { "info":"info", "type": "uint" } "{c}": { "info":"info", "type": "uint", "name": "c" }
} }
} }
]`, ]`,
@ -850,7 +890,8 @@ func TestMatchSimple(t *testing.T) {
"in": { "in": {
"{id}": { "{id}": {
"info": "info", "info": "info",
"type": "bool" "type": "bool",
"name": "id"
} }
} }
} ]`, } ]`,
@ -865,7 +906,8 @@ func TestMatchSimple(t *testing.T) {
"in": { "in": {
"{id}": { "{id}": {
"info": "info", "info": "info",
"type": "int" "type": "int",
"name": "id"
} }
} }
} ]`, } ]`,
@ -880,7 +922,8 @@ func TestMatchSimple(t *testing.T) {
"in": { "in": {
"{valid}": { "{valid}": {
"info": "info", "info": "info",
"type": "bool" "type": "bool",
"name": "valid"
} }
} }
} ]`, } ]`,
@ -895,7 +938,8 @@ func TestMatchSimple(t *testing.T) {
"in": { "in": {
"{valid}": { "{valid}": {
"info": "info", "info": "info",
"type": "bool" "type": "bool",
"name": "valid"
} }
} }
} ]`, } ]`,

View File

@ -29,6 +29,9 @@ const ErrInvalidPatternBraceCapture = cerr("invalid uri capturing braces")
// ErrUnspecifiedBraceCapture - a parameter brace capture is not specified in the pattern // ErrUnspecifiedBraceCapture - a parameter brace capture is not specified in the pattern
const ErrUnspecifiedBraceCapture = cerr("capturing brace missing in the path") const ErrUnspecifiedBraceCapture = cerr("capturing brace missing in the path")
// ErrMandatoryRename - capture/query parameters must have a rename
const ErrMandatoryRename = cerr("capture and query parameters must have a 'name'")
// ErrUndefinedBraceCapture - a parameter brace capture in the pattern is not defined in parameters // ErrUndefinedBraceCapture - a parameter brace capture in the pattern is not defined in parameters
const ErrUndefinedBraceCapture = cerr("capturing brace missing input definition") const ErrUndefinedBraceCapture = cerr("capturing brace missing input definition")

View File

@ -183,7 +183,7 @@ func (svc *Service) validateInput(types []datatype.T) error {
} }
// fail if brace capture does not exists in pattern // fail if brace capture does not exists in pattern
iscapture := false var iscapture, isquery bool
if matches := braceRegex.FindAllStringSubmatch(paramName, -1); len(matches) > 0 && len(matches[0]) > 1 { if matches := braceRegex.FindAllStringSubmatch(paramName, -1); len(matches) > 0 && len(matches[0]) > 1 {
braceName := matches[0][1] braceName := matches[0][1]
@ -209,7 +209,7 @@ func (svc *Service) validateInput(types []datatype.T) error {
svc.Query = make(map[string]*Parameter) svc.Query = make(map[string]*Parameter)
} }
svc.Query[queryName] = param svc.Query[queryName] = param
isquery = true
} else { } else {
if svc.Form == nil { if svc.Form == nil {
svc.Form = make(map[string]*Parameter) svc.Form = make(map[string]*Parameter)
@ -217,6 +217,11 @@ func (svc *Service) validateInput(types []datatype.T) error {
svc.Form[paramName] = param svc.Form[paramName] = param
} }
// fail if capture or query without rename
if len(param.Rename) < 1 && (iscapture || isquery) {
return fmt.Errorf("%s: %w", paramName, ErrMandatoryRename)
}
// use param name if no rename // use param name if no rename
if len(param.Rename) < 1 { if len(param.Rename) < 1 {
param.Rename = paramName param.Rename = paramName