mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-10 04:20:24 +00:00
abort BundleBytes only when there is no document returned
This commit is contained in:
@@ -14,6 +14,9 @@ import (
|
|||||||
"github.com/pb33f/libopenapi/index"
|
"github.com/pb33f/libopenapi/index"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ErrInvalidModel is returned when the model is not usable.
|
||||||
|
var ErrInvalidModel = errors.New("invalid model")
|
||||||
|
|
||||||
// BundleBytes will take a byte slice of an OpenAPI specification and return a bundled version of it.
|
// BundleBytes will take a byte slice of an OpenAPI specification and return a bundled version of it.
|
||||||
// This is useful for when you want to take a specification with external references, and you want to bundle it
|
// This is useful for when you want to take a specification with external references, and you want to bundle it
|
||||||
// into a single document.
|
// into a single document.
|
||||||
@@ -29,8 +32,9 @@ func BundleBytes(bytes []byte, configuration *datamodel.DocumentConfiguration) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
v3Doc, errs := doc.BuildV3Model()
|
v3Doc, errs := doc.BuildV3Model()
|
||||||
if len(errs) > 0 {
|
err = errors.Join(errs...)
|
||||||
return nil, errors.Join(errs...)
|
if v3Doc == nil {
|
||||||
|
return nil, errors.Join(ErrInvalidModel, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
bundledBytes, e := bundle(&v3Doc.Model, configuration.BundleInlineRefs)
|
bundledBytes, e := bundle(&v3Doc.Model, configuration.BundleInlineRefs)
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import (
|
|||||||
|
|
||||||
"github.com/pb33f/libopenapi"
|
"github.com/pb33f/libopenapi"
|
||||||
"github.com/pb33f/libopenapi/datamodel"
|
"github.com/pb33f/libopenapi/datamodel"
|
||||||
|
"github.com/pb33f/libopenapi/utils"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
@@ -113,17 +114,9 @@ func TestBundleBytes(t *testing.T) {
|
|||||||
})),
|
})),
|
||||||
}
|
}
|
||||||
|
|
||||||
_, e := BundleBytes(digi, config)
|
bytes, e := BundleBytes(digi, config)
|
||||||
require.Error(t, e)
|
assert.Error(t, e)
|
||||||
i, ok := e.(interface{ Unwrap() []error })
|
assert.Len(t, bytes, 2016)
|
||||||
if !ok {
|
|
||||||
t.Fatal("expect join error")
|
|
||||||
}
|
|
||||||
unwrap := i.Unwrap()
|
|
||||||
require.Len(t, unwrap, 3)
|
|
||||||
assert.Equal(t, "infinite circular reference detected: Two: Two -> One -> Two [15:9]", unwrap[0].Error())
|
|
||||||
assert.Equal(t, "infinite circular reference detected: Seven: Seven -> Three -> Seven [32:9]", unwrap[1].Error())
|
|
||||||
assert.Equal(t, "infinite circular reference detected: Ten: Ten -> Ten [71:9]", unwrap[2].Error())
|
|
||||||
|
|
||||||
logEntries := strings.Split(byteBuf.String(), "\n")
|
logEntries := strings.Split(byteBuf.String(), "\n")
|
||||||
if len(logEntries) == 1 && logEntries[0] == "" {
|
if len(logEntries) == 1 && logEntries[0] == "" {
|
||||||
@@ -152,14 +145,13 @@ components:
|
|||||||
|
|
||||||
_, e := BundleBytes(digi, config)
|
_, e := BundleBytes(digi, config)
|
||||||
require.Error(t, e)
|
require.Error(t, e)
|
||||||
i, ok := e.(interface{ Unwrap() []error })
|
unwrap := utils.UnwrapErrors(e)
|
||||||
if !ok {
|
|
||||||
t.Fatal("expect join error")
|
|
||||||
}
|
|
||||||
unwrap := i.Unwrap()
|
|
||||||
require.Len(t, unwrap, 2)
|
require.Len(t, unwrap, 2)
|
||||||
assert.Equal(t, "component 'bork' does not exist in the specification", unwrap[0].Error())
|
assert.ErrorIs(t, unwrap[0], ErrInvalidModel)
|
||||||
assert.Equal(t, "cannot resolve reference `bork`, it's missing: $bork [5:7]", unwrap[1].Error())
|
unwrapNext := utils.UnwrapErrors(unwrap[1])
|
||||||
|
require.Len(t, unwrapNext, 2)
|
||||||
|
assert.Equal(t, "component 'bork' does not exist in the specification", unwrapNext[0].Error())
|
||||||
|
assert.Equal(t, "cannot resolve reference `bork`, it's missing: $bork [5:7]", unwrapNext[1].Error())
|
||||||
|
|
||||||
logEntries := strings.Split(byteBuf.String(), "\n")
|
logEntries := strings.Split(byteBuf.String(), "\n")
|
||||||
if len(logEntries) == 1 && logEntries[0] == "" {
|
if len(logEntries) == 1 && logEntries[0] == "" {
|
||||||
@@ -249,18 +241,12 @@ components:
|
|||||||
})),
|
})),
|
||||||
}
|
}
|
||||||
|
|
||||||
_, e := BundleBytes(digi, config)
|
bytes, e := BundleBytes(digi, config)
|
||||||
require.Error(t, e)
|
assert.Error(t, e)
|
||||||
i, ok := e.(interface{ Unwrap() []error })
|
assert.Len(t, bytes, 458)
|
||||||
if !ok {
|
|
||||||
t.Fatal("expect join error")
|
|
||||||
}
|
|
||||||
unwrap := i.Unwrap()
|
|
||||||
require.Len(t, unwrap, 1)
|
|
||||||
assert.Equal(t, "infinite circular reference detected: One: One -> Two -> One [19:9]", unwrap[0].Error())
|
|
||||||
|
|
||||||
logEntries := strings.Split(byteBuf.String(), "\n")
|
logEntries := strings.Split(byteBuf.String(), "\n")
|
||||||
assert.Len(t, logEntries, 12)
|
assert.Len(t, logEntries, 13)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBundleBytes_Bad(t *testing.T) {
|
func TestBundleBytes_Bad(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user