addressed issue #264

Error was not being returned when rendering JSON
This commit is contained in:
quobix
2024-04-18 12:33:47 -04:00
parent fc6f12e6ca
commit 1cc814411d
3 changed files with 27 additions and 10 deletions

View File

@@ -171,11 +171,14 @@ func (d *Document) RenderWithIndention(indent int) []byte {
}
// RenderJSON will return a JSON representation of the Document object as a byte slice.
func (d *Document) RenderJSON(indention string) []byte {
func (d *Document) RenderJSON(indention string) ([]byte, error) {
nb := high.NewNodeBuilder(d, d.low)
dat, _ := json.YAMLNodeToJSON(nb.Render(), indention)
return dat
dat, err := json.YAMLNodeToJSON(nb.Render(), indention)
if err != nil {
return dat, err
}
return dat, nil
}
func (d *Document) RenderInline() ([]byte, error) {

View File

@@ -208,7 +208,9 @@ func (d *document) RenderAndReload() ([]byte, Document, *DocumentModel[v3high.Do
newDoc, err := NewDocumentWithConfiguration(newBytes, d.config)
errs = append(errs, err)
if newDoc == nil {
return newBytes, nil, nil, errs
}
// build the model.
m, buildErrs := newDoc.BuildV3Model()
if buildErrs != nil {
@@ -224,7 +226,7 @@ func (d *document) Render() ([]byte, error) {
}
var newBytes []byte
var jsonErr error
if d.info.SpecFileType == datamodel.JSONFileType {
jsonIndent := " "
i := d.info.OriginalIndentation
@@ -233,13 +235,12 @@ func (d *document) Render() ([]byte, error) {
jsonIndent += " "
}
}
newBytes = d.highOpenAPI3Model.Model.RenderJSON(jsonIndent)
newBytes, jsonErr = d.highOpenAPI3Model.Model.RenderJSON(jsonIndent)
}
if d.info.SpecFileType == datamodel.YAMLFileType {
newBytes = d.highOpenAPI3Model.Model.RenderWithIndention(d.info.OriginalIndentation)
}
return newBytes, nil
return newBytes, jsonErr
}
func (d *document) BuildV2Model() (*DocumentModel[v2high.Swagger], []error) {

View File

@@ -158,7 +158,7 @@ func TestDocument_RoundTrip_JSON(t *testing.T) {
m, errs := doc.BuildV3Model()
require.Empty(t, errs)
out := m.Model.RenderJSON(" ")
out, _ := m.Model.RenderJSON(" ")
// windows has to be different, it does not add carriage returns.
if runtime.GOOS != "windows" {
@@ -192,7 +192,7 @@ func TestDocument_RoundTrip_YAML_To_JSON(t *testing.T) {
m, errs := doc.BuildV3Model()
require.Empty(t, errs)
out := m.Model.RenderJSON(" ")
out, _ := m.Model.RenderJSON(" ")
require.NoError(t, err)
if runtime.GOOS != "windows" {
assert.Equal(t, string(j), string(out))
@@ -1324,3 +1324,16 @@ func TestDocument_TestNestedFiles(t *testing.T) {
_, errs := doc.BuildV3Model()
require.Empty(t, errs)
}
func TestDocument_Issue264(t *testing.T) {
openAPISpec := `{"openapi":"3.0.0","info":{"title":"dummy","version":"1.0.0"},"paths":{"/dummy":{"post":{"requestBody":{"content":{"application/json":{"schema":{"type":"object","properties":{"value":{"type":"number","format":"decimal","multipleOf":0.01,"minimum":-999.99}}}}}},"responses":{"200":{"description":"OK"}}}}}}`
d, _ := NewDocument([]byte(openAPISpec))
_, _ = d.BuildV3Model()
_, _, _, errs := d.RenderAndReload() // code panics here
assert.Len(t, errs, 1)
assert.Equal(t, "yaml: cannot decode !!float `-999.99` as a !!int", errs[0].Error())
}