From c0385f2664a6de6dab04bb4f59d45193f7d5662a Mon Sep 17 00:00:00 2001 From: Emilien Puget Date: Mon, 26 Feb 2024 20:55:54 +0100 Subject: [PATCH] abort BundleBytes only when there is no document returned --- bundler/bundler.go | 8 ++++++-- bundler/bundler_test.go | 42 ++++++++++++++--------------------------- 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/bundler/bundler.go b/bundler/bundler.go index 2c740f3..75b9c61 100644 --- a/bundler/bundler.go +++ b/bundler/bundler.go @@ -14,6 +14,9 @@ import ( "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. // This is useful for when you want to take a specification with external references, and you want to bundle it // into a single document. @@ -29,8 +32,9 @@ func BundleBytes(bytes []byte, configuration *datamodel.DocumentConfiguration) ( } v3Doc, errs := doc.BuildV3Model() - if len(errs) > 0 { - return nil, errors.Join(errs...) + err = errors.Join(errs...) + if v3Doc == nil { + return nil, errors.Join(ErrInvalidModel, err) } bundledBytes, e := bundle(&v3Doc.Model, configuration.BundleInlineRefs) diff --git a/bundler/bundler_test.go b/bundler/bundler_test.go index 98b119d..7ebbdf5 100644 --- a/bundler/bundler_test.go +++ b/bundler/bundler_test.go @@ -17,6 +17,7 @@ import ( "github.com/pb33f/libopenapi" "github.com/pb33f/libopenapi/datamodel" + "github.com/pb33f/libopenapi/utils" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -113,17 +114,9 @@ func TestBundleBytes(t *testing.T) { })), } - _, e := BundleBytes(digi, config) - require.Error(t, e) - i, ok := e.(interface{ Unwrap() []error }) - 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()) + bytes, e := BundleBytes(digi, config) + assert.Error(t, e) + assert.Len(t, bytes, 2016) logEntries := strings.Split(byteBuf.String(), "\n") if len(logEntries) == 1 && logEntries[0] == "" { @@ -152,14 +145,13 @@ components: _, e := BundleBytes(digi, config) require.Error(t, e) - i, ok := e.(interface{ Unwrap() []error }) - if !ok { - t.Fatal("expect join error") - } - unwrap := i.Unwrap() + unwrap := utils.UnwrapErrors(e) require.Len(t, unwrap, 2) - assert.Equal(t, "component 'bork' does not exist in the specification", unwrap[0].Error()) - assert.Equal(t, "cannot resolve reference `bork`, it's missing: $bork [5:7]", unwrap[1].Error()) + assert.ErrorIs(t, unwrap[0], ErrInvalidModel) + 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") if len(logEntries) == 1 && logEntries[0] == "" { @@ -249,18 +241,12 @@ components: })), } - _, e := BundleBytes(digi, config) - require.Error(t, e) - i, ok := e.(interface{ Unwrap() []error }) - 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()) + bytes, e := BundleBytes(digi, config) + assert.Error(t, e) + assert.Len(t, bytes, 458) logEntries := strings.Split(byteBuf.String(), "\n") - assert.Len(t, logEntries, 12) + assert.Len(t, logEntries, 13) } func TestBundleBytes_Bad(t *testing.T) {