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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"cross-fetch": "0.0.8",
"deep-extend": "^0.4.1",
"encode-3986": "^1.0.0",
"fast-json-patch": "1.1.8",
"fast-json-patch": "^1.2.2",
"isomorphic-form-data": "0.0.1",
"js-yaml": "^3.8.1",
"lodash": "^4.16.2",
Expand All @@ -77,4 +77,4 @@
"utf8-bytes": "0.0.1",
"utfstring": "^2.0.0"
}
}
}
4 changes: 3 additions & 1 deletion src/specmap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,10 @@ class SpecMap {
}

updateMutations(patch) {
if (lib.applyPatch(this.state, patch, {allowMetaPatches: this.allowMetaPatches})) {
const result = lib.applyPatch(this.state, patch, {allowMetaPatches: this.allowMetaPatches})
if (result) {
this.mutations.push(patch)
this.state = result
}
}

Expand Down
60 changes: 48 additions & 12 deletions src/specmap/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,62 @@ function applyPatch(obj, patch, opts) {
})

if (patch.op === 'merge') {
const valPatch = _get(patch.path)
jsonPatch.apply(obj, [valPatch])
Object.assign(valPatch.value, patch.value)
const newValue = getInByJsonPath(obj, patch.path)
Object.assign(newValue, patch.value)
jsonPatch.applyPatch(obj, [replace(patch.path, newValue)])
}
else if (patch.op === 'mergeDeep') {
const valPatch = _get(patch.path)
jsonPatch.apply(obj, [valPatch])
const origValPatchValue = Object.assign({}, valPatch.value)
deepExtend(valPatch.value, patch.value)
const currentValue = getInByJsonPath(obj, patch.path)
const origValPatchValue = Object.assign({}, currentValue)
deepExtend(currentValue, patch.value)

// deepExtend doesn't merge arrays, so we will do it manually
for (const prop in patch.value) {
if (Object.prototype.hasOwnProperty.call(patch.value, prop)) {
const propVal = patch.value[prop]
if (Array.isArray(propVal)) {
const existing = origValPatchValue[prop] || []
valPatch.value[prop] = existing.concat(propVal)
currentValue[prop] = existing.concat(propVal)
}
}
}
}
else if (patch.op === 'add' && patch.path === '' && isObject(patch.value)) {
// { op: 'add', path: '', value: { a: 1, b: 2 }}
// has no effect: json patch refuses to do anything.
// so let's break that patch down into a set of patches,
// one for each key in the intended root value.

const patches = Object.keys(patch.value)
.reduce((arr, key) => {
arr.push({
op: 'add',
path: `/${normalizeJSONPath(key)}`,
value: patch.value[key]
})
return arr
}, [])

jsonPatch.applyPatch(obj, patches)
}
else if (patch.op === 'replace' && patch.path === '') {
let value = patch.value

if (opts.allowMetaPatches && patch.meta && isAdditiveMutation(patch) &&
(Array.isArray(patch.value) || isObject(patch.value))) {
value = Object.assign({}, value, patch.meta)
}
obj = value
}
else {
jsonPatch.apply(obj, [patch])
jsonPatch.applyPatch(obj, [patch])

// Attach metadata to the resulting value.
if (opts.allowMetaPatches && patch.meta && isAdditiveMutation(patch) &&
(Array.isArray(patch.value) || isObject(patch.value))) {
const valPatch = _get(patch.path)
jsonPatch.apply(obj, [valPatch])
Object.assign(valPatch.value, patch.meta)
const currentValue = getInByJsonPath(obj, patch.path)
const newValue = Object.assign({}, currentValue, patch.meta)
jsonPatch.applyPatch(obj, [replace(patch.path, newValue)])
}
}

Expand Down Expand Up @@ -302,3 +328,13 @@ function isContextPatch(patch) {
function isPatch(patch) {
return patch && typeof patch === 'object'
}

function getInByJsonPath(obj, jsonPath) {
try {
return jsonPatch.getValueByPointer(obj, jsonPath)
}
catch (e) {
console.error(e) // eslint-disable-line no-console
return {}
}
}
4 changes: 1 addition & 3 deletions test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,6 @@ describe('resolver', () => {
},
definitions: {
Category: {
$$ref: '#/definitions/Category', // FIXME: benign, but this should not be present
type: 'object',
properties: {
id: {
Expand All @@ -460,14 +459,13 @@ describe('resolver', () => {
}
},
Pet: {
$$ref: '#/definitions/Pet',
type: 'object',
required: [
'category'
],
properties: {
category: {
$$ref: '#/definitions/Category', // FIXME: benign, but this should not be present
$$ref: '#/definitions/Category',
type: 'object',
properties: {
id: {
Expand Down
2 changes: 0 additions & 2 deletions test/specmap/all-of.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ describe('allOf', function () {
errors: [],
spec: {
Pet: {
$$ref: '#/Pet',
type: 'object',
properties: {
name: {
Expand All @@ -137,7 +136,6 @@ describe('allOf', function () {
}
},
Cat: {
$$ref: '#/Cat',
properties: {
meow: {
type: 'string'
Expand Down
1 change: 1 addition & 0 deletions test/specmap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ describe('specmap', function () {
})
.then((res) => {
expect(res.spec.one.$$ref).toEqual('#/two')
expect(res.spec.two.$$ref).toEqual(undefined)
})
})

Expand Down