Skip to content

Commit 73a9ace

Browse files
committed
chore: exit early when failed to parse json spec
1 parent 1f17c50 commit 73a9ace

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

datamodel/spec_info.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,21 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
104104
_, openAPI2 := utils.FindKeyNode(utils.OpenApi2, parsedSpec.Content)
105105
_, asyncAPI := utils.FindKeyNode(utils.AsyncApi, parsedSpec.Content)
106106

107-
parseJSON := func(bytes []byte, spec *SpecInfo, parsedNode *yaml.Node) {
107+
parseJSON := func(bytes []byte, spec *SpecInfo, parsedNode *yaml.Node) error {
108108
var jsonSpec map[string]interface{}
109109
if utils.IsYAML(string(bytes)) {
110110
_ = parsedNode.Decode(&jsonSpec)
111111
b, _ := json.Marshal(&jsonSpec)
112112
spec.SpecJSONBytes = &b
113113
spec.SpecJSON = &jsonSpec
114114
} else {
115-
_ = json.Unmarshal(bytes, &jsonSpec)
115+
if err := json.Unmarshal(bytes, &jsonSpec); err != nil {
116+
return err
117+
}
116118
spec.SpecJSONBytes = &bytes
117119
spec.SpecJSON = &jsonSpec
118120
}
121+
return nil
119122
}
120123

121124
// if !bypass {
@@ -149,7 +152,9 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
149152
}
150153

151154
// parse JSON
152-
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+
}
153158
parsed = true
154159

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

178183
// parse JSON
179-
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+
}
180187
parsed = true
181188

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

202209
// parse JSON
203-
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+
}
204213
parsed = true
205214

206215
// so far there is only 2 as a major release of AsyncAPI
@@ -215,7 +224,9 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
215224
if specInfo.SpecType == "" {
216225
// parse JSON
217226
if !bypass {
218-
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+
}
219230
parsed = true
220231
specInfo.Error = errors.New("spec type not supported by libopenapi, sorry")
221232
return specInfo, specInfo.Error
@@ -227,7 +238,9 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
227238
//}
228239

229240
if !parsed {
230-
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+
}
231244
}
232245

233246
// detect the original whitespace indentation

0 commit comments

Comments
 (0)