@@ -7,10 +7,11 @@ import (
7
7
"encoding/json"
8
8
"errors"
9
9
"fmt"
10
- "github.com/pb33f/libopenapi/utils"
11
- "gopkg.in/yaml.v3"
12
10
"strings"
13
11
"time"
12
+
13
+ "github.com/pb33f/libopenapi/utils"
14
+ "gopkg.in/yaml.v3"
14
15
)
15
16
16
17
const (
@@ -103,18 +104,21 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
103
104
_ , openAPI2 := utils .FindKeyNode (utils .OpenApi2 , parsedSpec .Content )
104
105
_ , asyncAPI := utils .FindKeyNode (utils .AsyncApi , parsedSpec .Content )
105
106
106
- parseJSON := func (bytes []byte , spec * SpecInfo , parsedNode * yaml.Node ) {
107
+ parseJSON := func (bytes []byte , spec * SpecInfo , parsedNode * yaml.Node ) error {
107
108
var jsonSpec map [string ]interface {}
108
109
if utils .IsYAML (string (bytes )) {
109
110
_ = parsedNode .Decode (& jsonSpec )
110
111
b , _ := json .Marshal (& jsonSpec )
111
112
spec .SpecJSONBytes = & b
112
113
spec .SpecJSON = & jsonSpec
113
114
} else {
114
- _ = json .Unmarshal (bytes , & jsonSpec )
115
+ if err := json .Unmarshal (bytes , & jsonSpec ); err != nil {
116
+ return err
117
+ }
115
118
spec .SpecJSONBytes = & bytes
116
119
spec .SpecJSON = & jsonSpec
117
120
}
121
+ return nil
118
122
}
119
123
120
124
//if !bypass {
@@ -148,7 +152,9 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
148
152
}
149
153
150
154
// 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
+ }
152
158
parsed = true
153
159
154
160
// double check for the right version, people mix this up.
@@ -175,7 +181,9 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
175
181
specInfo .APISchema = OpenAPI2SchemaData
176
182
177
183
// 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
+ }
179
187
parsed = true
180
188
181
189
// 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
199
207
// TODO: format for AsyncAPI.
200
208
201
209
// 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
+ }
203
213
parsed = true
204
214
205
215
// so far there is only 2 as a major release of AsyncAPI
@@ -214,7 +224,9 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
214
224
if specInfo .SpecType == "" {
215
225
// parse JSON
216
226
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
+ }
218
230
parsed = true
219
231
specInfo .Error = errors .New ("spec type not supported by libopenapi, sorry" )
220
232
return specInfo , specInfo .Error
@@ -226,7 +238,9 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
226
238
//}
227
239
228
240
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
+ }
230
244
}
231
245
232
246
// detect the original whitespace indentation
0 commit comments