diff --git a/datamodel/high/v3/document.go b/datamodel/high/v3/document.go index 90c83d9..1a04950 100644 --- a/datamodel/high/v3/document.go +++ b/datamodel/high/v3/document.go @@ -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) { diff --git a/document.go b/document.go index 8e37881..f27618b 100644 --- a/document.go +++ b/document.go @@ -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) { diff --git a/document_test.go b/document_test.go index 0be705b..b8c3e4f 100644 --- a/document_test.go +++ b/document_test.go @@ -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()) +}