Skip to content

Commit dc18274

Browse files
Merge branch 'master' into v5
2 parents 9cc4c18 + 3cc9c4b commit dc18274

26 files changed

+4931
-2804
lines changed

.travis.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@ addons:
1212
matrix:
1313
include:
1414
# Local Browsers
15-
- node_js: 8
15+
- node_js: 10
1616
env: KARMA=true KARMA_COVERAGE=true
1717

1818
# Sauce Labs
19-
- node_js: 8
19+
- node_js: 10
2020
env: KARMA=true KARMA_SAUCE=true
2121

2222
# Node
2323
- node_js: 4
2424
- node_js: 5
2525
- node_js: 6
2626
- node_js: 7
27+
- node_js: 8
28+
- node_js: 9
2729

2830
before_script:
2931
# Setup a virtual display for browser testing

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ All notable changes will be documented in this file.
33
JSON Schema $Ref Parser adheres to [Semantic Versioning](http://semver.org/).
44

55

6+
## [v5.1.0](https://github.com/BigstickCarpet/json-schema-ref-parser/tree/v5.1.0) (2018-07-11)
7+
8+
- Improved the logic of the [`bundle()` method](https://github.com/BigstickCarpet/json-schema-ref-parser/blob/master/docs/ref-parser.md#bundleschema-options-callback) to produce shorter reference paths when possible. This is not a breaking change, since both the old reference paths and the new reference paths are valid. The new ones are just shorter. Big thanks to [@hipstersmoothie](https://github.com/hipstersmoothie) for [PR #68](https://github.com/BigstickCarpet/json-schema-ref-parser/pull/68), which helped a lot with this.
9+
10+
[Full Changelog](https://github.com/BigstickCarpet/json-schema-ref-parser/compare/v5.0.0...v5.1.0)
11+
12+
613
## [v5.0.0](https://github.com/BigstickCarpet/json-schema-ref-parser/tree/v5.0.0) (2018-03-18)
714

815
This release contains two bug fixes related to file paths. They are _technically_ breaking changes — hence the major version bump — but they're both edge cases that probably won't affect most users.

dist/ref-parser.js

Lines changed: 541 additions & 501 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ref-parser.js.map

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ref-parser.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ref-parser.min.js.map

Lines changed: 15 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<html>
22
<head>
3-
<meta http-equiv="refresh" content="0; url=tests/index.html">
3+
<meta http-equiv="refresh" content="0; url=test/index.html">
44
</head>
55
<body>
66
<script>
7-
window.___location = 'tests/index.html';
7+
window.___location = 'test/index.html';
88
</script>
99
</body>
1010
</html>

lib/bundle.js

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,25 @@ function crawl (parent, key, path, pathFromRoot, indirections, inventory, $refs,
4545
inventory$Ref(parent, key, path, pathFromRoot, indirections, inventory, $refs, options);
4646
}
4747
else {
48-
var keys = Object.keys(obj);
49-
50-
// Most people will expect references to be bundled into the the "definitions" property,
51-
// so we always crawl that property first, if it exists.
52-
var defs = keys.indexOf('definitions');
53-
if (defs > 0) {
54-
keys.splice(0, 0, keys.splice(defs, 1)[0]);
55-
}
48+
// Crawl the object in a specific order that's optimized for bundling.
49+
// This is important because it determines how `pathFromRoot` gets built,
50+
// which later determines which keys get dereferenced and which ones get remapped
51+
var keys = Object.keys(obj)
52+
.sort(function (a, b) {
53+
// Most people will expect references to be bundled into the the "definitions" property,
54+
// so we always crawl that property first, if it exists.
55+
if (a === 'definitions') {
56+
return -1;
57+
}
58+
else if (b === 'definitions') {
59+
return 1;
60+
}
61+
else {
62+
// Otherwise, crawl the keys based on their length.
63+
// This produces the shortest possible bundled references
64+
return a.length - b.length;
65+
}
66+
});
5667

5768
keys.forEach(function (key) {
5869
var keyPath = Pointer.join(path, key);
@@ -150,26 +161,43 @@ function remap (inventory) {
150161
// Group & sort all the $ref pointers, so they're in the order that we need to dereference/remap them
151162
inventory.sort(function (a, b) {
152163
if (a.file !== b.file) {
153-
return a.file < b.file ? -1 : +1; // Group all the $refs that point to the same file
164+
// Group all the $refs that point to the same file
165+
return a.file < b.file ? -1 : +1;
154166
}
155167
else if (a.hash !== b.hash) {
156-
return a.hash < b.hash ? -1 : +1; // Group all the $refs that point to the same part of the file
168+
// Group all the $refs that point to the same part of the file
169+
return a.hash < b.hash ? -1 : +1;
157170
}
158171
else if (a.circular !== b.circular) {
159-
return a.circular ? -1 : +1; // If the $ref points to itself, then sort it higher than other $refs that point to this $ref
172+
// If the $ref points to itself, then sort it higher than other $refs that point to this $ref
173+
return a.circular ? -1 : +1;
160174
}
161175
else if (a.extended !== b.extended) {
162-
return a.extended ? +1 : -1; // If the $ref extends the resolved value, then sort it lower than other $refs that don't extend the value
176+
// If the $ref extends the resolved value, then sort it lower than other $refs that don't extend the value
177+
return a.extended ? +1 : -1;
163178
}
164179
else if (a.indirections !== b.indirections) {
165-
return a.indirections - b.indirections; // Sort direct references higher than indirect references
180+
// Sort direct references higher than indirect references
181+
return a.indirections - b.indirections;
166182
}
167183
else if (a.depth !== b.depth) {
168-
return a.depth - b.depth; // Sort $refs by how close they are to the JSON Schema root
184+
// Sort $refs by how close they are to the JSON Schema root
185+
return a.depth - b.depth;
169186
}
170187
else {
171-
// If all else is equal, use the $ref that's in the "definitions" property
172-
return b.pathFromRoot.lastIndexOf('/definitions') - a.pathFromRoot.lastIndexOf('/definitions');
188+
// Determine how far each $ref is from the "definitions" property.
189+
// Most people will expect references to be bundled into the the "definitions" property if possible.
190+
var aDefinitionsIndex = a.pathFromRoot.lastIndexOf('/definitions');
191+
var bDefinitionsIndex = b.pathFromRoot.lastIndexOf('/definitions');
192+
193+
if (aDefinitionsIndex !== bDefinitionsIndex) {
194+
// Give higher priority to the $ref that's closer to the "definitions" property
195+
return bDefinitionsIndex - aDefinitionsIndex;
196+
}
197+
else {
198+
// All else is equal, so use the shorter path, which will produce the shortest possible reference
199+
return a.pathFromRoot.length - b.pathFromRoot.length;
200+
}
173201
}
174202
});
175203

lib/pointer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ Pointer.parse = function (path) {
167167

168168
// Decode each part, according to RFC 6901
169169
for (var i = 0; i < pointer.length; i++) {
170-
pointer[i] = decodeURI(pointer[i].replace(escapedSlash, '/').replace(escapedTilde, '~'));
170+
pointer[i] = decodeURIComponent(pointer[i].replace(escapedSlash, '/').replace(escapedTilde, '~'));
171171
}
172172

173173
if (pointer[0] !== '') {
@@ -195,7 +195,7 @@ Pointer.join = function (base, tokens) {
195195
for (var i = 0; i < tokens.length; i++) {
196196
var token = tokens[i];
197197
// Encode the token, according to RFC 6901
198-
base += '/' + encodeURI(token.replace(tildes, '~0').replace(slashes, '~1'));
198+
base += '/' + encodeURIComponent(token.replace(tildes, '~0').replace(slashes, '~1'));
199199
}
200200

201201
return base;

0 commit comments

Comments
 (0)