mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-07 20:47:45 +00:00
Added support for unevaluatedProperties as Schema and bool #118
Also ran `gofmt` across the entire project. Things need cleaning up. Signed-off-by: Dave Shanley <dave@quobix.com>
This commit is contained in:
@@ -4,127 +4,127 @@
|
||||
package high
|
||||
|
||||
import (
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/yaml.v3"
|
||||
"testing"
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/yaml.v3"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExtractExtensions(t *testing.T) {
|
||||
n := make(map[low.KeyReference[string]]low.ValueReference[any])
|
||||
n[low.KeyReference[string]{
|
||||
Value: "pb33f",
|
||||
}] = low.ValueReference[any]{
|
||||
Value: "new cowboy in town",
|
||||
}
|
||||
ext := ExtractExtensions(n)
|
||||
assert.Equal(t, "new cowboy in town", ext["pb33f"])
|
||||
n := make(map[low.KeyReference[string]]low.ValueReference[any])
|
||||
n[low.KeyReference[string]{
|
||||
Value: "pb33f",
|
||||
}] = low.ValueReference[any]{
|
||||
Value: "new cowboy in town",
|
||||
}
|
||||
ext := ExtractExtensions(n)
|
||||
assert.Equal(t, "new cowboy in town", ext["pb33f"])
|
||||
}
|
||||
|
||||
type textExtension struct {
|
||||
Cowboy string
|
||||
Power int
|
||||
Cowboy string
|
||||
Power int
|
||||
}
|
||||
|
||||
type parent struct {
|
||||
low *child
|
||||
low *child
|
||||
}
|
||||
|
||||
func (p *parent) GoLow() *child {
|
||||
return p.low
|
||||
return p.low
|
||||
}
|
||||
|
||||
type child struct {
|
||||
Extensions map[low.KeyReference[string]]low.ValueReference[any]
|
||||
Extensions map[low.KeyReference[string]]low.ValueReference[any]
|
||||
}
|
||||
|
||||
func (c *child) GetExtensions() map[low.KeyReference[string]]low.ValueReference[any] {
|
||||
return c.Extensions
|
||||
return c.Extensions
|
||||
}
|
||||
|
||||
func TestUnpackExtensions(t *testing.T) {
|
||||
|
||||
var resultA, resultB yaml.Node
|
||||
var resultA, resultB yaml.Node
|
||||
|
||||
ymlA := `
|
||||
ymlA := `
|
||||
cowboy: buckaroo
|
||||
power: 100`
|
||||
|
||||
ymlB := `
|
||||
ymlB := `
|
||||
cowboy: frogman
|
||||
power: 2`
|
||||
|
||||
err := yaml.Unmarshal([]byte(ymlA), &resultA)
|
||||
assert.NoError(t, err)
|
||||
err = yaml.Unmarshal([]byte(ymlB), &resultB)
|
||||
assert.NoError(t, err)
|
||||
err := yaml.Unmarshal([]byte(ymlA), &resultA)
|
||||
assert.NoError(t, err)
|
||||
err = yaml.Unmarshal([]byte(ymlB), &resultB)
|
||||
assert.NoError(t, err)
|
||||
|
||||
n := make(map[low.KeyReference[string]]low.ValueReference[any])
|
||||
n[low.KeyReference[string]{
|
||||
Value: "x-rancher-a",
|
||||
}] = low.ValueReference[any]{
|
||||
ValueNode: resultA.Content[0],
|
||||
}
|
||||
n := make(map[low.KeyReference[string]]low.ValueReference[any])
|
||||
n[low.KeyReference[string]{
|
||||
Value: "x-rancher-a",
|
||||
}] = low.ValueReference[any]{
|
||||
ValueNode: resultA.Content[0],
|
||||
}
|
||||
|
||||
n[low.KeyReference[string]{
|
||||
Value: "x-rancher-b",
|
||||
}] = low.ValueReference[any]{
|
||||
ValueNode: resultB.Content[0],
|
||||
}
|
||||
n[low.KeyReference[string]{
|
||||
Value: "x-rancher-b",
|
||||
}] = low.ValueReference[any]{
|
||||
ValueNode: resultB.Content[0],
|
||||
}
|
||||
|
||||
c := new(child)
|
||||
c.Extensions = n
|
||||
c := new(child)
|
||||
c.Extensions = n
|
||||
|
||||
p := new(parent)
|
||||
p.low = c
|
||||
p := new(parent)
|
||||
p.low = c
|
||||
|
||||
res, err := UnpackExtensions[textExtension, *child](p)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, res)
|
||||
assert.Equal(t, "buckaroo", res["x-rancher-a"].Cowboy)
|
||||
assert.Equal(t, 100, res["x-rancher-a"].Power)
|
||||
assert.Equal(t, "frogman", res["x-rancher-b"].Cowboy)
|
||||
assert.Equal(t, 2, res["x-rancher-b"].Power)
|
||||
res, err := UnpackExtensions[textExtension, *child](p)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, res)
|
||||
assert.Equal(t, "buckaroo", res["x-rancher-a"].Cowboy)
|
||||
assert.Equal(t, 100, res["x-rancher-a"].Power)
|
||||
assert.Equal(t, "frogman", res["x-rancher-b"].Cowboy)
|
||||
assert.Equal(t, 2, res["x-rancher-b"].Power)
|
||||
}
|
||||
|
||||
func TestUnpackExtensions_Fail(t *testing.T) {
|
||||
|
||||
var resultA, resultB yaml.Node
|
||||
var resultA, resultB yaml.Node
|
||||
|
||||
ymlA := `
|
||||
ymlA := `
|
||||
cowboy: buckaroo
|
||||
power: 100`
|
||||
|
||||
// this is incorrect types, unpacking will fail.
|
||||
ymlB := `
|
||||
// this is incorrect types, unpacking will fail.
|
||||
ymlB := `
|
||||
cowboy: 0
|
||||
power: hello`
|
||||
|
||||
err := yaml.Unmarshal([]byte(ymlA), &resultA)
|
||||
assert.NoError(t, err)
|
||||
err = yaml.Unmarshal([]byte(ymlB), &resultB)
|
||||
assert.NoError(t, err)
|
||||
err := yaml.Unmarshal([]byte(ymlA), &resultA)
|
||||
assert.NoError(t, err)
|
||||
err = yaml.Unmarshal([]byte(ymlB), &resultB)
|
||||
assert.NoError(t, err)
|
||||
|
||||
n := make(map[low.KeyReference[string]]low.ValueReference[any])
|
||||
n[low.KeyReference[string]{
|
||||
Value: "x-rancher-a",
|
||||
}] = low.ValueReference[any]{
|
||||
ValueNode: resultA.Content[0],
|
||||
}
|
||||
n := make(map[low.KeyReference[string]]low.ValueReference[any])
|
||||
n[low.KeyReference[string]{
|
||||
Value: "x-rancher-a",
|
||||
}] = low.ValueReference[any]{
|
||||
ValueNode: resultA.Content[0],
|
||||
}
|
||||
|
||||
n[low.KeyReference[string]{
|
||||
Value: "x-rancher-b",
|
||||
}] = low.ValueReference[any]{
|
||||
ValueNode: resultB.Content[0],
|
||||
}
|
||||
n[low.KeyReference[string]{
|
||||
Value: "x-rancher-b",
|
||||
}] = low.ValueReference[any]{
|
||||
ValueNode: resultB.Content[0],
|
||||
}
|
||||
|
||||
c := new(child)
|
||||
c.Extensions = n
|
||||
c := new(child)
|
||||
c.Extensions = n
|
||||
|
||||
p := new(parent)
|
||||
p.low = c
|
||||
p := new(parent)
|
||||
p.low = c
|
||||
|
||||
res, er := UnpackExtensions[textExtension, *child](p)
|
||||
assert.Error(t, er)
|
||||
assert.Empty(t, res)
|
||||
}
|
||||
res, er := UnpackExtensions[textExtension, *child](p)
|
||||
assert.Error(t, er)
|
||||
assert.Empty(t, res)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user