Skip to content

Commit

Permalink
Fix RenderAndReload error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
maboehm committed May 7, 2024
1 parent 8a1d3ae commit c2e0334
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
11 changes: 5 additions & 6 deletions document.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,21 +198,20 @@ func (d *document) Serialize() ([]byte, error) {
}

func (d *document) RenderAndReload() ([]byte, Document, *DocumentModel[v3high.Document], []error) {

newBytes, rerr := d.Render()
if rerr != nil {
return nil, nil, nil, []error{rerr}
}

var errs []error

newDoc, err := NewDocumentWithConfiguration(newBytes, d.config)
errs = append(errs, err)
if err != nil {
return nil, nil, nil, []error{err}
}

// build the model.
m, buildErrs := newDoc.BuildV3Model()
if buildErrs != nil {
return newBytes, newDoc, m, errs
if len(buildErrs) > 0 {
return newBytes, newDoc, m, buildErrs
}
// this document is now dead, long live the new document!
return newBytes, newDoc, m, nil
Expand Down
23 changes: 23 additions & 0 deletions document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,29 @@ func TestDocument_RenderAndReload(t *testing.T) {
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) {
// load an OpenAPI 3 specification from bytes
petstore, _ := os.ReadFile("test_specs/petstorev3.json")
Expand Down

0 comments on commit c2e0334

Please sign in to comment.