Merge branch 'main' into Baliedge/PIP-2552-consistent-ordering

This commit is contained in:
Tristan Cartledge
2023-11-27 12:06:39 +00:00
219 changed files with 20934 additions and 4697 deletions

View File

@@ -2,11 +2,15 @@ package v3
import (
"fmt"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/utils"
"log/slog"
"net/http"
"net/url"
"os"
"testing"
"github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/libopenapi/datamodel/low/base"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/assert"
)
@@ -19,9 +23,9 @@ func initTest() {
}
data, _ := os.ReadFile("../../../test_specs/burgershop.openapi.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
var err []error
var err error
// deprecated function test.
doc, err = CreateDocument(info)
doc, err = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
if err != nil {
panic("broken something")
}
@@ -31,10 +35,7 @@ func BenchmarkCreateDocument(b *testing.B) {
data, _ := os.ReadFile("../../../test_specs/burgershop.openapi.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
for i := 0; i < b.N; i++ {
doc, _ = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
doc, _ = CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
}
}
@@ -42,47 +43,145 @@ func BenchmarkCreateDocument_Circular(b *testing.B) {
data, _ := os.ReadFile("../../../test_specs/circular-tests.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
for i := 0; i < b.N; i++ {
_, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
if err != nil {
panic("this should not error")
}
}
}
func BenchmarkCreateDocument_k8s(b *testing.B) {
data, _ := os.ReadFile("../../../test_specs/k8s.json")
info, _ := datamodel.ExtractSpecInfo(data)
for i := 0; i < b.N; i++ {
_, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
if err != nil {
panic("this should not error")
_, err := CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
if err == nil {
panic("this should error, it has circular references")
}
}
}
func TestCircularReferenceError(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/circular-tests.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
circDoc, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
circDoc, err := CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
assert.NotNil(t, circDoc)
assert.Len(t, err, 3)
assert.Error(t, err)
assert.Len(t, utils.UnwrapErrors(err), 3)
}
func TestRolodexLocalFileSystem(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/first.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
cf := datamodel.NewDocumentConfiguration()
cf.BasePath = "../../../test_specs"
cf.FileFilter = []string{"first.yaml", "second.yaml", "third.yaml"}
lDoc, err := CreateDocumentFromConfig(info, cf)
assert.NotNil(t, lDoc)
assert.NoError(t, err)
}
func TestRolodexLocalFileSystem_ProvideNonRolodexFS(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/first.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
baseDir := "../../../test_specs"
cf := datamodel.NewDocumentConfiguration()
cf.BasePath = baseDir
cf.FileFilter = []string{"first.yaml", "second.yaml", "third.yaml"}
cf.LocalFS = os.DirFS(baseDir)
lDoc, err := CreateDocumentFromConfig(info, cf)
assert.NotNil(t, lDoc)
assert.Error(t, err)
}
func TestRolodexLocalFileSystem_ProvideRolodexFS(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/first.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
baseDir := "../../../test_specs"
cf := datamodel.NewDocumentConfiguration()
cf.BasePath = baseDir
cf.FileFilter = []string{"first.yaml", "second.yaml", "third.yaml"}
localFS, lErr := index.NewLocalFSWithConfig(&index.LocalFSConfig{
BaseDirectory: baseDir,
DirFS: os.DirFS(baseDir),
FileFilters: cf.FileFilter,
})
cf.LocalFS = localFS
assert.NoError(t, lErr)
lDoc, err := CreateDocumentFromConfig(info, cf)
assert.NotNil(t, lDoc)
assert.NoError(t, err)
}
func TestRolodexLocalFileSystem_BadPath(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/first.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
cf := datamodel.NewDocumentConfiguration()
cf.BasePath = "/NOWHERE"
cf.FileFilter = []string{"first.yaml", "second.yaml", "third.yaml"}
lDoc, err := CreateDocumentFromConfig(info, cf)
assert.NotNil(t, lDoc)
assert.Error(t, err)
}
func TestRolodexRemoteFileSystem(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/first.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
cf := datamodel.NewDocumentConfiguration()
cf.Logger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))
baseUrl := "https://raw.githubusercontent.com/pb33f/libopenapi/main/test_specs"
u, _ := url.Parse(baseUrl)
cf.BaseURL = u
lDoc, err := CreateDocumentFromConfig(info, cf)
assert.NotNil(t, lDoc)
assert.NoError(t, err)
}
func TestRolodexRemoteFileSystem_BadBase(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/first.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
cf := datamodel.NewDocumentConfiguration()
baseUrl := "https://no-no-this-will-not-work-it-just-will-not-get-the-job-done-mate.com"
u, _ := url.Parse(baseUrl)
cf.BaseURL = u
lDoc, err := CreateDocumentFromConfig(info, cf)
assert.NotNil(t, lDoc)
assert.Error(t, err)
}
func TestRolodexRemoteFileSystem_CustomRemote_NoBaseURL(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/first.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
cf := datamodel.NewDocumentConfiguration()
cf.RemoteFS, _ = index.NewRemoteFSWithConfig(&index.SpecIndexConfig{})
lDoc, err := CreateDocumentFromConfig(info, cf)
assert.NotNil(t, lDoc)
assert.Error(t, err)
}
func TestRolodexRemoteFileSystem_CustomHttpHandler(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/first.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
cf := datamodel.NewDocumentConfiguration()
cf.RemoteURLHandler = http.Get
baseUrl := "https://no-no-this-will-not-work-it-just-will-not-get-the-job-done-mate.com"
u, _ := url.Parse(baseUrl)
cf.BaseURL = u
pizza := func(url string) (resp *http.Response, err error) {
return nil, nil
}
cf.RemoteURLHandler = pizza
lDoc, err := CreateDocumentFromConfig(info, cf)
assert.NotNil(t, lDoc)
assert.Error(t, err)
}
func TestCircularReference_IgnoreArray(t *testing.T) {
spec := `openapi: 3.1.0
components:
schemas:
@@ -102,16 +201,13 @@ components:
info, _ := datamodel.ExtractSpecInfo([]byte(spec))
circDoc, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
IgnoreArrayCircularReferences: true,
})
assert.NotNil(t, circDoc)
assert.Len(t, err, 0)
assert.Len(t, utils.UnwrapErrors(err), 0)
}
func TestCircularReference_IgnorePoly(t *testing.T) {
spec := `openapi: 3.1.0
components:
schemas:
@@ -131,12 +227,10 @@ components:
info, _ := datamodel.ExtractSpecInfo([]byte(spec))
circDoc, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
IgnorePolymorphicCircularReferences: true,
})
assert.NotNil(t, circDoc)
assert.Len(t, err, 0)
assert.Len(t, utils.UnwrapErrors(err), 0)
}
func BenchmarkCreateDocument_Stripe(b *testing.B) {
@@ -144,10 +238,7 @@ func BenchmarkCreateDocument_Stripe(b *testing.B) {
info, _ := datamodel.ExtractSpecInfo(data)
for i := 0; i < b.N; i++ {
_, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
_, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
if err != nil {
panic("this should not error")
}
@@ -158,10 +249,7 @@ func BenchmarkCreateDocument_Petstore(b *testing.B) {
data, _ := os.ReadFile("../../../test_specs/petstorev3.json")
info, _ := datamodel.ExtractSpecInfo(data)
for i := 0; i < b.N; i++ {
_, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
_, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
if err != nil {
panic("this should not error")
}
@@ -169,15 +257,10 @@ func BenchmarkCreateDocument_Petstore(b *testing.B) {
}
func TestCreateDocumentStripe(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/stripe.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
d, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
BasePath: "/here",
})
assert.Len(t, err, 3)
d, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
assert.Len(t, utils.UnwrapErrors(err), 3)
assert.Equal(t, "3.0.0", d.Version.Value)
assert.Equal(t, "Stripe API", d.Info.Value.Title.Value)
@@ -234,16 +317,14 @@ func TestCreateDocument_WebHooks(t *testing.T) {
}
func TestCreateDocument_WebHooks_Error(t *testing.T) {
yml := `webhooks:
yml := `openapi: 3.0
webhooks:
$ref: #bork`
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err []error
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
assert.Len(t, err, 1)
var err error
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
assert.Len(t, utils.UnwrapErrors(err), 1)
}
func TestCreateDocument_Servers(t *testing.T) {
@@ -304,7 +385,6 @@ func TestCreateDocument_Tags(t *testing.T) {
// this is why we will need a higher level API to this model, this looks cool and all, but dude.
assert.Equal(t, "now?", extension.Value.(map[string]interface{})["ok"].([]interface{})[0].(map[string]interface{})["what"])
}
}
/// tag2
@@ -314,7 +394,6 @@ func TestCreateDocument_Tags(t *testing.T) {
assert.Equal(t, "https://pb33f.io", doc.Tags.Value[1].Value.ExternalDocs.Value.URL.Value)
assert.NotEmpty(t, doc.Tags.Value[1].Value.ExternalDocs.Value.URL.Value)
assert.Len(t, doc.Tags.Value[1].Value.Extensions, 0)
}
func TestCreateDocument_Paths(t *testing.T) {
@@ -439,7 +518,6 @@ func TestCreateDocument_Paths(t *testing.T) {
assert.NotNil(t, servers)
assert.Len(t, servers, 1)
assert.Equal(t, "https://pb33f.io", servers[0].Value.URL.Value)
}
func TestCreateDocument_Components_Schemas(t *testing.T) {
@@ -465,7 +543,6 @@ func TestCreateDocument_Components_Schemas(t *testing.T) {
p := fries.Value.Schema().FindProperty("favoriteDrink")
assert.Equal(t, "a frosty cold beverage can be coke or sprite",
p.Value.Schema().Description.Value)
}
func TestCreateDocument_Components_SecuritySchemes(t *testing.T) {
@@ -494,7 +571,6 @@ func TestCreateDocument_Components_SecuritySchemes(t *testing.T) {
readScope = oAuth.Flows.Value.AuthorizationCode.Value.FindScope("write:burgers")
assert.NotNil(t, readScope)
assert.Equal(t, "modify burgers and stuff", readScope.Value)
}
func TestCreateDocument_Components_Responses(t *testing.T) {
@@ -507,7 +583,6 @@ func TestCreateDocument_Components_Responses(t *testing.T) {
assert.NotNil(t, dressingResponse.Value)
assert.Equal(t, "all the dressings for a burger.", dressingResponse.Value.Description.Value)
assert.Len(t, dressingResponse.Value.Content.Value, 1)
}
func TestCreateDocument_Components_Examples(t *testing.T) {
@@ -594,7 +669,6 @@ func TestCreateDocument_Component_Discriminator(t *testing.T) {
assert.Nil(t, dsc.FindMappingValue("don't exist"))
assert.NotNil(t, doc.GetExternalDocs())
assert.Nil(t, doc.FindSecurityRequirement("scooby doo"))
}
func TestCreateDocument_CheckAdditionalProperties_Schema(t *testing.T) {
@@ -602,11 +676,8 @@ func TestCreateDocument_CheckAdditionalProperties_Schema(t *testing.T) {
components := doc.Components.Value
d := components.FindSchema("Dressing")
assert.NotNil(t, d.Value.Schema().AdditionalProperties.Value)
if n, ok := d.Value.Schema().AdditionalProperties.Value.(*base.SchemaProxy); ok {
assert.Equal(t, "something in here.", n.Schema().Description.Value)
} else {
assert.Fail(t, "should be a schema")
}
assert.True(t, d.Value.Schema().AdditionalProperties.Value.IsA(), "should be a schema")
}
func TestCreateDocument_CheckAdditionalProperties_Bool(t *testing.T) {
@@ -614,7 +685,7 @@ func TestCreateDocument_CheckAdditionalProperties_Bool(t *testing.T) {
components := doc.Components.Value
d := components.FindSchema("Drink")
assert.NotNil(t, d.Value.Schema().AdditionalProperties.Value)
assert.True(t, d.Value.Schema().AdditionalProperties.Value.(bool))
assert.True(t, d.Value.Schema().AdditionalProperties.Value.B)
}
func TestCreateDocument_Components_Error(t *testing.T) {
@@ -627,12 +698,9 @@ components:
$ref: #bork`
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err []error
doc, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
assert.Len(t, err, 0)
var err error
doc, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
assert.NoError(t, err)
ob := doc.Components.Value.FindSchema("bork").Value
ob.Schema()
@@ -646,12 +714,10 @@ webhooks:
$ref: #bork`
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err []error
doc, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
assert.Len(t, err, 1)
var err error
doc, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
assert.Equal(t, "flat map build failed: reference cannot be found: reference at line 4, column 5 is empty, it cannot be resolved",
err.Error())
}
func TestCreateDocument_Components_Error_Extract(t *testing.T) {
@@ -662,13 +728,9 @@ components:
$ref: #bork`
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err []error
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
assert.Len(t, err, 1)
var err error
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
assert.Equal(t, "reference at line 5, column 7 is empty, it cannot be resolved", err.Error())
}
func TestCreateDocument_Paths_Errors(t *testing.T) {
@@ -678,12 +740,10 @@ paths:
$ref: #bork`
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err []error
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
assert.Len(t, err, 1)
var err error
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
assert.Equal(t,
"path item build failed: cannot find reference: at line 4, col 10", err.Error())
}
func TestCreateDocument_Tags_Errors(t *testing.T) {
@@ -692,12 +752,10 @@ tags:
- $ref: #bork`
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err []error
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
assert.Len(t, err, 1)
var err error
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
assert.Equal(t,
"object extraction failed: reference at line 3, column 5 is empty, it cannot be resolved", err.Error())
}
func TestCreateDocument_Security_Error(t *testing.T) {
@@ -706,12 +764,11 @@ security:
$ref: #bork`
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err []error
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
assert.Len(t, err, 1)
var err error
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
assert.Equal(t,
"array build failed: reference cannot be found: reference at line 3, column 3 is empty, it cannot be resolved",
err.Error())
}
func TestCreateDocument_ExternalDoc_Error(t *testing.T) {
@@ -720,12 +777,9 @@ externalDocs:
$ref: #bork`
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err []error
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
assert.Len(t, err, 1)
var err error
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
assert.Equal(t, "object extraction failed: reference at line 3, column 3 is empty, it cannot be resolved", err.Error())
}
func TestCreateDocument_YamlAnchor(t *testing.T) {
@@ -736,16 +790,10 @@ func TestCreateDocument_YamlAnchor(t *testing.T) {
info, _ := datamodel.ExtractSpecInfo(anchorDocument)
// build low-level document model
document, errors := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
document, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
// if something went wrong, a slice of errors is returned
if len(errors) > 0 {
for i := range errors {
fmt.Printf("error: %s\n", errors[i].Error())
}
if err != nil {
fmt.Printf("error: %s\n", err.Error())
panic("cannot build document")
}
@@ -777,8 +825,19 @@ func TestCreateDocument_YamlAnchor(t *testing.T) {
assert.NotNil(t, jsonGet)
// Should this work? It doesn't
//postJsonType := examplePath.GetValue().Post.GetValue().RequestBody.GetValue().FindContent("application/json")
//assert.NotNil(t, postJsonType)
// update from quobix 10/14/2023: It does now!
postJsonType := examplePath.GetValue().Post.GetValue().RequestBody.GetValue().FindContent("application/json")
assert.NotNil(t, postJsonType)
}
func TestCreateDocument_NotOpenAPI_EnforcedDocCheck(t *testing.T) {
yml := `notadoc: no`
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err error
_, err = CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
assert.Equal(t,
"no openapi version/tag found, cannot create document", err.Error())
}
func ExampleCreateDocument() {
@@ -791,16 +850,10 @@ func ExampleCreateDocument() {
info, _ := datamodel.ExtractSpecInfo(petstoreBytes)
// build low-level document model
document, errors := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: false,
AllowRemoteReferences: false,
})
document, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
// if something went wrong, a slice of errors is returned
if len(errors) > 0 {
for i := range errors {
fmt.Printf("error: %s\n", errors[i].Error())
}
if err != nil {
fmt.Printf("error: %s\n", err.Error())
panic("cannot build document")
}