* Clone an OpenAPI spec. When a `$ref` is encountered, we store it as `x-$ref` * and keep the stringified original value as `x-$original-value`. These * metadata allows us to access the original object after `$ref` is resolved * and dereferenced by the parser. * * @param {*} spec An OpenAPI spe
(spec)
| 194 | * @param {*} spec An OpenAPI spec object |
| 195 | */ |
| 196 | function cloneSpecObject(spec) { |
| 197 | return _.cloneDeepWith(spec, item => { |
| 198 | /** |
| 199 | * A yaml object below produces `null` for `servers.url` |
| 200 | * ```yaml |
| 201 | * servers: |
| 202 | * - url: |
| 203 | * description: null url for testing |
| 204 | * ``` |
| 205 | */ |
| 206 | if (item != null && item.$ref) { |
| 207 | const copy = _.cloneDeep(item); |
| 208 | return { |
| 209 | ...copy, |
| 210 | // Store the original item in `x-$original` |
| 211 | 'x-$original-value': json5.stringify(item, null, 2), |
| 212 | // Keep `$ref` as `x-$ref` as `$ref` will be removed during dereferencing |
| 213 | 'x-$ref': item.$ref, |
| 214 | }; |
| 215 | } |
| 216 | }); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Print an OpenAPI spec object as JavaScript object literal. The original value |