|
1 | 1 | const jsonPtr = require('json-ptr');
|
2 |
| -const $RefParser = require('json-schema-ref-parser'); |
3 |
| -module.exports = $RefParser; |
| 2 | +const JsonSchemaRefParser = require('json-schema-ref-parser'); |
4 | 3 |
|
5 | 4 | /**
|
6 | 5 | Purpose of this module is to protect $inherit.with from resolving local references inside it
|
7 | 6 | Bundle checks $ref's and fails when comes to JSON Patch document, so here we decorate 'resolve' and 'bundle' methods
|
8 | 7 | */
|
9 | 8 |
|
10 |
| -/** |
11 |
| - Set custom word instead of '$inherit' |
12 |
| -*/ |
13 |
| -$RefParser.prototype.setJybidInheritWord = function (word) { |
14 |
| - this._jybid_inherit_word = word || '$inherit'; |
15 |
| -}; |
16 |
| - |
17 | 9 | const PROTECTED_REF = 'this_$ref_is_protected_from_bundle';
|
18 | 10 |
|
19 | 11 | const maybe = (err, res, callback) => {
|
@@ -64,64 +56,71 @@ const inNodes = function (prefix, suffix, root, doc, cb) {
|
64 | 56 | return;
|
65 | 57 | }
|
66 | 58 |
|
67 |
| -$RefParser.prototype._jybid_processResolved = function (doc) { |
68 |
| - if (this._jybid_inherit_word == null) { |
69 |
| - this.setJybidInheritWord(); |
| 59 | +class PatchedJsonSchemaRefParser extends JsonSchemaRefParser { |
| 60 | + /** |
| 61 | + Set custom word instead of '$inherit' |
| 62 | + */ |
| 63 | + setJybidInheritWord(word) { |
| 64 | + this._jybid_inherit_word = word || '$inherit'; |
70 | 65 | }
|
71 |
| - // refs to #/... in $inherit/... should be preserved till the end |
72 |
| - // because it is what can be body of json-patch operations |
73 |
| - const re_local = /^#\//; |
74 |
| - inNodes(this._jybid_inherit_word, null, doc, doc, (node) => { |
75 |
| - inNodes('$ref', null, doc, node, (_node) => { |
76 |
| - if (_node.$ref.match(re_local)) { |
77 |
| -//console.error('protectRef', _node) |
78 |
| - protectRef(_node); |
79 |
| - } |
80 |
| - }); |
81 |
| - }); |
82 |
| -} |
83 | 66 |
|
84 |
| -$RefParser.prototype._jybid_processBundled = function (doc) { |
85 |
| - inNodes(PROTECTED_REF, null, doc, doc, unprotectRef); |
86 |
| -} |
| 67 | + _jybid_processResolved(doc) { |
| 68 | + if (this._jybid_inherit_word == null) { |
| 69 | + this.setJybidInheritWord(); |
| 70 | + } |
| 71 | + // refs to #/... in $inherit/... should be preserved till the end |
| 72 | + // because it is what can be body of json-patch operations |
| 73 | + const re_local = /^#\//; |
| 74 | + inNodes(this._jybid_inherit_word, null, doc, doc, (node) => { |
| 75 | + inNodes('$ref', null, doc, node, (_node) => { |
| 76 | + if (_node.$ref.match(re_local)) { |
| 77 | + //console.error('protectRef', _node) |
| 78 | + protectRef(_node); |
| 79 | + } |
| 80 | + }); |
| 81 | + }); |
| 82 | + } |
87 | 83 |
|
88 |
| -$RefParser.prototype._jybid_resolve = $RefParser.prototype.resolve; |
| 84 | + _jybid_processBundled(doc) { |
| 85 | + inNodes(PROTECTED_REF, null, doc, doc, unprotectRef); |
| 86 | + } |
89 | 87 |
|
90 |
| -$RefParser.prototype.resolve = function (path, schema, options, callback) { |
91 |
| - const promise = this._jybid_resolve(path, schema, options) |
92 |
| - .then((resolved) => { |
93 |
| - resolved.paths().forEach((_path) => { |
94 |
| - const doc = resolved.get(_path); |
95 |
| -//console.error('this._jybid_processResolved', JSON.stringify(doc, null, ' ')) |
96 |
| - if (path) { |
97 |
| - this._jybid_processResolved(doc); |
98 |
| - } |
99 |
| - resolved.set(_path, doc); |
| 88 | + resolve(path, schema, options, callback) { |
| 89 | + const promise = super.resolve(path, schema, options) |
| 90 | + .then((resolved) => { |
| 91 | + resolved.paths().forEach((_path) => { |
| 92 | + const doc = resolved.get(_path); |
| 93 | + //console.error('this._jybid_processResolved', JSON.stringify(doc, null, ' ')) |
| 94 | + if (path) { |
| 95 | + this._jybid_processResolved(doc); |
| 96 | + } |
| 97 | + resolved.set(_path, doc); |
| 98 | + }); |
| 99 | + //console.error('resolved', JSON.stringify(resolved, null, ' ')) |
| 100 | + return maybe(null, resolved, callback); |
| 101 | + }) |
| 102 | + .catch((e) => { |
| 103 | + e.message = `In resolve(path="${path}") ` + e.message; |
| 104 | + maybe(e, null, callback); |
100 | 105 | });
|
101 |
| -//console.error('resolved', JSON.stringify(resolved, null, ' ')) |
102 |
| - return maybe(null, resolved, callback); |
103 |
| - }) |
104 |
| - .catch((e) => { |
105 |
| - e.message = `In resolve(path="${path}") ` + e.message; |
106 |
| - maybe(e, null, callback); |
107 |
| - }); |
108 |
| - |
109 |
| - if (callback == null) return promise; |
110 |
| -}; |
| 106 | + |
| 107 | + if (callback == null) return promise; |
| 108 | + } |
111 | 109 |
|
112 |
| -$RefParser.prototype._jybid_bundle = $RefParser.prototype.bundle; |
| 110 | + bundle(path, schema, options, callback) { |
| 111 | + const promise = super.bundle(path, schema, options) |
| 112 | + .then((bundled) => { |
| 113 | + //console.error('bundled', JSON.stringify(bundled, null, ' ')) |
| 114 | + this._jybid_processBundled(bundled); |
| 115 | + return maybe(null, bundled, callback); |
| 116 | + }) |
| 117 | + .catch((e) => { |
| 118 | + e.message = `In bundle(path="${path}") ` + e.message; |
| 119 | + maybe(e, null, callback); |
| 120 | + }); |
| 121 | + |
| 122 | + if (callback == null) return promise; |
| 123 | + } |
| 124 | +} |
113 | 125 |
|
114 |
| -$RefParser.prototype.bundle = function (path, schema, options, callback) { |
115 |
| - const promise = this._jybid_bundle(path, schema, options) |
116 |
| - .then((bundled) => { |
117 |
| -//console.error('bundled', JSON.stringify(bundled, null, ' ')) |
118 |
| - this._jybid_processBundled(bundled); |
119 |
| - return maybe(null, bundled, callback); |
120 |
| - }) |
121 |
| - .catch((e) => { |
122 |
| - e.message = `In bundle(path="${path}") ` + e.message; |
123 |
| - maybe(e, null, callback); |
124 |
| - }); |
125 |
| - |
126 |
| - if (callback == null) return promise; |
127 |
| -}; |
| 126 | +module.exports = PatchedJsonSchemaRefParser |
0 commit comments