@@ -104,18 +104,22 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
104
104
_ , openAPI2 := utils .FindKeyNode (utils .OpenApi2 , parsedSpec .Content )
105
105
_ , asyncAPI := utils .FindKeyNode (utils .AsyncApi , parsedSpec .Content )
106
106
107
- parseJSON := func (bytes []byte , spec * SpecInfo , parsedNode * yaml.Node ) {
107
+ parseJSON := func (bytes []byte , spec * SpecInfo , parsedNode * yaml.Node ) error {
108
108
var jsonSpec map [string ]interface {}
109
- if utils .IsYAML (string (bytes )) {
109
+ switch {
110
+ case utils .IsYAML (string (bytes )):
110
111
_ = parsedNode .Decode (& jsonSpec )
111
112
b , _ := json .Marshal (& jsonSpec )
112
113
spec .SpecJSONBytes = & b
113
114
spec .SpecJSON = & jsonSpec
114
- } else {
115
- _ = json .Unmarshal (bytes , & jsonSpec )
115
+ case utils .IsJSON (string (bytes )):
116
+ if err := json .Unmarshal (bytes , & jsonSpec ); err != nil {
117
+ return err
118
+ }
116
119
spec .SpecJSONBytes = & bytes
117
120
spec .SpecJSON = & jsonSpec
118
121
}
122
+ return nil
119
123
}
120
124
121
125
// if !bypass {
@@ -149,7 +153,10 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
149
153
}
150
154
151
155
// parse JSON
152
- parseJSON (spec , specInfo , & parsedSpec )
156
+ if err := parseJSON (spec , specInfo , & parsedSpec ); err != nil {
157
+ specInfo .Error = fmt .Errorf ("unable to parse specification: %w" , err )
158
+ return specInfo , specInfo .Error
159
+ }
153
160
parsed = true
154
161
155
162
// double check for the right version, people mix this up.
@@ -176,7 +183,10 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
176
183
specInfo .APISchema = OpenAPI2SchemaData
177
184
178
185
// parse JSON
179
- parseJSON (spec , specInfo , & parsedSpec )
186
+ if err := parseJSON (spec , specInfo , & parsedSpec ); err != nil {
187
+ specInfo .Error = fmt .Errorf ("unable to parse specification: %w" , err )
188
+ return specInfo , specInfo .Error
189
+ }
180
190
parsed = true
181
191
182
192
// I am not certain this edge-case is very frequent, but let's make sure we handle it anyway.
@@ -200,7 +210,10 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
200
210
// TODO: format for AsyncAPI.
201
211
202
212
// parse JSON
203
- parseJSON (spec , specInfo , & parsedSpec )
213
+ if err := parseJSON (spec , specInfo , & parsedSpec ); err != nil {
214
+ specInfo .Error = fmt .Errorf ("unable to parse specification: %w" , err )
215
+ return specInfo , nil
216
+ }
204
217
parsed = true
205
218
206
219
// so far there is only 2 as a major release of AsyncAPI
@@ -215,7 +228,10 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
215
228
if specInfo .SpecType == "" {
216
229
// parse JSON
217
230
if ! bypass {
218
- parseJSON (spec , specInfo , & parsedSpec )
231
+ if err := parseJSON (spec , specInfo , & parsedSpec ); err != nil {
232
+ specInfo .Error = errors .New ("spec type not supported by libopenapi, sorry" )
233
+ return specInfo , specInfo .Error
234
+ }
219
235
parsed = true
220
236
specInfo .Error = errors .New ("spec type not supported by libopenapi, sorry" )
221
237
return specInfo , specInfo .Error
@@ -227,7 +243,10 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
227
243
//}
228
244
229
245
if ! parsed {
230
- parseJSON (spec , specInfo , & parsedSpec )
246
+ if err := parseJSON (spec , specInfo , & parsedSpec ); err != nil {
247
+ specInfo .Error = fmt .Errorf ("unable to parse specification: %w" , err )
248
+ return specInfo , specInfo .Error
249
+ }
231
250
}
232
251
233
252
// detect the original whitespace indentation
0 commit comments