@@ -104,18 +104,22 @@ 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 {}
109- if utils .IsYAML (string (bytes )) {
109+ switch {
110+ case utils .IsYAML (string (bytes )):
110111 _ = parsedNode .Decode (& jsonSpec )
111112 b , _ := json .Marshal (& jsonSpec )
112113 spec .SpecJSONBytes = & b
113114 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+ }
116119 spec .SpecJSONBytes = & bytes
117120 spec .SpecJSON = & jsonSpec
118121 }
122+ return nil
119123 }
120124
121125 // if !bypass {
@@ -149,7 +153,10 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
149153 }
150154
151155 // 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+ }
153160 parsed = true
154161
155162 // double check for the right version, people mix this up.
@@ -176,7 +183,10 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
176183 specInfo .APISchema = OpenAPI2SchemaData
177184
178185 // 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+ }
180190 parsed = true
181191
182192 // 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
200210 // TODO: format for AsyncAPI.
201211
202212 // 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+ }
204217 parsed = true
205218
206219 // so far there is only 2 as a major release of AsyncAPI
@@ -215,7 +228,10 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
215228 if specInfo .SpecType == "" {
216229 // parse JSON
217230 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+ }
219235 parsed = true
220236 specInfo .Error = errors .New ("spec type not supported by libopenapi, sorry" )
221237 return specInfo , specInfo .Error
@@ -227,7 +243,10 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
227243 //}
228244
229245 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+ }
231250 }
232251
233252 // detect the original whitespace indentation
0 commit comments