mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-07 20:47:45 +00:00
fix: Update tests where appropriate
This commit is contained in:
committed by
Dave Shanley
parent
1f2709fef3
commit
5f9541283a
@@ -5,13 +5,14 @@ package v3
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/pb33f/libopenapi/datamodel"
|
||||
v2 "github.com/pb33f/libopenapi/datamodel/high/v2"
|
||||
lowv2 "github.com/pb33f/libopenapi/datamodel/low/v2"
|
||||
lowv3 "github.com/pb33f/libopenapi/datamodel/low/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var lowDoc *lowv3.Document
|
||||
@@ -51,7 +52,6 @@ func TestNewDocument_Security(t *testing.T) {
|
||||
assert.Len(t, h.Security, 1)
|
||||
assert.Len(t, h.Security[0].Requirements, 1)
|
||||
assert.Len(t, h.Security[0].Requirements["OAuthScheme"], 2)
|
||||
|
||||
}
|
||||
|
||||
func TestNewDocument_Info(t *testing.T) {
|
||||
@@ -83,7 +83,6 @@ func TestNewDocument_Info(t *testing.T) {
|
||||
wentOnceMore := highDoc.Info.License.GoLow()
|
||||
assert.Equal(t, 12, wentOnceMore.Name.ValueNode.Line)
|
||||
assert.Equal(t, 11, wentOnceMore.Name.ValueNode.Column)
|
||||
|
||||
}
|
||||
|
||||
func TestNewDocument_Servers(t *testing.T) {
|
||||
@@ -115,7 +114,6 @@ func TestNewDocument_Servers(t *testing.T) {
|
||||
wentLowest := h.Servers[0].Variables["scheme"].GoLow()
|
||||
assert.Equal(t, 50, wentLowest.Description.ValueNode.Line)
|
||||
assert.Equal(t, 22, wentLowest.Description.ValueNode.Column)
|
||||
|
||||
}
|
||||
|
||||
func TestNewDocument_Tags(t *testing.T) {
|
||||
@@ -157,20 +155,27 @@ func TestNewDocument_Components_Links(t *testing.T) {
|
||||
wentLow := h.Components.Links["LocateBurger"].GoLow()
|
||||
assert.Equal(t, 305, wentLow.OperationId.ValueNode.Line)
|
||||
assert.Equal(t, 20, wentLow.OperationId.ValueNode.Column)
|
||||
|
||||
}
|
||||
|
||||
func TestNewDocument_Components_Callbacks(t *testing.T) {
|
||||
initTest()
|
||||
h := NewDocument(lowDoc)
|
||||
assert.Len(t, h.Components.Callbacks, 1)
|
||||
assert.Equal(t, "Callback payload",
|
||||
h.Components.Callbacks["BurgerCallback"].Expression["{$request.query.queryUrl}"].Post.RequestBody.Description)
|
||||
|
||||
assert.Equal(t, 293,
|
||||
h.Components.Callbacks["BurgerCallback"].GoLow().FindExpression("{$request.query.queryUrl}").ValueNode.Line)
|
||||
assert.Equal(t, 9,
|
||||
h.Components.Callbacks["BurgerCallback"].GoLow().FindExpression("{$request.query.queryUrl}").ValueNode.Column)
|
||||
assert.Equal(
|
||||
t,
|
||||
"Callback payload",
|
||||
h.Components.Callbacks["BurgerCallback"].Expression["{$request.query.queryUrl}"].Post.RequestBody.Description,
|
||||
)
|
||||
assert.Equal(
|
||||
t,
|
||||
293,
|
||||
h.Components.Callbacks["BurgerCallback"].GoLow().FindExpression("{$request.query.queryUrl}").ValueNode.Line,
|
||||
)
|
||||
assert.Equal(
|
||||
t,
|
||||
9,
|
||||
h.Components.Callbacks["BurgerCallback"].GoLow().FindExpression("{$request.query.queryUrl}").ValueNode.Column,
|
||||
)
|
||||
|
||||
assert.Equal(t, "please", h.Components.Callbacks["BurgerCallback"].Extensions["x-break-everything"])
|
||||
|
||||
@@ -250,7 +255,6 @@ func TestNewDocument_Components_Examples(t *testing.T) {
|
||||
assert.Equal(t, "A juicy two hander sammich", h.Components.Examples["QuarterPounder"].Summary)
|
||||
assert.Equal(t, 341, h.Components.Examples["QuarterPounder"].GoLow().Summary.ValueNode.Line)
|
||||
assert.Equal(t, 16, h.Components.Examples["QuarterPounder"].GoLow().Summary.ValueNode.Column)
|
||||
|
||||
}
|
||||
|
||||
func TestNewDocument_Components_Responses(t *testing.T) {
|
||||
@@ -291,7 +295,6 @@ func TestNewDocument_Components_SecuritySchemes(t *testing.T) {
|
||||
assert.Equal(t, 11, oAuth.Flows.GoLow().Implicit.Value.Scopes.KeyNode.Column)
|
||||
assert.Equal(t, 375, oAuth.Flows.Implicit.GoLow().Scopes.KeyNode.Line)
|
||||
assert.Equal(t, 11, oAuth.Flows.Implicit.GoLow().Scopes.KeyNode.Column)
|
||||
|
||||
}
|
||||
|
||||
func TestNewDocument_Components_Parameters(t *testing.T) {
|
||||
@@ -304,11 +307,13 @@ func TestNewDocument_Components_Parameters(t *testing.T) {
|
||||
assert.Len(t, bh.Schema.Schema().Properties, 2)
|
||||
assert.Equal(t, "big-mac", bh.Example)
|
||||
assert.True(t, bh.Required)
|
||||
assert.Equal(t, "this is a header",
|
||||
bh.Content["application/json"].Encoding["burgerTheme"].Headers["someHeader"].Description)
|
||||
assert.Equal(
|
||||
t,
|
||||
"this is a header",
|
||||
bh.Content["application/json"].Encoding["burgerTheme"].Headers["someHeader"].Description,
|
||||
)
|
||||
assert.Len(t, bh.Content["application/json"].Schema.Schema().Properties, 2)
|
||||
assert.Equal(t, 404, bh.Content["application/json"].Encoding["burgerTheme"].GoLow().ContentType.ValueNode.Line)
|
||||
|
||||
}
|
||||
|
||||
func TestNewDocument_Paths(t *testing.T) {
|
||||
@@ -339,8 +344,11 @@ func TestNewDocument_Paths(t *testing.T) {
|
||||
assert.Equal(t, "A tasty burger for you to eat.", okResp.Description)
|
||||
assert.Equal(t, 69, burgersOp.Post.GoLow().Description.ValueNode.Line)
|
||||
assert.Len(t, okResp.Content["application/json"].Examples, 2)
|
||||
assert.Equal(t, "a cripsy fish sammich filled with ocean goodness.",
|
||||
okResp.Content["application/json"].Examples["filetOFish"].Summary)
|
||||
assert.Equal(
|
||||
t,
|
||||
"a cripsy fish sammich filled with ocean goodness.",
|
||||
okResp.Content["application/json"].Examples["filetOFish"].Summary,
|
||||
)
|
||||
assert.Equal(t, 74, burgersOp.Post.Responses.GoLow().FindResponseByCode("200").ValueNode.Line)
|
||||
|
||||
assert.Equal(t, 80, okResp.Content["application/json"].GoLow().Schema.KeyNode.Line)
|
||||
@@ -356,7 +364,6 @@ func TestNewDocument_Paths(t *testing.T) {
|
||||
assert.Equal(t, 118, burgersOp.Post.Security[0].GoLow().Requirements.ValueNode.Line)
|
||||
assert.Len(t, burgersOp.Post.Servers, 1)
|
||||
assert.Equal(t, "https://pb33f.io", burgersOp.Post.Servers[0].URL)
|
||||
|
||||
}
|
||||
|
||||
func TestStripeAsDoc(t *testing.T) {
|
||||
@@ -364,7 +371,7 @@ func TestStripeAsDoc(t *testing.T) {
|
||||
info, _ := datamodel.ExtractSpecInfo(data)
|
||||
var err []error
|
||||
lowDoc, err = lowv3.CreateDocument(info)
|
||||
assert.Len(t, err, 23)
|
||||
assert.Len(t, err, 3)
|
||||
d := NewDocument(lowDoc)
|
||||
assert.NotNil(t, d)
|
||||
}
|
||||
@@ -375,9 +382,8 @@ func TestK8sAsDoc(t *testing.T) {
|
||||
var err []error
|
||||
lowSwag, err := lowv2.CreateDocument(info)
|
||||
d := v2.NewSwaggerDocument(lowSwag)
|
||||
assert.Len(t, err, 1)
|
||||
assert.Len(t, err, 0)
|
||||
assert.NotNil(t, d)
|
||||
|
||||
}
|
||||
|
||||
func TestAsanaAsDoc(t *testing.T) {
|
||||
|
||||
@@ -1154,10 +1154,15 @@ func TestExtractSchema_CheckChildPropCircular(t *testing.T) {
|
||||
properties:
|
||||
nothing:
|
||||
$ref: '#/components/schemas/Nothing'
|
||||
required:
|
||||
- nothing
|
||||
Nothing:
|
||||
properties:
|
||||
something:
|
||||
$ref: '#/components/schemas/Something'`
|
||||
$ref: '#/components/schemas/Something'
|
||||
required:
|
||||
- something
|
||||
`
|
||||
|
||||
var iNode yaml.Node
|
||||
mErr := yaml.Unmarshal([]byte(yml), &iNode)
|
||||
|
||||
@@ -6,13 +6,14 @@ package low
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/pb33f/libopenapi/index"
|
||||
"github.com/pb33f/libopenapi/resolver"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/yaml.v3"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFindItemInMap(t *testing.T) {
|
||||
@@ -218,7 +219,6 @@ func TestExtractObject_DoubleRef(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractObject_DoubleRef_Circular(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
loopy:
|
||||
@@ -249,7 +249,6 @@ func TestExtractObject_DoubleRef_Circular(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractObject_DoubleRef_Circular_Fail(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
loopy:
|
||||
|
||||
@@ -5,11 +5,12 @@ package low
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"testing"
|
||||
|
||||
"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) {
|
||||
@@ -103,10 +104,15 @@ func TestIsCircular_LookupFromJourney(t *testing.T) {
|
||||
properties:
|
||||
nothing:
|
||||
$ref: '#/components/schemas/Nothing'
|
||||
required:
|
||||
- nothing
|
||||
Nothing:
|
||||
properties:
|
||||
something:
|
||||
$ref: '#/components/schemas/Something'`
|
||||
$ref: '#/components/schemas/Something'
|
||||
required:
|
||||
- something
|
||||
`
|
||||
|
||||
var iNode yaml.Node
|
||||
mErr := yaml.Unmarshal([]byte(yml), &iNode)
|
||||
@@ -135,10 +141,15 @@ func TestIsCircular_LookupFromLoopPoint(t *testing.T) {
|
||||
properties:
|
||||
nothing:
|
||||
$ref: '#/components/schemas/Nothing'
|
||||
required:
|
||||
- nothing
|
||||
Nothing:
|
||||
properties:
|
||||
something:
|
||||
$ref: '#/components/schemas/Something'`
|
||||
$ref: '#/components/schemas/Something'
|
||||
required:
|
||||
- something
|
||||
`
|
||||
|
||||
var iNode yaml.Node
|
||||
mErr := yaml.Unmarshal([]byte(yml), &iNode)
|
||||
@@ -169,10 +180,15 @@ func TestIsCircular_FromRefLookup(t *testing.T) {
|
||||
properties:
|
||||
nothing:
|
||||
$ref: '#/components/schemas/Nothing'
|
||||
required:
|
||||
- nothing
|
||||
Nothing:
|
||||
properties:
|
||||
something:
|
||||
$ref: '#/components/schemas/Something'`
|
||||
$ref: '#/components/schemas/Something'
|
||||
required:
|
||||
- something
|
||||
`
|
||||
|
||||
var iNode yaml.Node
|
||||
mErr := yaml.Unmarshal([]byte(yml), &iNode)
|
||||
@@ -211,10 +227,15 @@ func TestGetCircularReferenceResult_FromJourney(t *testing.T) {
|
||||
properties:
|
||||
nothing:
|
||||
$ref: '#/components/schemas/Nothing'
|
||||
required:
|
||||
- nothing
|
||||
Nothing:
|
||||
properties:
|
||||
something:
|
||||
$ref: '#/components/schemas/Something'`
|
||||
$ref: '#/components/schemas/Something'
|
||||
required:
|
||||
- something
|
||||
`
|
||||
|
||||
var iNode yaml.Node
|
||||
mErr := yaml.Unmarshal([]byte(yml), &iNode)
|
||||
@@ -246,10 +267,15 @@ func TestGetCircularReferenceResult_FromLoopPoint(t *testing.T) {
|
||||
properties:
|
||||
nothing:
|
||||
$ref: '#/components/schemas/Nothing'
|
||||
required:
|
||||
- nothing
|
||||
Nothing:
|
||||
properties:
|
||||
something:
|
||||
$ref: '#/components/schemas/Something'`
|
||||
$ref: '#/components/schemas/Something'
|
||||
required:
|
||||
- something
|
||||
`
|
||||
|
||||
var iNode yaml.Node
|
||||
mErr := yaml.Unmarshal([]byte(yml), &iNode)
|
||||
@@ -281,10 +307,15 @@ func TestGetCircularReferenceResult_FromMappedRef(t *testing.T) {
|
||||
properties:
|
||||
nothing:
|
||||
$ref: '#/components/schemas/Nothing'
|
||||
required:
|
||||
- nothing
|
||||
Nothing:
|
||||
properties:
|
||||
something:
|
||||
$ref: '#/components/schemas/Something'`
|
||||
$ref: '#/components/schemas/Something'
|
||||
required:
|
||||
- something
|
||||
`
|
||||
|
||||
var iNode yaml.Node
|
||||
mErr := yaml.Unmarshal([]byte(yml), &iNode)
|
||||
|
||||
@@ -2,11 +2,12 @@ package v3
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/pb33f/libopenapi/datamodel"
|
||||
"github.com/pb33f/libopenapi/datamodel/low/base"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var doc *Document
|
||||
@@ -94,7 +95,7 @@ func TestCreateDocumentStripe(t *testing.T) {
|
||||
data, _ := ioutil.ReadFile("../../../test_specs/stripe.yaml")
|
||||
info, _ := datamodel.ExtractSpecInfo(data)
|
||||
d, err := CreateDocument(info)
|
||||
assert.Len(t, err, 23)
|
||||
assert.Len(t, err, 3)
|
||||
|
||||
assert.Equal(t, "3.0.0", d.Version.Value)
|
||||
assert.Equal(t, "Stripe API", d.Info.Value.Title.Value)
|
||||
|
||||
@@ -4,12 +4,13 @@
|
||||
package v3
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
"github.com/pb33f/libopenapi/index"
|
||||
"github.com/pb33f/libopenapi/resolver"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/yaml.v3"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPaths_Build(t *testing.T) {
|
||||
@@ -374,6 +375,43 @@ func TestPath_Build_Using_CircularRef(t *testing.T) {
|
||||
assert.Error(t, err)
|
||||
|
||||
}
|
||||
|
||||
func TestPath_Build_Using_CircularRef_Invalid(t *testing.T) {
|
||||
|
||||
// first we need an index.
|
||||
yml := `paths:
|
||||
'/something/here':
|
||||
post:
|
||||
$ref: '#/paths/~1something~1there/post'
|
||||
'/something/there':
|
||||
post:
|
||||
$ref: '#/paths/~1something~1here/post'`
|
||||
|
||||
var idxNode yaml.Node
|
||||
mErr := yaml.Unmarshal([]byte(yml), &idxNode)
|
||||
assert.NoError(t, mErr)
|
||||
idx := index.NewSpecIndex(&idxNode)
|
||||
|
||||
resolve := resolver.NewResolver(idx)
|
||||
errs := resolve.CheckForCircularReferences()
|
||||
assert.Len(t, errs, 1)
|
||||
|
||||
yml = `"/some/path":
|
||||
$ref: '#/paths/~1something~1here/post'`
|
||||
|
||||
var rootNode yaml.Node
|
||||
mErr = yaml.Unmarshal([]byte(yml), &rootNode)
|
||||
assert.NoError(t, mErr)
|
||||
|
||||
var n Paths
|
||||
err := low.BuildModel(rootNode.Content[0], &n)
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = n.Build(rootNode.Content[0], idx)
|
||||
assert.Error(t, err)
|
||||
|
||||
}
|
||||
|
||||
func TestPath_Build_Using_CircularRefWithOp(t *testing.T) {
|
||||
|
||||
// first we need an index.
|
||||
|
||||
@@ -5,15 +5,16 @@ package libopenapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/pb33f/libopenapi/datamodel/high"
|
||||
low "github.com/pb33f/libopenapi/datamodel/low/base"
|
||||
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
|
||||
"github.com/pb33f/libopenapi/resolver"
|
||||
"github.com/pb33f/libopenapi/utils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLoadDocument_Simple_V2(t *testing.T) {
|
||||
@@ -518,7 +519,7 @@ func TestDocument_Paths_As_Array(t *testing.T) {
|
||||
// If you want to know more about circular references that have been found
|
||||
// during the parsing/indexing/building of a document, you can capture the
|
||||
// []errors thrown which are pointers to *resolver.ResolvingError
|
||||
func ExampleNewDocument_circular_references() {
|
||||
func ExampleNewDocument_infinite_circular_references() {
|
||||
|
||||
// create a specification with an obvious and deliberate circular reference
|
||||
spec := `openapi: "3.1"
|
||||
@@ -529,11 +530,15 @@ components:
|
||||
properties:
|
||||
things:
|
||||
"$ref": "#/components/schemas/Two"
|
||||
required:
|
||||
- things
|
||||
Two:
|
||||
description: "test two"
|
||||
properties:
|
||||
testThing:
|
||||
"$ref": "#/components/schemas/One"
|
||||
required:
|
||||
- testThing
|
||||
`
|
||||
// create a new document from specification bytes
|
||||
doc, err := NewDocument([]byte(spec))
|
||||
@@ -569,6 +574,36 @@ components:
|
||||
// Loop Point: #/components/schemas/Two
|
||||
}
|
||||
|
||||
// This tests checks that circular references which are _not_ marked as required pass correctly
|
||||
func TestNewDocument_terminable_circular_references(t *testing.T) {
|
||||
|
||||
// create a specification with an obvious and deliberate circular reference
|
||||
spec := `openapi: "3.1"
|
||||
components:
|
||||
schemas:
|
||||
One:
|
||||
description: "test one"
|
||||
properties:
|
||||
things:
|
||||
"$ref": "#/components/schemas/Two"
|
||||
Two:
|
||||
description: "test two"
|
||||
properties:
|
||||
testThing:
|
||||
"$ref": "#/components/schemas/One"
|
||||
`
|
||||
// create a new document from specification bytes
|
||||
doc, err := NewDocument([]byte(spec))
|
||||
|
||||
// if anything went wrong, an error is thrown
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("cannot create new document: %e", err))
|
||||
}
|
||||
_, errs := doc.BuildV3Model()
|
||||
|
||||
assert.Len(t, errs, 0)
|
||||
}
|
||||
|
||||
// If you're using complex types with OpenAPI Extensions, it's simple to unpack extensions into complex
|
||||
// types using `high.UnpackExtensions()`. libopenapi retains the original raw data in the low model (not the high)
|
||||
// which means unpacking them can be a little complex.
|
||||
@@ -687,4 +722,3 @@ components:
|
||||
//parameterOne 'x-custom-burgers' (some burgers) has 2 burgers, 'anotherBurger' has mayo sauce and a lamb patty
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -119,7 +119,7 @@ func TestResolver_ResolveComponents_Stripe(t *testing.T) {
|
||||
assert.Len(t, circ, 3)
|
||||
|
||||
assert.Len(t, resolver.GetNonPolymorphicCircularErrors(), 3)
|
||||
assert.Len(t, resolver.GetPolymorphicCircularErrors(), 20)
|
||||
assert.Len(t, resolver.GetPolymorphicCircularErrors(), 0)
|
||||
}
|
||||
|
||||
func TestResolver_ResolveComponents_BurgerShop(t *testing.T) {
|
||||
@@ -225,5 +225,5 @@ func ExampleNewResolver() {
|
||||
//
|
||||
fmt.Printf("There are %d circular reference errors, %d of them are polymorphic errors, %d are not",
|
||||
len(circularErrors), len(resolver.GetPolymorphicCircularErrors()), len(resolver.GetNonPolymorphicCircularErrors()))
|
||||
// Output: There are 23 circular reference errors, 20 of them are polymorphic errors, 3 are not
|
||||
// Output: There are 3 circular reference errors, 0 of them are polymorphic errors, 3 are not
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user