Skip to content
This repository has been archived by the owner on Jul 2, 2019. It is now read-only.

Commit

Permalink
Merge pull request #237 from apiaryio/kylef/deref-object
Browse files Browse the repository at this point in the history
Don't try to dereference non-ref properties called $ref
  • Loading branch information
pksunkara authored Dec 11, 2018
2 parents f4b7370 + a36c68d commit de3db0e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,32 @@
The parser will now generate a JSON body of `["Doe"]` whereas in previous
versions would've resulted in an empty array.

- Fixes parsing Swagger documents that contain properties called `$ref`.
Previously the parser would attempt to dereference the property as would
generically try to dereference any key in an object called `$ref`, as per the
JSON Schema specification, references are only permitted when a schema type
is expected.

For example, the following is a schema which is trying to describe an object
with a property called `$ref`, previously the parser would attempt to
dereference `$ref` and crash in the process.

```yaml
type: object
properties:
$ref:
type: string
example: '#/definitions/User'
required: ['$ref']
```

There is a still an open known issue
[#235](https://github.com/apiaryio/fury-adapter-swagger/issues/235) that
uses of `$ref` in a Swagger 2 document where the value a string will cause
the parser to attempt to dereference it incorrectly when the `$ref` property
is found on an object that does not support `$ref` as per the Swagger 2 and
JSON Schema specifications.

## 0.22.6 (2018-12-07)

### Enhancements
Expand Down
2 changes: 1 addition & 1 deletion src/json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export function dereference(example, root, paths, path) {
return example;
}

if (example.$ref) {
if (example.$ref && _.isString(example.$ref)) {
const refPath = example.$ref.split('/');
const currentPath = (path || []).join('/');

Expand Down
23 changes: 23 additions & 0 deletions test/json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -775,4 +775,27 @@ describe('Dereferencing', () => {
},
});
});

it('does not dereference non-string references', () => {
// https://github.com/apiaryio/fury-adapter-swagger/issues/235

// Schema for a JSON ReferencePointer
const result = dereference({
type: 'object',
properties: {
$ref: {
type: 'string',
},
},
});

expect(result).to.deep.equal({
type: 'object',
properties: {
$ref: {
type: 'string',
},
},
});
});
});

0 comments on commit de3db0e

Please sign in to comment.