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

@@ -5,17 +5,25 @@ package v3
import (
"fmt"
"log"
"log/slog"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"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/pb33f/libopenapi/orderedmap"
"github.com/pb33f/libopenapi/utils"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
var lowDoc *lowv3.Document
@@ -23,7 +31,7 @@ var lowDoc *lowv3.Document
func initTest() {
data, _ := os.ReadFile("../../../test_specs/burgershop.openapi.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
var err []error
var err error
lowDoc, err = lowv3.CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: true,
AllowRemoteReferences: true,
@@ -221,7 +229,7 @@ func TestNewDocument_Components_Schemas(t *testing.T) {
d := h.Components.Schemas.GetOrZero("Drink")
assert.Len(t, d.Schema().Required, 2)
assert.True(t, d.Schema().AdditionalProperties.(bool))
assert.True(t, d.Schema().AdditionalProperties.B)
assert.Equal(t, "drinkType", d.Schema().Discriminator.PropertyName)
assert.Equal(t, "some value", d.Schema().Discriminator.Mapping["drink"])
assert.Equal(t, 516, d.Schema().Discriminator.GoLow().PropertyName.ValueNode.Line)
@@ -378,15 +386,14 @@ func testBurgerShop(t *testing.T, h *Document, checkLines bool) {
assert.Equal(t, 310, okResp.Links.GetOrZero("LocateBurger").GoLow().OperationId.ValueNode.Line)
assert.Equal(t, 118, burgersOp.Post.Security[0].GoLow().Requirements.ValueNode.Line)
}
}
func TestStripeAsDoc(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/stripe.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
var err []error
lowDoc, err = lowv3.CreateDocumentFromConfig(info, datamodel.NewOpenDocumentConfiguration())
assert.Len(t, err, 3)
var err error
lowDoc, err = lowv3.CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
assert.Len(t, utils.UnwrapErrors(err), 3)
d := NewDocument(lowDoc)
assert.NotNil(t, d)
}
@@ -394,18 +401,18 @@ func TestStripeAsDoc(t *testing.T) {
func TestK8sAsDoc(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/k8s.json")
info, _ := datamodel.ExtractSpecInfo(data)
var err []error
lowSwag, err := lowv2.CreateDocumentFromConfig(info, datamodel.NewOpenDocumentConfiguration())
var err error
lowSwag, err := lowv2.CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
d := v2.NewSwaggerDocument(lowSwag)
assert.Len(t, err, 0)
assert.Len(t, utils.UnwrapErrors(err), 0)
assert.NotNil(t, d)
}
func TestAsanaAsDoc(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/asana.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
var err []error
lowDoc, err = lowv3.CreateDocumentFromConfig(info, datamodel.NewOpenDocumentConfiguration())
var err error
lowDoc, err = lowv3.CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
if err != nil {
panic("broken something")
}
@@ -414,10 +421,53 @@ func TestAsanaAsDoc(t *testing.T) {
assert.Equal(t, 118, orderedmap.Len(d.Paths.PathItems))
}
func TestDigitalOceanAsDocViaCheckout(t *testing.T) {
// this is a full checkout of the digitalocean API repo.
tmp, _ := os.MkdirTemp("", "openapi")
cmd := exec.Command("git", "clone", "https://github.com/digitalocean/openapi", tmp)
defer os.RemoveAll(filepath.Join(tmp, "openapi"))
err := cmd.Run()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
spec, _ := filepath.Abs(filepath.Join(tmp, "specification", "DigitalOcean-public.v2.yaml"))
doLocal, _ := os.ReadFile(spec)
var rootNode yaml.Node
_ = yaml.Unmarshal(doLocal, &rootNode)
basePath := filepath.Join(tmp, "specification")
data, _ := os.ReadFile("../../../test_specs/digitalocean.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
config := datamodel.DocumentConfiguration{
AllowFileReferences: true,
AllowRemoteReferences: true,
BasePath: basePath,
Logger: slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
})),
}
lowDoc, err = lowv3.CreateDocumentFromConfig(info, &config)
if err != nil {
er := utils.UnwrapErrors(err)
for e := range er {
fmt.Println(er[e])
}
}
d := NewDocument(lowDoc)
assert.NotNil(t, d)
assert.Equal(t, 183, d.Paths.PathItems.Len())
}
func TestDigitalOceanAsDocFromSHA(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/digitalocean.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
var err []error
var err error
baseURL, _ := url.Parse("https://raw.githubusercontent.com/digitalocean/openapi/82e1d558e15a59edc1d47d2c5544e7138f5b3cbf/specification")
config := datamodel.DocumentConfiguration{
@@ -426,24 +476,68 @@ func TestDigitalOceanAsDocFromSHA(t *testing.T) {
BaseURL: baseURL,
}
if os.Getenv("GH_PAT") != "" {
client := &http.Client{
Timeout: time.Second * 60,
}
config.RemoteURLHandler = func(url string) (*http.Response, error) {
request, _ := http.NewRequest(http.MethodGet, url, nil)
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", os.Getenv("GH_PAT")))
return client.Do(request)
}
}
lowDoc, err = lowv3.CreateDocumentFromConfig(info, &config)
assert.Len(t, utils.UnwrapErrors(err), 3) // there are 3 404's in this release of the API.
d := NewDocument(lowDoc)
assert.NotNil(t, d)
assert.Equal(t, 183, d.Paths.PathItems.Len())
}
func TestDigitalOceanAsDocFromMain(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/digitalocean.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
var err error
baseURL, _ := url.Parse("https://raw.githubusercontent.com/digitalocean/openapi/main/specification")
config := datamodel.DocumentConfiguration{
AllowFileReferences: true,
AllowRemoteReferences: true,
BaseURL: baseURL,
}
config.Logger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelError,
}))
if os.Getenv("GH_PAT") != "" {
client := &http.Client{
Timeout: time.Second * 60,
}
config.RemoteURLHandler = func(url string) (*http.Response, error) {
request, _ := http.NewRequest(http.MethodGet, url, nil)
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", os.Getenv("GITHUB_TOKEN")))
return client.Do(request)
}
}
lowDoc, err = lowv3.CreateDocumentFromConfig(info, &config)
if err != nil {
for e := range err {
fmt.Println(err[e])
er := utils.UnwrapErrors(err)
for e := range er {
fmt.Printf("Reported Error: %s\n", er[e])
}
panic("broken something")
}
d := NewDocument(lowDoc)
assert.NotNil(t, d)
assert.Equal(t, 183, orderedmap.Len(d.Paths.PathItems))
}
func TestPetstoreAsDoc(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/petstorev3.json")
info, _ := datamodel.ExtractSpecInfo(data)
var err []error
lowDoc, err = lowv3.CreateDocumentFromConfig(info, datamodel.NewOpenDocumentConfiguration())
var err error
lowDoc, err = lowv3.CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
if err != nil {
panic("broken something")
}
@@ -455,16 +549,15 @@ func TestPetstoreAsDoc(t *testing.T) {
func TestCircularReferencesDoc(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/circular-tests.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
var err []error
lowDoc, err = lowv3.CreateDocumentFromConfig(info, datamodel.NewOpenDocumentConfiguration())
assert.Len(t, err, 3)
d := NewDocument(lowDoc)
lDoc, err := lowv3.CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
assert.Len(t, utils.UnwrapErrors(err), 3)
d := NewDocument(lDoc)
assert.Len(t, d.Components.Schemas, 9)
assert.Len(t, d.Index.GetCircularReferences(), 3)
}
func TestDocument_MarshalYAML(t *testing.T) {
// create a new document
initTest()
h := NewDocument(lowDoc)
@@ -473,20 +566,18 @@ func TestDocument_MarshalYAML(t *testing.T) {
r, _ := h.Render()
info, _ := datamodel.ExtractSpecInfo(r)
lDoc, e := lowv3.CreateDocumentFromConfig(info, datamodel.NewOpenDocumentConfiguration())
lDoc, e := lowv3.CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
assert.Nil(t, e)
highDoc := NewDocument(lDoc)
testBurgerShop(t, highDoc, false)
}
func TestDocument_MarshalIndention(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/single-definition.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
lowDoc, _ = lowv3.CreateDocumentFromConfig(info, datamodel.NewOpenDocumentConfiguration())
lowDoc, _ = lowv3.CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
highDoc := NewDocument(lowDoc)
rendered := highDoc.RenderWithIndention(2)
@@ -496,15 +587,13 @@ func TestDocument_MarshalIndention(t *testing.T) {
rendered = highDoc.RenderWithIndention(4)
assert.NotEqual(t, string(data), strings.TrimSpace(string(rendered)))
}
func TestDocument_MarshalIndention_Error(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/single-definition.yaml")
info, _ := datamodel.ExtractSpecInfo(data)
lowDoc, _ = lowv3.CreateDocumentFromConfig(info, datamodel.NewOpenDocumentConfiguration())
lowDoc, _ = lowv3.CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
highDoc := NewDocument(lowDoc)
rendered := highDoc.RenderWithIndention(2)
@@ -514,15 +603,13 @@ func TestDocument_MarshalIndention_Error(t *testing.T) {
rendered = highDoc.RenderWithIndention(4)
assert.NotEqual(t, string(data), strings.TrimSpace(string(rendered)))
}
func TestDocument_MarshalJSON(t *testing.T) {
data, _ := os.ReadFile("../../../test_specs/petstorev3.json")
info, _ := datamodel.ExtractSpecInfo(data)
lowDoc, _ = lowv3.CreateDocumentFromConfig(info, datamodel.NewOpenDocumentConfiguration())
lowDoc, _ = lowv3.CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
highDoc := NewDocument(lowDoc)
@@ -530,7 +617,7 @@ func TestDocument_MarshalJSON(t *testing.T) {
// now read back in the JSON
info, _ = datamodel.ExtractSpecInfo(rendered)
lowDoc, _ = lowv3.CreateDocumentFromConfig(info, datamodel.NewOpenDocumentConfiguration())
lowDoc, _ = lowv3.CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
newDoc := NewDocument(lowDoc)
assert.Equal(t, orderedmap.Len(newDoc.Paths.PathItems), orderedmap.Len(highDoc.Paths.PathItems))
@@ -538,7 +625,6 @@ func TestDocument_MarshalJSON(t *testing.T) {
}
func TestDocument_MarshalYAMLInline(t *testing.T) {
// create a new document
initTest()
h := NewDocument(lowDoc)
@@ -547,16 +633,14 @@ func TestDocument_MarshalYAMLInline(t *testing.T) {
r, _ := h.RenderInline()
info, _ := datamodel.ExtractSpecInfo(r)
lDoc, e := lowv3.CreateDocumentFromConfig(info, datamodel.NewOpenDocumentConfiguration())
lDoc, e := lowv3.CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
assert.Nil(t, e)
highDoc := NewDocument(lDoc)
testBurgerShop(t, highDoc, false)
}
func TestDocument_MarshalYAML_TestRefs(t *testing.T) {
// create a new document
yml := `openapi: 3.1.0
paths:
@@ -617,7 +701,7 @@ components:
numPatties: 1`
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err []error
var err error
lowDoc, err = lowv3.CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: true,
AllowRemoteReferences: true,
@@ -634,7 +718,6 @@ components:
}
func TestDocument_MarshalYAML_TestParamRefs(t *testing.T) {
// create a new document
yml := `openapi: 3.1.0
paths:
@@ -671,7 +754,7 @@ components:
required: true`
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err []error
var err error
lowDoc, err = lowv3.CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: true,
AllowRemoteReferences: true,
@@ -687,7 +770,6 @@ components:
}
func TestDocument_MarshalYAML_TestModifySchemas(t *testing.T) {
// create a new document
yml := `openapi: 3.1.0
components:
@@ -700,7 +782,7 @@ components:
`
info, _ := datamodel.ExtractSpecInfo([]byte(yml))
var err []error
var err error
lowDoc, err = lowv3.CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{
AllowFileReferences: true,
AllowRemoteReferences: true,