Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support export for SO with circular refs #81582

Merged
merged 5 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('getObjectReferencesToFetch()', () => {
`);
});

test(`doesn't deal with circular dependencies`, () => {
test('does not fail on circular dependencies', () => {
const map = new Map<string, SavedObject>();
map.set('index-pattern:1', {
id: '1',
Expand Down Expand Up @@ -527,7 +527,7 @@ describe('injectNestedDependencies', () => {
`);
});

test(`doesn't deal with circular dependencies`, async () => {
test('does not fail on circular dependencies', async () => {
const savedObjects = [
{
id: '2',
Expand Down
284 changes: 155 additions & 129 deletions src/core/server/saved_objects/export/sort_objects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,27 @@ describe('sortObjects()', () => {
},
];
expect(sortObjects(docs)).toMatchInlineSnapshot(`
Array [
Object {
"attributes": Object {},
"id": "1",
"references": Array [],
"type": "index-pattern",
},
Object {
"attributes": Object {},
"id": "2",
"references": Array [
Object {
"id": "1",
"name": "ref1",
"type": "index-pattern",
},
],
"type": "search",
},
]
`);
Array [
Object {
"attributes": Object {},
"id": "1",
"references": Array [],
"type": "index-pattern",
},
Object {
"attributes": Object {},
"id": "2",
"references": Array [
Object {
"id": "1",
"name": "ref1",
"type": "index-pattern",
},
],
"type": "search",
},
]
`);
});

test('should not mutate parameter', () => {
Expand All @@ -91,49 +91,49 @@ Array [
},
];
expect(sortObjects(docs)).toMatchInlineSnapshot(`
Array [
Object {
"attributes": Object {},
"id": "1",
"references": Array [],
"type": "index-pattern",
},
Object {
"attributes": Object {},
"id": "2",
"references": Array [
Object {
"id": "1",
"name": "ref1",
"type": "index-pattern",
},
],
"type": "search",
},
]
`);
Array [
Object {
"attributes": Object {},
"id": "1",
"references": Array [],
"type": "index-pattern",
},
Object {
"attributes": Object {},
"id": "2",
"references": Array [
Object {
"id": "1",
"name": "ref1",
"type": "index-pattern",
},
],
"type": "search",
},
]
`);
expect(docs).toMatchInlineSnapshot(`
Array [
Object {
"attributes": Object {},
"id": "2",
"references": Array [
Object {
"id": "1",
"name": "ref1",
"type": "index-pattern",
},
],
"type": "search",
},
Object {
"attributes": Object {},
"id": "1",
"references": Array [],
"type": "index-pattern",
},
]
`);
Array [
Object {
"attributes": Object {},
"id": "2",
"references": Array [
Object {
"id": "1",
"name": "ref1",
"type": "index-pattern",
},
],
"type": "search",
},
Object {
"attributes": Object {},
"id": "1",
"references": Array [],
"type": "index-pattern",
},
]
`);
});

test('should sort unordered array', () => {
Expand Down Expand Up @@ -199,71 +199,71 @@ Array [
},
];
expect(sortObjects(docs)).toMatchInlineSnapshot(`
Array [
Object {
"attributes": Object {},
"id": "1",
"references": Array [],
"type": "index-pattern",
},
Object {
"attributes": Object {},
"id": "2",
"references": Array [
Object {
"id": "1",
"name": "ref1",
"type": "index-pattern",
},
],
"type": "search",
},
Object {
"attributes": Object {},
"id": "3",
"references": Array [
Object {
"id": "2",
"name": "ref1",
"type": "search",
},
],
"type": "visualization",
},
Object {
"attributes": Object {},
"id": "4",
"references": Array [
Object {
"id": "1",
"name": "ref1",
"type": "index-pattern",
},
],
"type": "visualization",
},
Object {
"attributes": Object {},
"id": "5",
"references": Array [
Object {
"id": "3",
"name": "ref1",
"type": "visualization",
},
Object {
"id": "4",
"name": "ref2",
"type": "visualization",
},
],
"type": "dashboard",
},
]
`);
Array [
Object {
"attributes": Object {},
"id": "1",
"references": Array [],
"type": "index-pattern",
},
Object {
"attributes": Object {},
"id": "2",
"references": Array [
Object {
"id": "1",
"name": "ref1",
"type": "index-pattern",
},
],
"type": "search",
},
Object {
"attributes": Object {},
"id": "3",
"references": Array [
Object {
"id": "2",
"name": "ref1",
"type": "search",
},
],
"type": "visualization",
},
Object {
"attributes": Object {},
"id": "4",
"references": Array [
Object {
"id": "1",
"name": "ref1",
"type": "index-pattern",
},
],
"type": "visualization",
},
Object {
"attributes": Object {},
"id": "5",
"references": Array [
Object {
"id": "3",
"name": "ref1",
"type": "visualization",
},
Object {
"id": "4",
"name": "ref2",
"type": "visualization",
},
],
"type": "dashboard",
},
]
`);
});

test('detects circular dependencies', () => {
test('should not fail on circular dependencies', () => {
const docs = [
{
id: '1',
mshustov marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -290,8 +290,34 @@ Array [
],
},
];
expect(() => sortObjects(docs)).toThrowErrorMatchingInlineSnapshot(
`"circular reference: [foo:1] ref-> [foo:2] ref-> [foo:1]"`
);

expect(sortObjects(docs)).toMatchInlineSnapshot(`
Array [
Object {
"attributes": Object {},
"id": "2",
"references": Array [
Object {
"id": "1",
"name": "ref1",
"type": "foo",
},
],
"type": "foo",
},
Object {
"attributes": Object {},
"id": "1",
"references": Array [
Object {
"id": "2",
"name": "ref1",
"type": "foo",
},
],
"type": "foo",
},
]
`);
});
});
7 changes: 1 addition & 6 deletions src/core/server/saved_objects/export/sort_objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* under the License.
*/

import Boom from 'boom';
import { SavedObject } from '../types';

export function sortObjects(savedObjects: SavedObject[]): SavedObject[] {
Expand All @@ -30,11 +29,7 @@ export function sortObjects(savedObjects: SavedObject[]): SavedObject[] {
function includeObjects(objects: SavedObject[]) {
for (const object of objects) {
if (path.has(object)) {
throw Boom.badRequest(
`circular reference: ${[...path, object]
.map((obj) => `[${obj.type}:${obj.id}]`)
.join(' ref-> ')}`
);
continue;
Comment on lines -33 to +32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, but can we now delete the path.delete(object); L47?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. It acts as a pop operation in 'path stack'

}

const refdObjects = object.references
Expand Down
Loading