Merge pull request #14334 from strapi/relations-main-view/remove-populate-from-responses

Change populate params for all CM actions responses
This commit is contained in:
Gustav Hansen 2022-09-07 16:44:20 +02:00 committed by GitHub
commit f03d058cfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 137 additions and 122 deletions

View File

@ -171,7 +171,10 @@ module.exports = ({ strapi }) => ({
publishData[PUBLISHED_AT_ATTRIBUTE] = null;
}
const params = { data: publishData, populate: getDeepPopulate(uid) };
const params = {
data: publishData,
populate: getDeepPopulate(uid, null, { onlyMany: true, countMany: true }),
};
return strapi.entityService.create(uid, params);
},
@ -179,13 +182,16 @@ module.exports = ({ strapi }) => ({
update(entity, body, uid) {
const publishData = omitPublishedAtField(body);
const params = { data: publishData, populate: getDeepPopulate(uid) };
const params = {
data: publishData,
populate: getDeepPopulate(uid, null, { onlyMany: true, countMany: true }),
};
return strapi.entityService.update(uid, entity.id, params);
},
delete(entity, uid) {
const params = { populate: getDeepPopulate(uid) };
const params = { populate: getDeepPopulate(uid, null, { onlyMany: true, countMany: true }) };
return strapi.entityService.delete(uid, entity.id, params);
},
@ -212,7 +218,10 @@ module.exports = ({ strapi }) => ({
const data = { ...body, [PUBLISHED_AT_ATTRIBUTE]: new Date() };
const params = { data, populate: getDeepPopulate(uid) };
const params = {
data,
populate: getDeepPopulate(uid, null, { onlyMany: true, countMany: true }),
};
return strapi.entityService.update(uid, entity.id, params);
}),
@ -224,7 +233,10 @@ module.exports = ({ strapi }) => ({
const data = { ...body, [PUBLISHED_AT_ATTRIBUTE]: null };
const params = { data, populate: getDeepPopulate(uid) };
const params = {
data,
populate: getDeepPopulate(uid, null, { onlyMany: true, countMany: true }),
};
return strapi.entityService.update(uid, entity.id, params);
}),

View File

@ -157,9 +157,6 @@ describe('CM API - Populate dz', () => {
__component: 'default.compo-a',
items: {
title: 'AAAA',
category: {
name: 'name',
},
},
},
{
@ -167,9 +164,6 @@ describe('CM API - Populate dz', () => {
items: {
name: 'BBBB',
category: 'smthg',
category_diff: {
name: 'name',
},
},
},
],

View File

@ -1,6 +1,7 @@
'use strict';
// Helpers.
const { isEmpty } = require('lodash/fp');
const { createTestBuilder } = require('../../../../../test/helpers/builder');
const { createStrapiInstance } = require('../../../../../test/helpers/strapi');
const form = require('../../../../../test/helpers/generators');
@ -16,6 +17,14 @@ let strapi;
let data;
let rq;
const getRelations = async (modelName, field, id) => {
const res = await rq({
method: 'GET',
url: `/content-manager/collection-types/api::${modelName}.${modelName}/${id}/${field}`,
});
return res.body.results || res.body;
};
const deleteFixtures = async () => {
for (const [name, modelName] of [
['references', 'reference'],
@ -80,7 +89,6 @@ describe('Relations', () => {
data.tags.push(body);
expect(body.id).toBeDefined();
expect(Array.isArray(body.articles)).toBeTruthy();
expect(body.name).toBe('tag1');
expect(body.createdBy).toMatchObject({
firstname: 'admin',
@ -109,7 +117,6 @@ describe('Relations', () => {
data.tags.push(body);
expect(body.id).toBeDefined();
expect(Array.isArray(body.articles)).toBeTruthy();
expect(body.name).toBe('tag2');
expect(body.createdBy).toMatchObject({
firstname: 'admin',
@ -138,7 +145,6 @@ describe('Relations', () => {
data.tags.push(body);
expect(body.id).toBeDefined();
expect(Array.isArray(body.articles)).toBeTruthy();
expect(body.name).toBe('tag3');
expect(body.createdBy).toMatchObject({
firstname: 'admin',
@ -173,8 +179,6 @@ describe('Relations', () => {
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.tags.length).toBe(0);
expect(body.createdBy).toMatchObject({
firstname: 'admin',
id: 1,
@ -188,6 +192,9 @@ describe('Relations', () => {
username: null,
});
expect(body.publishedAt).toBeUndefined();
const tags = await getRelations('article', 'tags', body.id);
expect(tags.length).toBe(0);
});
test('Create article2 with tag1', async () => {
@ -208,9 +215,6 @@ describe('Relations', () => {
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.tags.length).toBe(1);
expect(body.tags[0].id).toBe(data.tags[0].id);
expect(body.createdBy).toMatchObject({
firstname: 'admin',
id: 1,
@ -224,6 +228,10 @@ describe('Relations', () => {
username: null,
});
expect(body.publishedAt).toBeUndefined();
const tags = await getRelations('article', 'tags', body.id);
expect(tags.length).toBe(1);
expect(tags[0].id).toBe(data.tags[0].id);
});
test('Update article1 add tag2', async () => {
@ -242,9 +250,6 @@ describe('Relations', () => {
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.tags.length).toBe(1);
expect(body.tags[0].id).toBe(data.tags[1].id);
expect(body.createdBy).toMatchObject({
firstname: 'admin',
id: 1,
@ -258,30 +263,24 @@ describe('Relations', () => {
username: null,
});
expect(body.publishedAt).toBeUndefined();
const tags = await getRelations('article', 'tags', body.id);
expect(tags.length).toBe(1);
expect(tags[0].id).toBe(data.tags[1].id);
});
test('Update article1 add tag1 and tag3', async () => {
const entry = { ...data.articles[0] };
entry.tags = entry.tags.map((tag) => tag.id);
entry.tags.push(data.tags[0].id);
entry.tags.push(data.tags[2].id);
cleanDate(entry);
const { body } = await rq({
url: `/content-manager/collection-types/api::article.article/${entry.id}`,
url: `/content-manager/collection-types/api::article.article/${data.articles[0].id}`,
method: 'PUT',
body: entry,
body: {
tags: [data.tags[0].id, data.tags[1].id, data.tags[2].id],
},
});
data.articles[0] = body;
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.tags.length).toBe(3);
expect(body.title).toBe(data.articles[0].title);
expect(body.content).toBe(data.articles[0].content);
expect(body.createdBy).toMatchObject({
firstname: 'admin',
id: 1,
@ -295,27 +294,23 @@ describe('Relations', () => {
username: null,
});
expect(body.publishedAt).toBeUndefined();
const tags = await getRelations('article', 'tags', body.id);
expect(tags.length).toBe(3);
});
test('Update article1 remove one tag', async () => {
const entry = { ...data.articles[0] };
entry.tags = entry.tags.slice(1).map((tag) => tag.id);
cleanDate(entry);
const { body } = await rq({
url: `/content-manager/collection-types/api::article.article/${entry.id}`,
url: `/content-manager/collection-types/api::article.article/${data.articles[0].id}`,
method: 'PUT',
body: entry,
body: {
tags: [data.tags[1].id, data.tags[2].id],
},
});
data.articles[0] = body;
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.tags.length).toBe(2);
expect(body.title).toBe(data.articles[0].title);
expect(body.content).toBe(data.articles[0].content);
expect(body.createdBy).toMatchObject({
firstname: 'admin',
id: 1,
@ -329,6 +324,9 @@ describe('Relations', () => {
username: null,
});
expect(body.publishedAt).toBeUndefined();
const tags = await getRelations('article', 'tags', body.id);
expect(tags.length).toBe(2);
});
test('Update article1 remove all tag', async () => {
@ -347,8 +345,6 @@ describe('Relations', () => {
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.tags.length).toBe(0);
expect(body.createdBy).toMatchObject({
firstname: 'admin',
id: 1,
@ -362,6 +358,9 @@ describe('Relations', () => {
username: null,
});
expect(body.publishedAt).toBeUndefined();
const tags = await getRelations('article', 'tags', body.id);
expect(tags.length).toBe(0);
});
test('Delete all articles should remove the association in each tags related to them', async () => {
@ -398,13 +397,6 @@ describe('Relations', () => {
},
});
const articles = [article12, article13];
expect(Array.isArray(articles[0].tags)).toBeTruthy();
expect(articles[0].tags.length).toBe(1);
expect(Array.isArray(articles[1].tags)).toBeTruthy();
expect(articles[1].tags.length).toBe(1);
const { body: foundTag } = await rq({
url: `/content-manager/collection-types/api::tag.tag/${createdTag.id}`,
method: 'GET',
@ -416,7 +408,7 @@ describe('Relations', () => {
url: '/content-manager/collection-types/api::article.article/actions/bulkDelete',
method: 'POST',
body: {
ids: articles.map((article) => article.id),
ids: [article12.id, article13.id],
},
});
@ -465,9 +457,6 @@ describe('Relations', () => {
data.articlesWithTag.push(body);
expect(body.id).toBeDefined();
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.tags.length).toBe(1);
expect(body.tags[0].id).toBe(data.tags[0].id);
expect(body.createdBy).toMatchObject({
firstname: 'admin',
id: 1,
@ -480,6 +469,10 @@ describe('Relations', () => {
lastname: 'admin',
username: null,
});
const tags = await getRelations('articlewithtag', 'tags', body.id);
expect(tags.length).toBe(1);
expect(tags[0].id).toBe(data.tags[0].id);
});
});
@ -507,7 +500,6 @@ describe('Relations', () => {
data.categories.push(body);
expect(body.id).toBeDefined();
expect(Array.isArray(body.articles)).toBeTruthy();
expect(body.name).toBe('cat1');
expect(body.createdBy).toMatchObject({
firstname: 'admin',
@ -522,6 +514,9 @@ describe('Relations', () => {
username: null,
});
expect(body.publishedAt).toBeUndefined();
const articles = await getRelations('category', 'articles', body.id);
expect(articles.length).toBe(0);
});
test('Create cat2', async () => {
@ -536,7 +531,6 @@ describe('Relations', () => {
data.categories.push(body);
expect(body.id).toBeDefined();
expect(Array.isArray(body.articles)).toBeTruthy();
expect(body.name).toBe('cat2');
expect(body.createdBy).toMatchObject({
firstname: 'admin',
@ -551,6 +545,8 @@ describe('Relations', () => {
username: null,
});
expect(body.publishedAt).toBeUndefined();
const articles = await getRelations('category', 'articles', body.id);
expect(articles.length).toBe(0);
});
test('Create article1 with cat1', async () => {
@ -571,8 +567,6 @@ describe('Relations', () => {
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
expect(body.category.name).toBe(data.categories[0].name);
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.createdBy).toMatchObject({
firstname: 'admin',
id: 1,
@ -585,26 +579,29 @@ describe('Relations', () => {
lastname: 'admin',
username: null,
});
expect(body.publishedAt).toBeUndefined();
const tags = await getRelations('article', 'tags', body.id);
expect(tags.length).toBe(0);
const category = await getRelations('article', 'category', body.id);
expect(category.name).toBe(data.categories[0].name);
});
test('Update article1 with cat2', async () => {
const entry = { ...data.articles[0], category: data.categories[1].id };
cleanDate(entry);
const { body } = await rq({
url: `/content-manager/collection-types/api::article.article/${entry.id}`,
url: `/content-manager/collection-types/api::article.article/${data.articles[0].id}`,
method: 'PUT',
body: entry,
body: {
category: data.categories[1].id,
},
});
data.articles[0] = body;
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
expect(body.category.name).toBe(data.categories[1].name);
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.title).toBe(data.articles[0].title);
expect(body.content).toBe(data.articles[0].content);
expect(body.createdBy).toMatchObject({
firstname: 'admin',
id: 1,
@ -617,6 +614,12 @@ describe('Relations', () => {
lastname: 'admin',
username: null,
});
const tags = await getRelations('article', 'tags', body.id);
expect(tags.length).toBe(0);
const category = await getRelations('article', 'category', body.id);
expect(category.name).toBe(data.categories[1].name);
});
test('Create article2', async () => {
@ -636,7 +639,6 @@ describe('Relations', () => {
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.createdBy).toMatchObject({
firstname: 'admin',
id: 1,
@ -649,26 +651,25 @@ describe('Relations', () => {
lastname: 'admin',
username: null,
});
const tags = await getRelations('article', 'tags', body.id);
expect(tags.length).toBe(0);
});
test('Update article2 with cat2', async () => {
const entry = { ...data.articles[1], category: data.categories[1].id };
cleanDate(entry);
const { body } = await rq({
url: `/content-manager/collection-types/api::article.article/${entry.id}`,
url: `/content-manager/collection-types/api::article.article/${data.articles[1].id}`,
method: 'PUT',
body: entry,
body: {
category: data.categories[1].id,
},
});
data.articles[1] = body;
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
expect(body.category.name).toBe(data.categories[1].name);
expect(Array.isArray(body.tags)).toBeTruthy();
expect(body.title).toBe(data.articles[1].title);
expect(body.content).toBe(data.articles[1].content);
expect(body.createdBy).toMatchObject({
firstname: 'admin',
id: 1,
@ -681,27 +682,27 @@ describe('Relations', () => {
lastname: 'admin',
username: null,
});
const tags = await getRelations('article', 'tags', body.id);
expect(tags.length).toBe(0);
const category = await getRelations('article', 'category', body.id);
expect(category.name).toBe(data.categories[1].name);
});
test('Update cat1 with article1', async () => {
const entry = { ...data.categories[0] };
entry.articles = entry.articles.map((article) => article.id);
entry.articles.push(data.articles[0].id);
cleanDate(entry);
const { body } = await rq({
url: `/content-manager/collection-types/api::category.category/${entry.id}`,
url: `/content-manager/collection-types/api::category.category/${data.categories[0].id}`,
method: 'PUT',
body: entry,
body: {
articles: [data.articles[0].id],
},
});
data.categories[0] = body;
expect(body.id).toBeDefined();
expect(Array.isArray(body.articles)).toBeTruthy();
expect(body.articles.length).toBe(1);
expect(body.name).toBe(entry.name);
expect(body.name).toBe(data.categories[0].name);
expect(body.createdBy).toMatchObject({
firstname: 'admin',
id: 1,
@ -714,6 +715,9 @@ describe('Relations', () => {
lastname: 'admin',
username: null,
});
const articles = await getRelations('category', 'articles', body.id);
expect(articles.length).toBe(1);
});
test('Create cat3 with article1', async () => {
@ -731,8 +735,6 @@ describe('Relations', () => {
data.categories.push(body);
expect(body.id).toBeDefined();
expect(Array.isArray(body.articles)).toBeTruthy();
expect(body.articles.length).toBe(1);
expect(body.name).toBe(entry.name);
expect(body.createdBy).toMatchObject({
firstname: 'admin',
@ -746,10 +748,12 @@ describe('Relations', () => {
lastname: 'admin',
username: null,
});
const articles = await getRelations('category', 'articles', body.id);
expect(articles.length).toBe(1);
});
// TODO RELATIONS: reimplement following tests
test.skip('Get article1 with cat3', async () => {
test('Get article1 with cat3', async () => {
const { body } = await rq({
url: `/content-manager/collection-types/api::article.article/${data.articles[0].id}/category`,
method: 'GET',
@ -760,7 +764,7 @@ describe('Relations', () => {
});
});
test.skip('Get article2 with cat2', async () => {
test('Get article2 with cat2', async () => {
const { body } = await rq({
url: `/content-manager/collection-types/api::article.article/${data.articles[1].id}/category`,
method: 'GET',
@ -771,7 +775,7 @@ describe('Relations', () => {
});
});
test.skip('Get cat1 without relations', async () => {
test('Get cat1 without relations', async () => {
const { body } = await rq({
url: `/content-manager/collection-types/api::category.category/${data.categories[0].id}/articles`,
method: 'GET',
@ -788,7 +792,7 @@ describe('Relations', () => {
});
});
test.skip('Get cat2 with article2', async () => {
test('Get cat2 with article2', async () => {
const { body } = await rq({
url: `/content-manager/collection-types/api::category.category/${data.categories[1].id}/articles`,
method: 'GET',
@ -800,7 +804,7 @@ describe('Relations', () => {
});
});
test.skip('Get cat3 with article1', async () => {
test('Get cat3 with article1', async () => {
const { body } = await rq({
url: `/content-manager/collection-types/api::category.category/${data.categories[2].id}/articles`,
method: 'GET',
@ -885,22 +889,19 @@ describe('Relations', () => {
});
test('Update article1 with ref1', async () => {
const entry = { ...data.articles[0], reference: data.references[0].id };
cleanDate(entry);
const { body } = await rq({
url: `/content-manager/collection-types/api::article.article/${entry.id}`,
url: `/content-manager/collection-types/api::article.article/${data.articles[0].id}`,
method: 'PUT',
body: entry,
body: {
reference: data.references[0].id,
},
});
data.articles[0] = body;
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
expect(body.reference.id).toBe(entry.reference);
expect(body.title).toBe(data.articles[0].title);
expect(body.content).toBe(data.articles[0].content);
expect(body.createdBy).toMatchObject({
firstname: 'admin',
id: 1,
@ -913,6 +914,9 @@ describe('Relations', () => {
lastname: 'admin',
username: null,
});
const reference = await getRelations('article', 'reference', body.id);
expect(reference.id).toBe(data.references[0].id);
});
test('Create article2 with ref1', async () => {
@ -933,7 +937,6 @@ describe('Relations', () => {
expect(body.id).toBeDefined();
expect(body.title).toBe(entry.title);
expect(body.content).toBe(entry.content);
expect(body.reference.id).toBe(entry.reference);
expect(body.createdBy).toMatchObject({
firstname: 'admin',
id: 1,
@ -946,6 +949,8 @@ describe('Relations', () => {
lastname: 'admin',
username: null,
});
const reference = await getRelations('article', 'reference', body.id);
expect(reference.id).toBe(data.references[0].id);
});
});
@ -969,7 +974,9 @@ describe('Relations', () => {
});
expect(createdReference.id).toBeDefined();
expect(createdReference.tag.id).toBe(createdTag.id);
const tag = await getRelations('reference', 'tag', createdReference.id);
expect(tag.id).toBe(createdTag.id);
});
test('Detach Tag to a Reference', async () => {
@ -990,7 +997,8 @@ describe('Relations', () => {
},
});
expect(createdReference.tag.id).toBe(createdTag.id);
let tag = await getRelations('reference', 'tag', createdReference.id);
expect(tag.id).toBe(createdTag.id);
const { body: referenceToUpdate } = await rq({
url: `/content-manager/collection-types/api::reference.reference/${createdReference.id}`,
@ -1000,7 +1008,8 @@ describe('Relations', () => {
},
});
expect(referenceToUpdate.tag).toBe(null);
tag = await getRelations('reference', 'tag', referenceToUpdate.id);
expect(isEmpty(tag)).toBe(true);
});
test('Delete Tag so the relation in the Reference side should be removed', async () => {