mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-10 12:37:48 +00:00
Datamodel now at 100% coverage
working with k8s, stripe, petstore and locals. Speed is as good as I can make it at this point, not without further performance tunupes for memory consumption. less copying everywhere.
This commit is contained in:
21
datamodel/high/shared_test.go
Normal file
21
datamodel/high/shared_test.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package high
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/pb33f/libopenapi/datamodel/low"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"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"])
|
||||||
|
}
|
||||||
@@ -955,9 +955,6 @@ func TestExtractSchema_CheckChildPropCircular(t *testing.T) {
|
|||||||
props := res.Value.Schema().FindProperty("nothing")
|
props := res.Value.Schema().FindProperty("nothing")
|
||||||
assert.NotNil(t, props)
|
assert.NotNil(t, props)
|
||||||
|
|
||||||
//n := props.Value.Schema()
|
|
||||||
//assert.NotNil(t, n)
|
|
||||||
//assert.Equal(t, "this is something", res.Value.Schema().Description.Value)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExtractSchema_RefRoot(t *testing.T) {
|
func TestExtractSchema_RefRoot(t *testing.T) {
|
||||||
|
|||||||
@@ -210,27 +210,6 @@ func TestSetField_NodeRefAny_Error(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetField_MapStringAny_Error(t *testing.T) {
|
|
||||||
|
|
||||||
type internal struct {
|
|
||||||
Thing map[string]NodeReference[any]
|
|
||||||
}
|
|
||||||
|
|
||||||
yml := `thing:
|
|
||||||
thang:
|
|
||||||
tang:
|
|
||||||
bang:`
|
|
||||||
|
|
||||||
ins := new(internal)
|
|
||||||
var rootNode yaml.Node
|
|
||||||
mErr := yaml.Unmarshal([]byte(yml), &rootNode)
|
|
||||||
assert.NoError(t, mErr)
|
|
||||||
|
|
||||||
try := BuildModel(&rootNode, ins)
|
|
||||||
assert.Error(t, try)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSetField_MapHelperWrapped(t *testing.T) {
|
func TestSetField_MapHelperWrapped(t *testing.T) {
|
||||||
|
|
||||||
type internal struct {
|
type internal struct {
|
||||||
|
|||||||
@@ -65,6 +65,11 @@ func IsCircular(node *yaml.Node, idx *index.SpecIndex) bool {
|
|||||||
if refs[i].LoopPoint.Node == node {
|
if refs[i].LoopPoint.Node == node {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
for k := range refs[i].Journey {
|
||||||
|
if refs[i].Journey[k].Node == node {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// check mapped references in case we didn't find it.
|
// check mapped references in case we didn't find it.
|
||||||
_, nv := utils.FindKeyNode("$ref", node.Content)
|
_, nv := utils.FindKeyNode("$ref", node.Content)
|
||||||
@@ -86,6 +91,11 @@ func GetCircularReferenceResult(node *yaml.Node, idx *index.SpecIndex) *index.Ci
|
|||||||
if refs[i].LoopPoint.Node == node {
|
if refs[i].LoopPoint.Node == node {
|
||||||
return refs[i]
|
return refs[i]
|
||||||
}
|
}
|
||||||
|
for k := range refs[i].Journey {
|
||||||
|
if refs[i].Journey[k].Node == node {
|
||||||
|
return refs[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// check mapped references in case we didn't find it.
|
// check mapped references in case we didn't find it.
|
||||||
_, nv := utils.FindKeyNode("$ref", node.Content)
|
_, nv := utils.FindKeyNode("$ref", node.Content)
|
||||||
|
|||||||
289
datamodel/low/reference_test.go
Normal file
289
datamodel/low/reference_test.go
Normal file
@@ -0,0 +1,289 @@
|
|||||||
|
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package low
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/pb33f/libopenapi/index"
|
||||||
|
"github.com/pb33f/libopenapi/resolver"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNodeReference_IsEmpty(t *testing.T) {
|
||||||
|
nr := new(NodeReference[string])
|
||||||
|
assert.True(t, nr.IsEmpty())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNodeReference_GenerateMapKey(t *testing.T) {
|
||||||
|
nr := new(NodeReference[string])
|
||||||
|
nr.ValueNode = &yaml.Node{
|
||||||
|
Line: 22,
|
||||||
|
Column: 23,
|
||||||
|
}
|
||||||
|
assert.Equal(t, "22:23", nr.GenerateMapKey())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValueReference_IsEmpty(t *testing.T) {
|
||||||
|
nr := new(ValueReference[string])
|
||||||
|
assert.True(t, nr.IsEmpty())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValueReference_GenerateMapKey(t *testing.T) {
|
||||||
|
nr := new(ValueReference[string])
|
||||||
|
nr.ValueNode = &yaml.Node{
|
||||||
|
Line: 22,
|
||||||
|
Column: 23,
|
||||||
|
}
|
||||||
|
assert.Equal(t, "22:23", nr.GenerateMapKey())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestKeyReference_IsEmpty(t *testing.T) {
|
||||||
|
nr := new(KeyReference[string])
|
||||||
|
assert.True(t, nr.IsEmpty())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestKeyReference_GenerateMapKey(t *testing.T) {
|
||||||
|
nr := new(KeyReference[string])
|
||||||
|
nr.KeyNode = &yaml.Node{
|
||||||
|
Line: 22,
|
||||||
|
Column: 23,
|
||||||
|
}
|
||||||
|
assert.Equal(t, "22:23", nr.GenerateMapKey())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsCircular_LookupFromJourney(t *testing.T) {
|
||||||
|
|
||||||
|
yml := `components:
|
||||||
|
schemas:
|
||||||
|
Something:
|
||||||
|
properties:
|
||||||
|
nothing:
|
||||||
|
$ref: '#/components/schemas/Nothing'
|
||||||
|
Nothing:
|
||||||
|
properties:
|
||||||
|
something:
|
||||||
|
$ref: '#/components/schemas/Something'`
|
||||||
|
|
||||||
|
var iNode yaml.Node
|
||||||
|
mErr := yaml.Unmarshal([]byte(yml), &iNode)
|
||||||
|
assert.NoError(t, mErr)
|
||||||
|
idx := index.NewSpecIndex(&iNode)
|
||||||
|
|
||||||
|
yml = `$ref: '#/components/schemas/Something'`
|
||||||
|
|
||||||
|
resolve := resolver.NewResolver(idx)
|
||||||
|
errs := resolve.CheckForCircularReferences()
|
||||||
|
assert.Len(t, errs, 1)
|
||||||
|
|
||||||
|
var idxNode yaml.Node
|
||||||
|
_ = yaml.Unmarshal([]byte(yml), &idxNode)
|
||||||
|
|
||||||
|
ref, err := LocateRefNode(idxNode.Content[0], idx)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, IsCircular(ref, idx))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsCircular_LookupFromLoopPoint(t *testing.T) {
|
||||||
|
|
||||||
|
yml := `components:
|
||||||
|
schemas:
|
||||||
|
Something:
|
||||||
|
properties:
|
||||||
|
nothing:
|
||||||
|
$ref: '#/components/schemas/Nothing'
|
||||||
|
Nothing:
|
||||||
|
properties:
|
||||||
|
something:
|
||||||
|
$ref: '#/components/schemas/Something'`
|
||||||
|
|
||||||
|
var iNode yaml.Node
|
||||||
|
mErr := yaml.Unmarshal([]byte(yml), &iNode)
|
||||||
|
assert.NoError(t, mErr)
|
||||||
|
idx := index.NewSpecIndex(&iNode)
|
||||||
|
|
||||||
|
yml = `$ref: '#/components/schemas/Nothing'`
|
||||||
|
|
||||||
|
resolve := resolver.NewResolver(idx)
|
||||||
|
errs := resolve.CheckForCircularReferences()
|
||||||
|
assert.Len(t, errs, 1)
|
||||||
|
|
||||||
|
var idxNode yaml.Node
|
||||||
|
_ = yaml.Unmarshal([]byte(yml), &idxNode)
|
||||||
|
|
||||||
|
ref, err := LocateRefNode(idxNode.Content[0], idx)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, IsCircular(ref, idx))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsCircular_FromRefLookup(t *testing.T) {
|
||||||
|
|
||||||
|
yml := `components:
|
||||||
|
schemas:
|
||||||
|
NotCircle:
|
||||||
|
description: not a circle
|
||||||
|
Something:
|
||||||
|
properties:
|
||||||
|
nothing:
|
||||||
|
$ref: '#/components/schemas/Nothing'
|
||||||
|
Nothing:
|
||||||
|
properties:
|
||||||
|
something:
|
||||||
|
$ref: '#/components/schemas/Something'`
|
||||||
|
|
||||||
|
var iNode yaml.Node
|
||||||
|
mErr := yaml.Unmarshal([]byte(yml), &iNode)
|
||||||
|
assert.NoError(t, mErr)
|
||||||
|
idx := index.NewSpecIndex(&iNode)
|
||||||
|
|
||||||
|
resolve := resolver.NewResolver(idx)
|
||||||
|
errs := resolve.CheckForCircularReferences()
|
||||||
|
assert.Len(t, errs, 1)
|
||||||
|
|
||||||
|
yml = `$ref: '#/components/schemas/Nothing'`
|
||||||
|
var idxNode yaml.Node
|
||||||
|
_ = yaml.Unmarshal([]byte(yml), &idxNode)
|
||||||
|
|
||||||
|
assert.True(t, IsCircular(idxNode.Content[0], idx))
|
||||||
|
|
||||||
|
yml = `$ref: '#/components/schemas/NotCircle'`
|
||||||
|
_ = yaml.Unmarshal([]byte(yml), &idxNode)
|
||||||
|
|
||||||
|
assert.False(t, IsCircular(idxNode.Content[0], idx))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsCircular_NoNode(t *testing.T) {
|
||||||
|
assert.False(t, IsCircular(nil, nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetCircularReferenceResult_NoNode(t *testing.T) {
|
||||||
|
assert.Nil(t, GetCircularReferenceResult(nil, nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetCircularReferenceResult_FromJourney(t *testing.T) {
|
||||||
|
|
||||||
|
yml := `components:
|
||||||
|
schemas:
|
||||||
|
Something:
|
||||||
|
properties:
|
||||||
|
nothing:
|
||||||
|
$ref: '#/components/schemas/Nothing'
|
||||||
|
Nothing:
|
||||||
|
properties:
|
||||||
|
something:
|
||||||
|
$ref: '#/components/schemas/Something'`
|
||||||
|
|
||||||
|
var iNode yaml.Node
|
||||||
|
mErr := yaml.Unmarshal([]byte(yml), &iNode)
|
||||||
|
assert.NoError(t, mErr)
|
||||||
|
idx := index.NewSpecIndex(&iNode)
|
||||||
|
|
||||||
|
yml = `$ref: '#/components/schemas/Something'`
|
||||||
|
|
||||||
|
resolve := resolver.NewResolver(idx)
|
||||||
|
errs := resolve.CheckForCircularReferences()
|
||||||
|
assert.Len(t, errs, 1)
|
||||||
|
|
||||||
|
var idxNode yaml.Node
|
||||||
|
_ = yaml.Unmarshal([]byte(yml), &idxNode)
|
||||||
|
|
||||||
|
ref, err := LocateRefNode(idxNode.Content[0], idx)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
circ := GetCircularReferenceResult(ref, idx)
|
||||||
|
assert.NotNil(t, circ)
|
||||||
|
assert.Equal(t, "Nothing -> Something -> Nothing", circ.GenerateJourneyPath())
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetCircularReferenceResult_FromLoopPoint(t *testing.T) {
|
||||||
|
|
||||||
|
yml := `components:
|
||||||
|
schemas:
|
||||||
|
Something:
|
||||||
|
properties:
|
||||||
|
nothing:
|
||||||
|
$ref: '#/components/schemas/Nothing'
|
||||||
|
Nothing:
|
||||||
|
properties:
|
||||||
|
something:
|
||||||
|
$ref: '#/components/schemas/Something'`
|
||||||
|
|
||||||
|
var iNode yaml.Node
|
||||||
|
mErr := yaml.Unmarshal([]byte(yml), &iNode)
|
||||||
|
assert.NoError(t, mErr)
|
||||||
|
idx := index.NewSpecIndex(&iNode)
|
||||||
|
|
||||||
|
yml = `$ref: '#/components/schemas/Nothing'`
|
||||||
|
|
||||||
|
resolve := resolver.NewResolver(idx)
|
||||||
|
errs := resolve.CheckForCircularReferences()
|
||||||
|
assert.Len(t, errs, 1)
|
||||||
|
|
||||||
|
var idxNode yaml.Node
|
||||||
|
_ = yaml.Unmarshal([]byte(yml), &idxNode)
|
||||||
|
|
||||||
|
ref, err := LocateRefNode(idxNode.Content[0], idx)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
circ := GetCircularReferenceResult(ref, idx)
|
||||||
|
assert.NotNil(t, circ)
|
||||||
|
assert.Equal(t, "Nothing -> Something -> Nothing", circ.GenerateJourneyPath())
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetCircularReferenceResult_FromMappedRef(t *testing.T) {
|
||||||
|
|
||||||
|
yml := `components:
|
||||||
|
schemas:
|
||||||
|
Something:
|
||||||
|
properties:
|
||||||
|
nothing:
|
||||||
|
$ref: '#/components/schemas/Nothing'
|
||||||
|
Nothing:
|
||||||
|
properties:
|
||||||
|
something:
|
||||||
|
$ref: '#/components/schemas/Something'`
|
||||||
|
|
||||||
|
var iNode yaml.Node
|
||||||
|
mErr := yaml.Unmarshal([]byte(yml), &iNode)
|
||||||
|
assert.NoError(t, mErr)
|
||||||
|
idx := index.NewSpecIndex(&iNode)
|
||||||
|
|
||||||
|
yml = `$ref: '#/components/schemas/Nothing'`
|
||||||
|
|
||||||
|
resolve := resolver.NewResolver(idx)
|
||||||
|
errs := resolve.CheckForCircularReferences()
|
||||||
|
assert.Len(t, errs, 1)
|
||||||
|
|
||||||
|
var idxNode yaml.Node
|
||||||
|
_ = yaml.Unmarshal([]byte(yml), &idxNode)
|
||||||
|
|
||||||
|
circ := GetCircularReferenceResult(idxNode.Content[0], idx)
|
||||||
|
assert.NotNil(t, circ)
|
||||||
|
assert.Equal(t, "Nothing -> Something -> Nothing", circ.GenerateJourneyPath())
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetCircularReferenceResult_NothingFound(t *testing.T) {
|
||||||
|
|
||||||
|
yml := `components:
|
||||||
|
schemas:
|
||||||
|
NotCircle:
|
||||||
|
description: not a circle`
|
||||||
|
|
||||||
|
var iNode yaml.Node
|
||||||
|
mErr := yaml.Unmarshal([]byte(yml), &iNode)
|
||||||
|
assert.NoError(t, mErr)
|
||||||
|
idx := index.NewSpecIndex(&iNode)
|
||||||
|
|
||||||
|
resolve := resolver.NewResolver(idx)
|
||||||
|
errs := resolve.CheckForCircularReferences()
|
||||||
|
assert.Len(t, errs, 0)
|
||||||
|
|
||||||
|
var idxNode yaml.Node
|
||||||
|
yml = `$ref: '#/components/schemas/NotCircle'`
|
||||||
|
_ = yaml.Unmarshal([]byte(yml), &idxNode)
|
||||||
|
|
||||||
|
assert.Nil(t, GetCircularReferenceResult(idxNode.Content[0], idx))
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
|
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
package datamodel
|
package datamodel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
18
datamodel/spec_info_test.go
Normal file
18
datamodel/spec_info_test.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package datamodel
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSpecInfo_GetJSONParsingChannel(t *testing.T) {
|
||||||
|
|
||||||
|
// dumb, but we need to ensure coverage is as high as we can make it.
|
||||||
|
bchan := make(chan bool)
|
||||||
|
si := &SpecInfo{JsonParsingChannel: bchan}
|
||||||
|
assert.Equal(t, si.GetJSONParsingChannel(), bchan)
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user