Fix RenderAndReload error handling

This commit is contained in:
Marcel Boehm
2024-05-07 16:00:13 +02:00
committed by quobix
parent 0e7459847b
commit 3d92fc0b1a
2 changed files with 28 additions and 6 deletions

View File

@@ -198,21 +198,20 @@ func (d *document) Serialize() ([]byte, error) {
} }
func (d *document) RenderAndReload() ([]byte, Document, *DocumentModel[v3high.Document], []error) { func (d *document) RenderAndReload() ([]byte, Document, *DocumentModel[v3high.Document], []error) {
newBytes, rerr := d.Render() newBytes, rerr := d.Render()
if rerr != nil { if rerr != nil {
return nil, nil, nil, []error{rerr} return nil, nil, nil, []error{rerr}
} }
var errs []error
newDoc, err := NewDocumentWithConfiguration(newBytes, d.config) newDoc, err := NewDocumentWithConfiguration(newBytes, d.config)
errs = append(errs, err) if err != nil {
return nil, nil, nil, []error{err}
}
// build the model. // build the model.
m, buildErrs := newDoc.BuildV3Model() m, buildErrs := newDoc.BuildV3Model()
if buildErrs != nil { if len(buildErrs) > 0 {
return newBytes, newDoc, m, errs return newBytes, newDoc, m, buildErrs
} }
// this document is now dead, long live the new document! // this document is now dead, long live the new document!
return newBytes, newDoc, m, nil return newBytes, newDoc, m, nil

View File

@@ -345,6 +345,29 @@ func TestDocument_RenderAndReload(t *testing.T) {
h.Components.SecuritySchemes.GetOrZero("petstore_auth").Flows.Implicit.AuthorizationUrl) h.Components.SecuritySchemes.GetOrZero("petstore_auth").Flows.Implicit.AuthorizationUrl)
} }
func TestDocument_RenderAndReload_WithErrors(t *testing.T) {
// load an OpenAPI 3 specification from bytes
petstore, _ := os.ReadFile("test_specs/petstorev3.json")
// create a new document from specification bytes
doc, err := NewDocument(petstore)
// if anything went wrong, an error is thrown
if err != nil {
panic(fmt.Sprintf("cannot create new document: %e", err))
}
// because we know this is a v3 spec, we can build a ready to go model from it.
m, _ := doc.BuildV3Model()
// Remove a schema to make the model invalid
_, present := m.Model.Components.Schemas.Delete("Pet")
assert.True(t, present, "expected schema Pet to exist")
_, _, _, errors := doc.RenderAndReload()
assert.Len(t, errors, 2)
assert.Equal(t, errors[0].Error(), "component '#/components/schemas/Pet' does not exist in the specification")
}
func TestDocument_Render(t *testing.T) { func TestDocument_Render(t *testing.T) {
// load an OpenAPI 3 specification from bytes // load an OpenAPI 3 specification from bytes
petstore, _ := os.ReadFile("test_specs/petstorev3.json") petstore, _ := os.ReadFile("test_specs/petstorev3.json")