Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ function remap (inventory) {
else if (a.depth !== b.depth) {
return a.depth - b.depth; // Sort $refs by how close they are to the JSON Schema root
}
else if (a.pathFromRoot.lastIndexOf('/definitions') === -1 && b.pathFromRoot.lastIndexOf('/definitions') === -1) {
return a.pathFromRoot.localeCompare(b.pathFromRoot);
}
else {
// If all else is equal, use the $ref that's in the "definitions" property
return b.pathFromRoot.lastIndexOf('/definitions') - a.pathFromRoot.lastIndexOf('/definitions');
Expand Down
49 changes: 49 additions & 0 deletions test/specs/complexOrder/complexOrder.bundled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
helper.bundled.complexOrder = {
$schema: 'http://json-schema.org/draft-07/schema#',
properties: {
actions: {
type: 'object',
properties: {
affirmativeAction: {
type: 'object',
properties: {
$id: 'text_assets',
oneOf: [
{
$ref: '#/properties/actions/properties/affirmativeAction/properties/definitions/asset'
},
{
$ref: '#/properties/actions/properties/affirmativeAction/properties/definitions/asset'
}
],
definitions: {
switchWrapper: {
type: 'object',
$ref: '#/properties/actions/properties/affirmativeAction/properties/definitions/switch'
},
asset: {
type: 'object',
$id: 'asset_action',
properties: {
label: {
$ref: '#/properties/actions/properties/affirmativeAction/properties'
}
}
},
switch: {
type: 'array',
$ref: '#/properties/actions/properties/affirmativeAction/properties/definitions/asset'
}
}
}
},
negativeAction: {
$ref: '#/properties/actions/properties/affirmativeAction'
},
prevAction: {
$ref: '#/properties/actions/properties/affirmativeAction'
}
}
}
}
};
14 changes: 14 additions & 0 deletions test/specs/complexOrder/complexOrder.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
describe('$refs that are in the same file but also circular', function () {
'use strict';

it('should bundle successfully', function () {
var parser = new $RefParser();

return parser
.bundle(path.rel('specs/complexOrder/definitions/root.json'))
.then(function (schema) {
expect(schema).to.deep.equal(parser.schema);
expect(schema).to.deep.equal(helper.bundled.complexOrder);
});
});
});
8 changes: 8 additions & 0 deletions test/specs/complexOrder/definitions/action.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$id": "asset_action",
"properties": {
"label": {
"$ref": "text_assets.json"
}
}
}
29 changes: 29 additions & 0 deletions test/specs/complexOrder/definitions/def.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$id": "def",
"definitions": {
"container": {
"properties": {
"actions": {
"type": "object",
"properties": {
"affirmativeAction": {
"$ref": "#/definitions/actions"
},
"negativeAction": {
"$ref": "#/definitions/actions"
},
"prevAction": {
"$ref": "#/definitions/actions"
}
}
}
}
},
"actions": {
"type": "object",
"properties": {
"$ref": "text_assets.json"
}
}
}
}
4 changes: 4 additions & 0 deletions test/specs/complexOrder/definitions/root.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "def.json#/definitions/container"
}
26 changes: 26 additions & 0 deletions test/specs/complexOrder/definitions/text_assets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"$id": "text_assets",
"oneOf": [
{
"$ref": "#/definitions/asset"
},
{
"$ref": "#/definitions/asset"
}
],
"definitions": {
"switchWrapper": {
"type": "object",
"$ref": "#/definitions/switch"
},
"asset": {
"type": "object",
"$ref": "action.json"
},
"switch": {
"type": "array",
"$ref": "#/definitions/asset"
}
}
}