Skip to content

Commit 444c1e4

Browse files
committed
preview to query param, snake to camel case in request body
1 parent 15daad2 commit 444c1e4

2 files changed

Lines changed: 44 additions & 43 deletions

File tree

  • src/plugins/data_views/server/rest_api_routes/public
  • test/api_integration/apis/data_views/swap_references

src/plugins/data_views/server/rest_api_routes/public/swap_references.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,22 @@ export const swapReferencesRoute = (
5656
version: INITIAL_REST_VERSION,
5757
validate: {
5858
request: {
59-
body: schema.object({
60-
from_id: idSchema,
61-
from_type: schema.maybe(schema.string()),
62-
to_id: idSchema,
63-
for_id: schema.maybe(schema.oneOf([idSchema, schema.arrayOf(idSchema)])),
64-
for_type: schema.maybe(schema.string()),
59+
params: schema.object({
6560
preview: schema.maybe(schema.boolean()),
61+
}),
62+
body: schema.object({
63+
fromId: idSchema,
64+
fromType: schema.maybe(schema.string()),
65+
toId: idSchema,
66+
forId: schema.maybe(schema.oneOf([idSchema, schema.arrayOf(idSchema)])),
67+
forType: schema.maybe(schema.string()),
6668
delete: schema.maybe(schema.boolean()),
6769
}),
6870
},
6971
response: {
7072
200: {
7173
body: schema.object({
7274
result: schema.arrayOf(schema.object({ id: idSchema, type: schema.string() })),
73-
preview: schema.boolean(),
7475
deleteSuccess: schema.maybe(schema.boolean()),
7576
}),
7677
},
@@ -82,30 +83,30 @@ export const swapReferencesRoute = (
8283
const savedObjectsClient = (await ctx.core).savedObjects.client;
8384
const [core] = await getStartServices();
8485
const types = core.savedObjects.getTypeRegistry().getAllTypes();
85-
const type = req.body.from_type || DATA_VIEW_SAVED_OBJECT_TYPE;
86-
const preview = req.body.preview !== undefined ? req.body.preview : true;
86+
const type = req.body.fromType || DATA_VIEW_SAVED_OBJECT_TYPE;
87+
const preview = req.params.preview !== undefined ? req.params.preview : true;
8788
const searchId =
88-
!Array.isArray(req.body.for_id) && req.body.for_id !== undefined
89-
? [req.body.for_id]
90-
: req.body.for_id;
89+
!Array.isArray(req.body.forId) && req.body.forId !== undefined
90+
? [req.body.forId]
91+
: req.body.forId;
9192

9293
usageCollection?.incrementCounter({ counterName: 'swap_references' });
9394

9495
// verify 'to' object actually exists
9596
try {
96-
await savedObjectsClient.get(type, req.body.to_id);
97+
await savedObjectsClient.get(type, req.body.toId);
9798
} catch (e) {
98-
throw new Error(`Could not find object with type ${type} and id ${req.body.to_id}`);
99+
throw new Error(`Could not find object with type ${type} and id ${req.body.toId}`);
99100
}
100101

101102
// assemble search params
102103
const findParams: SavedObjectsFindOptions = {
103104
type: types.map((t) => t.name),
104-
hasReference: { type, id: req.body.from_id },
105+
hasReference: { type, id: req.body.fromId },
105106
};
106107

107-
if (req.body.for_type) {
108-
findParams.type = [req.body.for_type];
108+
if (req.body.forType) {
109+
findParams.type = [req.body.forType];
109110
}
110111

111112
const { saved_objects: savedObjects } = await savedObjectsClient.find(findParams);
@@ -138,8 +139,8 @@ export const swapReferencesRoute = (
138139
// iterate over list and update references
139140
for (const savedObject of filteredSavedObjects) {
140141
const updatedRefs = savedObject.references.map((ref) => {
141-
if (ref.type === type && ref.id === req.body.from_id) {
142-
return { ...ref, id: req.body.to_id };
142+
if (ref.type === type && ref.id === req.body.fromId) {
143+
return { ...ref, id: req.body.toId };
143144
} else {
144145
return ref;
145146
}
@@ -160,7 +161,7 @@ export const swapReferencesRoute = (
160161
if (verifyNoMoreRefs.total > 0) {
161162
body.deleteSuccess = false;
162163
} else {
163-
await savedObjectsClient.delete(type, req.body.from_id);
164+
await savedObjectsClient.delete(type, req.body.fromId);
164165
body.deleteSuccess = true;
165166
}
166167
}

test/api_integration/apis/data_views/swap_references/main.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ export default function ({ getService }: FtrProviderContext) {
5252
.post(DATA_VIEW_SWAP_REFERENCES_PATH)
5353
.set(ELASTIC_HTTP_VERSION_HEADER, INITIAL_REST_VERSION)
5454
.send({
55-
from_id: prevDataViewId,
56-
to_id: dataViewId,
55+
fromId: prevDataViewId,
56+
toId: dataViewId,
5757
});
5858
expect(res).to.have.property('status', 200);
5959
});
@@ -63,21 +63,21 @@ export default function ({ getService }: FtrProviderContext) {
6363
.post(DATA_VIEW_SWAP_REFERENCES_PATH)
6464
.set(ELASTIC_HTTP_VERSION_HEADER, INITIAL_REST_VERSION)
6565
.send({
66-
from_id: prevDataViewId,
67-
from_type: 'index-pattern',
68-
to_id: dataViewId,
66+
fromId: prevDataViewId,
67+
fromType: 'index-pattern',
68+
toId: dataViewId,
6969
});
7070
expect(res).to.have.property('status', 200);
7171
});
7272

7373
it('can save changes', async () => {
7474
const res = await supertest
7575
.post(DATA_VIEW_SWAP_REFERENCES_PATH)
76+
.query({ preview: false })
7677
.set(ELASTIC_HTTP_VERSION_HEADER, INITIAL_REST_VERSION)
7778
.send({
78-
from_id: prevDataViewId,
79-
to_id: dataViewId,
80-
preview: false,
79+
fromId: prevDataViewId,
80+
toId: dataViewId,
8181
});
8282
expect(res).to.have.property('status', 200);
8383
expect(res.body.result.length).to.equal(1);
@@ -89,11 +89,11 @@ export default function ({ getService }: FtrProviderContext) {
8989
it('can save changes and remove old saved object', async () => {
9090
const res = await supertest
9191
.post(DATA_VIEW_SWAP_REFERENCES_PATH)
92+
.query({ preview: false })
9293
.set(ELASTIC_HTTP_VERSION_HEADER, INITIAL_REST_VERSION)
9394
.send({
94-
from_id: prevDataViewId,
95-
to_id: dataViewId,
96-
preview: false,
95+
fromId: prevDataViewId,
96+
toId: dataViewId,
9797
delete: true,
9898
});
9999
expect(res).to.have.property('status', 200);
@@ -123,8 +123,8 @@ export default function ({ getService }: FtrProviderContext) {
123123
const res = await supertest
124124
.post(DATA_VIEW_SWAP_REFERENCES_PATH)
125125
.send({
126-
from_id: '8963ca30-3224-11e8-a572-ffca06da1357',
127-
to_id: '91200a00-9efd-11e7-acb3-3dab96693fab',
126+
fromId: '8963ca30-3224-11e8-a572-ffca06da1357',
127+
toId: '91200a00-9efd-11e7-acb3-3dab96693fab',
128128
})
129129
.set(ELASTIC_HTTP_VERSION_HEADER, INITIAL_REST_VERSION);
130130
expect(res).to.have.property('status', 200);
@@ -133,11 +133,11 @@ export default function ({ getService }: FtrProviderContext) {
133133
// limit to one item
134134
const res2 = await supertest
135135
.post(DATA_VIEW_SWAP_REFERENCES_PATH)
136+
.query({ preview: false })
136137
.send({
137-
from_id: '8963ca30-3224-11e8-a572-ffca06da1357',
138-
to_id: '91200a00-9efd-11e7-acb3-3dab96693fab',
139-
for_id: ['960372e0-3224-11e8-a572-ffca06da1357'],
140-
preview: false,
138+
fromId: '8963ca30-3224-11e8-a572-ffca06da1357',
139+
toId: '91200a00-9efd-11e7-acb3-3dab96693fab',
140+
forId: ['960372e0-3224-11e8-a572-ffca06da1357'],
141141
})
142142
.set(ELASTIC_HTTP_VERSION_HEADER, INITIAL_REST_VERSION);
143143
expect(res2).to.have.property('status', 200);
@@ -149,8 +149,8 @@ export default function ({ getService }: FtrProviderContext) {
149149
const res = await supertest
150150
.post(DATA_VIEW_SWAP_REFERENCES_PATH)
151151
.send({
152-
from_id: '8963ca30-3224-11e8-a572-ffca06da1357',
153-
to_id: '91200a00-9efd-11e7-acb3-3dab96693fab',
152+
fromId: '8963ca30-3224-11e8-a572-ffca06da1357',
153+
toId: '91200a00-9efd-11e7-acb3-3dab96693fab',
154154
})
155155
.set(ELASTIC_HTTP_VERSION_HEADER, INITIAL_REST_VERSION);
156156
expect(res).to.have.property('status', 200);
@@ -159,11 +159,11 @@ export default function ({ getService }: FtrProviderContext) {
159159
// limit to one item
160160
const res2 = await supertest
161161
.post(DATA_VIEW_SWAP_REFERENCES_PATH)
162+
.query({ preview: false })
162163
.send({
163-
from_id: '8963ca30-3224-11e8-a572-ffca06da1357',
164-
to_id: '91200a00-9efd-11e7-acb3-3dab96693fab',
165-
for_type: 'search',
166-
preview: false,
164+
fromId: '8963ca30-3224-11e8-a572-ffca06da1357',
165+
toId: '91200a00-9efd-11e7-acb3-3dab96693fab',
166+
forType: 'search',
167167
})
168168
.set(ELASTIC_HTTP_VERSION_HEADER, INITIAL_REST_VERSION);
169169
expect(res2).to.have.property('status', 200);

0 commit comments

Comments
 (0)