Skip to content

Commit c366493

Browse files
authored
fix: check node type before treating object as reference (#1719) (#1720)
ref #1719
1 parent 3554539 commit c366493

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

src/specmap/helpers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import traverse from 'traverse';
22
import URL from 'url';
3+
import isString from 'lodash/isString';
34

45
// This will match if the direct parent's key exactly matches an item.
56
const freelyNamedKeyParents = ['properties'];
@@ -58,7 +59,7 @@ export function generateAbsoluteRefPatches(
5859
const patches = [];
5960

6061
traverse(obj).forEach(function callback() {
61-
if (targetKeys.indexOf(this.key) > -1) {
62+
if (targetKeys.includes(this.key) && isString(this.node)) {
6263
const nodePath = this.path; // this node's path, relative to `obj`
6364
const fullPath = basePath.concat(this.path);
6465

test/bugs/1719.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// https://github.com/swagger-api/swagger-js/issues/1719
2+
3+
import path from 'path';
4+
import fs from 'fs';
5+
import jsYaml from 'js-yaml';
6+
7+
import resolveSubtree from '../../src/subtree-resolver';
8+
9+
const testDoc = jsYaml.safeLoad(
10+
fs.readFileSync(path.join('test', 'data', 'issue-1719-ref-object-reference.yaml'), 'utf8')
11+
);
12+
13+
test('#1719: $ref object should not be treated as a reference (string)', async () => {
14+
const specPath = ['paths', '/throwError', 'post'];
15+
const res = await resolveSubtree(testDoc, specPath);
16+
17+
expect(res.errors.length).toEqual(0);
18+
expect(res.spec).toHaveProperty(
19+
[
20+
'requestBody',
21+
'content',
22+
'application/json',
23+
'schema',
24+
'properties',
25+
'propertyOne',
26+
'items',
27+
'properties',
28+
'propertyTwo',
29+
'properties',
30+
'$ref',
31+
],
32+
{ type: 'string' }
33+
);
34+
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
openapi: 3.0.0
2+
servers:
3+
- description: "mock definition for swagger-client issue #1719"
4+
url: https://test.com/v1
5+
info:
6+
version: "0.0.1"
7+
title: Test Defect
8+
description: >-
9+
A client side JS error occured when the throwError endpoint was expanded in
10+
the UI Docs panel. This error was specifically visible when a $ref is seen
11+
by the subtree resolver after an allOf or after an anyOf. E.g. the subtree
12+
resolver was treating the $ref as a reference instead as an object.
13+
paths:
14+
/throwError:
15+
post:
16+
summary: Opening this documentation (previously) throws an error
17+
description: Allows baskets of new prospects to be imported in to a portal
18+
responses:
19+
'200':
20+
description: OK
21+
requestBody:
22+
content:
23+
application/json:
24+
schema:
25+
$ref: '#/components/schemas/1_SchemaRef'
26+
description: the data to import
27+
required: true
28+
components:
29+
schemas:
30+
1_SchemaRef:
31+
type: object
32+
properties:
33+
propertyOne:
34+
type: array
35+
items:
36+
$ref: '#/components/schemas/2_RefWithAllOf'
37+
38+
2_RefWithAllOf:
39+
allOf:
40+
- $ref: '#/components/schemas/3_InnerAllOfRef'
41+
42+
3_InnerAllOfRef:
43+
type: object
44+
properties:
45+
propertyTwo:
46+
$ref: '#/components/schemas/0_TargetRef'
47+
48+
0_TargetRef:
49+
type: object
50+
properties:
51+
$ref: # this $ref should be treated as an object, not a reference string
52+
type: string
53+
devTestRefId:
54+
type: string
55+

0 commit comments

Comments
 (0)