1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
package openapi3
import (
"context"
"os"
"regexp"
"testing"
"github.com/stretchr/testify/require"
)
func TestInternalizeRefs(t *testing.T) {
ctx := context.Background()
regexpRef := regexp.MustCompile(`"\$ref":`)
regexpRefInternal := regexp.MustCompile(`"\$ref":"#`)
tests := []struct {
filename string
}{
{"testdata/testref.openapi.yml"},
{"testdata/recursiveRef/openapi.yml"},
{"testdata/spec.yaml"},
{"testdata/callbacks.yml"},
{"testdata/issue831/testref.internalizepath.openapi.yml"},
}
for _, test := range tests {
t.Run(test.filename, func(t *testing.T) {
// Load in the reference spec from the testdata
sl := NewLoader()
sl.IsExternalRefsAllowed = true
doc, err := sl.LoadFromFile(test.filename)
require.NoError(t, err, "loading test file")
err = doc.Validate(ctx)
require.NoError(t, err, "validating spec")
// Internalize the references
doc.InternalizeRefs(ctx, nil)
// Validate the internalized spec
err = doc.Validate(ctx)
require.NoError(t, err, "validating internalized spec")
actual, err := doc.MarshalJSON()
require.NoError(t, err, "marshaling internalized spec")
// run a static check over the file, making sure each occurrence of a
// reference is followed by a #
numRefs := len(regexpRef.FindAll(actual, -1))
numInternalRefs := len(regexpRefInternal.FindAll(actual, -1))
require.Equal(t, numRefs, numInternalRefs, "checking all references are internal")
// load from actual, but with the path set to the current directory
doc2, err := sl.LoadFromData(actual)
require.NoError(t, err, "reloading spec")
err = doc2.Validate(ctx)
require.NoError(t, err, "validating reloaded spec")
// compare with expected
expected, err := os.ReadFile(test.filename + ".internalized.yml")
require.NoError(t, err)
require.JSONEq(t, string(expected), string(actual))
})
}
}
|