Skip to content

Commit 1ae8227

Browse files
committed
chore: exit early when failed to parse json spec
1 parent 0af9c35 commit 1ae8227

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

datamodel/spec_info.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77
"encoding/json"
88
"errors"
99
"fmt"
10-
"github.com/pb33f/libopenapi/utils"
11-
"gopkg.in/yaml.v3"
1210
"strings"
1311
"time"
12+
13+
"github.com/pb33f/libopenapi/utils"
14+
"gopkg.in/yaml.v3"
1415
)
1516

1617
const (
@@ -103,18 +104,21 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
103104
_, openAPI2 := utils.FindKeyNode(utils.OpenApi2, parsedSpec.Content)
104105
_, asyncAPI := utils.FindKeyNode(utils.AsyncApi, parsedSpec.Content)
105106

106-
parseJSON := func(bytes []byte, spec *SpecInfo, parsedNode *yaml.Node) {
107+
parseJSON := func(bytes []byte, spec *SpecInfo, parsedNode *yaml.Node) error {
107108
var jsonSpec map[string]interface{}
108109
if utils.IsYAML(string(bytes)) {
109110
_ = parsedNode.Decode(&jsonSpec)
110111
b, _ := json.Marshal(&jsonSpec)
111112
spec.SpecJSONBytes = &b
112113
spec.SpecJSON = &jsonSpec
113114
} else {
114-
_ = json.Unmarshal(bytes, &jsonSpec)
115+
if err := json.Unmarshal(bytes, &jsonSpec); err != nil {
116+
return err
117+
}
115118
spec.SpecJSONBytes = &bytes
116119
spec.SpecJSON = &jsonSpec
117120
}
121+
return nil
118122
}
119123

120124
//if !bypass {
@@ -148,7 +152,9 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
148152
}
149153

150154
// parse JSON
151-
parseJSON(spec, specInfo, &parsedSpec)
155+
if err := parseJSON(spec, specInfo, &parsedSpec); err != nil {
156+
return nil, fmt.Errorf("failed to parse json specification: %w", err)
157+
}
152158
parsed = true
153159

154160
// double check for the right version, people mix this up.
@@ -175,7 +181,9 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
175181
specInfo.APISchema = OpenAPI2SchemaData
176182

177183
// parse JSON
178-
parseJSON(spec, specInfo, &parsedSpec)
184+
if err := parseJSON(spec, specInfo, &parsedSpec); err != nil {
185+
return nil, fmt.Errorf("failed to parse json specification: %w", err)
186+
}
179187
parsed = true
180188

181189
// I am not certain this edge-case is very frequent, but let's make sure we handle it anyway.
@@ -199,7 +207,9 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
199207
// TODO: format for AsyncAPI.
200208

201209
// parse JSON
202-
parseJSON(spec, specInfo, &parsedSpec)
210+
if err := parseJSON(spec, specInfo, &parsedSpec); err != nil {
211+
return nil, fmt.Errorf("failed to parse json specification: %w", err)
212+
}
203213
parsed = true
204214

205215
// so far there is only 2 as a major release of AsyncAPI
@@ -214,7 +224,9 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
214224
if specInfo.SpecType == "" {
215225
// parse JSON
216226
if !bypass {
217-
parseJSON(spec, specInfo, &parsedSpec)
227+
if err := parseJSON(spec, specInfo, &parsedSpec); err != nil {
228+
return nil, fmt.Errorf("failed to parse json specification: %w", err)
229+
}
218230
parsed = true
219231
specInfo.Error = errors.New("spec type not supported by libopenapi, sorry")
220232
return specInfo, specInfo.Error
@@ -226,7 +238,9 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
226238
//}
227239

228240
if !parsed {
229-
parseJSON(spec, specInfo, &parsedSpec)
241+
if err := parseJSON(spec, specInfo, &parsedSpec); err != nil {
242+
return nil, fmt.Errorf("failed to parse json specification: %w", err)
243+
}
230244
}
231245

232246
// detect the original whitespace indentation

0 commit comments

Comments
 (0)