mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 04:20:11 +00:00
Fix RenderAndReload error handling
This commit is contained in:
11
document.go
11
document.go
@@ -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
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user