Compare commits

..

No commits in common. "4221f8cf2cee96d27b322885fb5f5f5dc6749883" and "8cebf52405c7baf9524eafbd2238464aa080062c" have entirely different histories.

2 changed files with 22 additions and 118 deletions

View File

@ -146,18 +146,6 @@ func TestParseMissingMethodDescription(t *testing.T) {
}`, }`,
ErrFormat.Wrap(ErrMissingMethodDesc.WrapString("GET /")), ErrFormat.Wrap(ErrMissingMethodDesc.WrapString("GET /")),
}, },
{ // missing description
`{
"/": {
"subservice": {
"GET": {
}
}
}
}`,
ErrFormat.Wrap(ErrMissingMethodDesc.WrapString("GET subservice")),
},
{ // empty description { // empty description
`{ `{
"GET": { "GET": {
@ -209,75 +197,6 @@ func TestParseMissingMethodDescription(t *testing.T) {
} }
func TestParamEmptyRenameNoRename(t *testing.T) {
reader := strings.NewReader(`{
"GET": {
"info": "info",
"in": {
"original": { "info": "valid-desc", "type": "valid-type", "name": "" }
}
}
}`)
srv, err := Parse(reader)
if err != nil {
t.Errorf("unexpected error: '%s'", err)
t.FailNow()
}
method := srv.Method(http.MethodGet)
if method == nil {
t.Errorf("expected GET method not to be nil")
t.FailNow()
}
for _, param := range method.Parameters {
if param.Rename != "original" {
t.Errorf("expected the parameter 'original' not to be renamed to '%s'", param.Rename)
t.FailNow()
}
}
}
func TestOptionalParam(t *testing.T) {
reader := strings.NewReader(`{
"GET": {
"info": "info",
"in": {
"optional": { "info": "valid-desc", "type": "?optional-type" },
"required": { "info": "valid-desc", "type": "required-type" },
"required2": { "info": "valid-desc", "type": "a" },
"optional2": { "info": "valid-desc", "type": "?a" }
}
}
}`)
srv, err := Parse(reader)
if err != nil {
t.Errorf("unexpected error: '%s'", err)
t.FailNow()
}
method := srv.Method(http.MethodGet)
if method == nil {
t.Errorf("expected GET method not to be nil")
t.FailNow()
}
for pName, param := range method.Parameters {
if pName == "optional" || pName == "optional2" {
if !param.Optional {
t.Errorf("expected parameter '%s' to be optional", pName)
t.Failed()
}
}
if pName == "required" || pName == "required2" {
if param.Optional {
t.Errorf("expected parameter '%s' to be required", pName)
t.Failed()
}
}
}
}
func TestParseParameters(t *testing.T) { func TestParseParameters(t *testing.T) {
tests := []struct { tests := []struct {
Raw string Raw string
@ -365,22 +284,6 @@ func TestParseParameters(t *testing.T) {
ErrFormat.Wrap(ErrMissingParamType.WrapString("GET / {param1}")), ErrFormat.Wrap(ErrMissingParamType.WrapString("GET / {param1}")),
nil, nil,
}, },
{ // invalid type (optional mark only)
`{
"GET": {
"info": "info",
"in": {
"param1": {
"info": "valid",
"type": "?"
}
}
}
}`,
ErrFormat.Wrap(ErrMissingParamType.WrapString("GET / {param1}")),
ErrFormat.Wrap(ErrMissingParamType.WrapString("GET / {param1}")),
},
{ // valid description + valid type { // valid description + valid type
`{ `{
"GET": { "GET": {
@ -388,22 +291,7 @@ func TestParseParameters(t *testing.T) {
"in": { "in": {
"param1": { "param1": {
"info": "valid", "info": "valid",
"type": "a" "type": "valid"
}
}
}
}`,
nil,
nil,
},
{ // valid description + valid OPTIONAL type
`{
"GET": {
"info": "info",
"in": {
"param1": {
"info": "valid",
"type": "?valid"
} }
} }
} }
@ -490,9 +378,7 @@ func TestParseParameters(t *testing.T) {
if err != nil && test.Error != nil { if err != nil && test.Error != nil {
if err.Error() != test.Error.Error() && err.Error() != test.ErrorAlternative.Error() { if err.Error() != test.Error.Error() && err.Error() != test.ErrorAlternative.Error() {
t.Errorf("got the error: '%s'", err.Error()) t.Errorf("expected the error '%s' (got '%s')", test.Error.Error(), err.Error())
t.Errorf("expected error (alternative 1): '%s'", test.Error.Error())
t.Errorf("expected error (alternative 2): '%s'", test.ErrorAlternative.Error())
t.FailNow() t.FailNow()
} }
} }

View File

@ -39,9 +39,17 @@ func (methodDef *Method) checkAndFormat(servicePath string, httpMethod string) e
} }
// 3.2.1. Same rename field // 3.2.1. Same rename field
if pData.Rename == param.Rename {
return ErrParamNameConflict.WrapString(httpMethod + " " + servicePath + " {" + pName + "}")
}
// 3.2.2. Not-renamed field matches a renamed field // 3.2.2. Not-renamed field matches a renamed field
if pName == param.Rename {
return ErrParamNameConflict.WrapString(httpMethod + " " + servicePath + " {" + pName + "}")
}
// 3.2.3. Renamed field matches name // 3.2.3. Renamed field matches name
if pData.Rename == param.Rename || pName == param.Rename || pData.Rename == paramName { if pData.Rename == paramName {
return ErrParamNameConflict.WrapString(httpMethod + " " + servicePath + " {" + pName + "}") return ErrParamNameConflict.WrapString(httpMethod + " " + servicePath + " {" + pName + "}")
} }
@ -53,7 +61,7 @@ func (methodDef *Method) checkAndFormat(servicePath string, httpMethod string) e
} }
// 3.4. Manage invalid type // 3.4. Manage invalid type
if len(pData.Type) < 1 || pData.Type == "?" { if len(pData.Type) < 1 {
return ErrMissingParamType.WrapString(httpMethod + " " + servicePath + " {" + pName + "}") return ErrMissingParamType.WrapString(httpMethod + " " + servicePath + " {" + pName + "}")
} }
@ -67,3 +75,13 @@ func (methodDef *Method) checkAndFormat(servicePath string, httpMethod string) e
return nil return nil
} }
// scopeHasPermission returns whether the permission fulfills a given scope
func scopeHasPermission(permission string, scope []string) bool {
for _, s := range scope {
if permission == s {
return true
}
}
return false
}