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

fix: update yaml validation to match kong behavior #37

Merged
merged 2 commits into from
Dec 20, 2022
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
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ require (
github.com/stretchr/testify v1.7.0
github.com/yuin/gluare v0.0.0-20170607022532-d7c94f1a80ed
github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9
gopkg.in/yaml.v3 v3.0.1
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/yuin/gopher-lua => github.com/hbagdi/gopher-lua v0.0.0-20211129210354-3e4a277fb892
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ github.com/yuin/gluare v0.0.0-20170607022532-d7c94f1a80ed/go.mod h1:9w6KSdZh23UW
golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0 h1:POO/ycCATvegFmVuPpQzZFJ+pGZeX22Ufu6fibxDVjU=
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0/go.mod h1:WDnlLJ4WF5VGsH/HVa3CI79GS0ol3YnhVnKP89i0kNg=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
14 changes: 10 additions & 4 deletions lualibs/go/lyaml/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package lyaml

import (
lua "github.com/yuin/gopher-lua"
"gopkg.in/yaml.v3"
"gopkg.in/yaml.v1"
)

func APILoad(l *lua.LState) int {
Expand All @@ -16,7 +16,13 @@ func APILoad(l *lua.LState) int {
return 2 //nolint:gomnd
}

l.Push(decode(l, value))
decoded := decode(l, value)
if decoded.Type() == lua.LNil.Type() {
l.Push(lua.LNil)
l.Push(lua.LString(err.Error()))
return 2 //nolint:gomnd
}
l.Push(decoded)
return 1
}

Expand All @@ -36,10 +42,10 @@ func decode(l *lua.LState, value interface{}) lua.LValue {
arr.Append(decode(l, item))
}
return arr
case map[string]interface{}:
case map[interface{}]interface{}:
table := l.CreateTable(0, len(valueType))
for key, item := range valueType {
table.RawSetH(lua.LString(key), decode(l, item))
table.RawSetH(decode(l, key), decode(l, item))
}
return table
}
Expand Down
7 changes: 7 additions & 0 deletions plugin/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2285,6 +2285,13 @@ func TestValidator_ValidateJSONYAML(t *testing.T) {
}),
expectedErr: "api_specification is neither valid json nor valid yaml",
},
{
name: "rejects invalid json content",
config: jsonify(map[string]string{
"spec": "{\"openapi\":\"3.0.0\",foo:}",
omegabytes marked this conversation as resolved.
Show resolved Hide resolved
}),
expectedErr: "api_specification is neither valid json nor valid yaml",
},
{
name: "accepts valid yaml content",
config: jsonify(map[string]string{
Expand Down