[d24f17c] | 1 | import { isFreelyNamed, generateAbsoluteRefPatches } from '../helpers.js';
|
---|
| 2 | export default {
|
---|
| 3 | key: 'allOf',
|
---|
| 4 | plugin: (val, key, fullPath, specmap, patch) => {
|
---|
| 5 | // Ignore replace patches created by $ref because the changes will
|
---|
| 6 | // occur in the original "add" patch and we don't want this plugin
|
---|
| 7 | // to redundantly processes those "relace" patches.
|
---|
| 8 | if (patch.meta && patch.meta.$$ref) {
|
---|
| 9 | return undefined;
|
---|
| 10 | }
|
---|
| 11 | const parent = fullPath.slice(0, -1);
|
---|
| 12 | if (isFreelyNamed(parent)) {
|
---|
| 13 | return undefined;
|
---|
| 14 | }
|
---|
| 15 | if (!Array.isArray(val)) {
|
---|
| 16 | const err = new TypeError('allOf must be an array');
|
---|
| 17 | err.fullPath = fullPath; // This is an array
|
---|
| 18 | return err;
|
---|
| 19 | }
|
---|
| 20 | let alreadyAddError = false;
|
---|
| 21 |
|
---|
| 22 | // Find the original definition from the `patch.value` object
|
---|
| 23 | // Remove the `allOf` property so it doesn't get added to the result of the `allOf` plugin
|
---|
| 24 | let originalDefinitionObj = patch.value;
|
---|
| 25 | parent.forEach(part => {
|
---|
| 26 | if (!originalDefinitionObj) return; // bail out if we've lost sight of our target
|
---|
| 27 | originalDefinitionObj = originalDefinitionObj[part];
|
---|
| 28 | });
|
---|
| 29 | originalDefinitionObj = {
|
---|
| 30 | ...originalDefinitionObj
|
---|
| 31 | };
|
---|
| 32 |
|
---|
| 33 | // when we've lost sight, interrupt prematurely
|
---|
| 34 | if (Object.keys(originalDefinitionObj).length === 0) {
|
---|
| 35 | return undefined;
|
---|
| 36 | }
|
---|
| 37 | delete originalDefinitionObj.allOf;
|
---|
| 38 | const patches = [];
|
---|
| 39 |
|
---|
| 40 | // remove existing content
|
---|
| 41 | patches.push(specmap.replace(parent, {}));
|
---|
| 42 | val.forEach((toMerge, i) => {
|
---|
| 43 | if (!specmap.isObject(toMerge)) {
|
---|
| 44 | if (alreadyAddError) {
|
---|
| 45 | return null;
|
---|
| 46 | }
|
---|
| 47 | alreadyAddError = true;
|
---|
| 48 | const err = new TypeError('Elements in allOf must be objects');
|
---|
| 49 | err.fullPath = fullPath; // This is an array
|
---|
| 50 | return patches.push(err);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | // Deeply merge the member's contents onto the parent location
|
---|
| 54 | patches.push(specmap.mergeDeep(parent, toMerge));
|
---|
| 55 |
|
---|
| 56 | // Generate patches that migrate $ref values based on ContextTree information
|
---|
| 57 |
|
---|
| 58 | // remove ["allOf"], which will not be present when these patches are applied
|
---|
| 59 | const collapsedFullPath = fullPath.slice(0, -1);
|
---|
| 60 | const absoluteRefPatches = generateAbsoluteRefPatches(toMerge, collapsedFullPath, {
|
---|
| 61 | getBaseUrlForNodePath: nodePath => specmap.getContext([...fullPath, i, ...nodePath]).baseDoc,
|
---|
| 62 | specmap
|
---|
| 63 | });
|
---|
| 64 | patches.push(...absoluteRefPatches);
|
---|
| 65 | return undefined;
|
---|
| 66 | });
|
---|
| 67 |
|
---|
| 68 | // If there was an example in the original definition,
|
---|
| 69 | // keep it instead of merging with examples from other schema
|
---|
| 70 | if (originalDefinitionObj.example) {
|
---|
| 71 | // Delete other schema examples
|
---|
| 72 | patches.push(specmap.remove([].concat(parent, 'example')));
|
---|
| 73 | }
|
---|
| 74 | // Merge back the values from the original definition
|
---|
| 75 | patches.push(specmap.mergeDeep(parent, originalDefinitionObj));
|
---|
| 76 |
|
---|
| 77 | // If there was not an original $$ref value, make sure to remove
|
---|
| 78 | // any $$ref value that may exist from the result of `allOf` merges
|
---|
| 79 | if (!originalDefinitionObj.$$ref) {
|
---|
| 80 | patches.push(specmap.remove([].concat(parent, '$$ref')));
|
---|
| 81 | }
|
---|
| 82 | return patches;
|
---|
| 83 | }
|
---|
| 84 | }; |
---|