From c1369c796034fb1b57471498eb1e0c75ce2d7715 Mon Sep 17 00:00:00 2001 From: Alexandre Bodin Date: Fri, 24 Sep 2021 18:52:49 +0200 Subject: [PATCH] Fix sanitizeEntity --- .../strapi/lib/middlewares/router/index.js | 8 ++++--- .../lib/__tests__/sanitize-entity.test.js | 4 +++- packages/core/utils/lib/sanitize-entity.js | 23 ++++++++----------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/packages/core/strapi/lib/middlewares/router/index.js b/packages/core/strapi/lib/middlewares/router/index.js index 837276608a..a97684c182 100644 --- a/packages/core/strapi/lib/middlewares/router/index.js +++ b/packages/core/strapi/lib/middlewares/router/index.js @@ -9,9 +9,11 @@ const createRouteScopeGenerator = namespace => route => { if (typeof route.handler === 'string') { const [controller, action] = route.handler.split('.'); - _.defaultsDeep(route.config, { - auth: { - scope: `${prefix}${controller}.${toLower(action)}`, + _.defaultsDeep(route, { + config: { + auth: { + scope: `${prefix}${controller}.${toLower(action)}`, + }, }, }); } diff --git a/packages/core/utils/lib/__tests__/sanitize-entity.test.js b/packages/core/utils/lib/__tests__/sanitize-entity.test.js index cb8fa37053..c13ddd611b 100644 --- a/packages/core/utils/lib/__tests__/sanitize-entity.test.js +++ b/packages/core/utils/lib/__tests__/sanitize-entity.test.js @@ -60,7 +60,9 @@ describe('Sanitize Entity', () => { attributes: { ...userModel.attributes, article: { - collection: 'article', + type: 'relation', + relation: 'oneToOne', + target: 'article', }, }, }; diff --git a/packages/core/utils/lib/sanitize-entity.js b/packages/core/utils/lib/sanitize-entity.js index f046553fe0..ba8f6b26c1 100644 --- a/packages/core/utils/lib/sanitize-entity.js +++ b/packages/core/utils/lib/sanitize-entity.js @@ -48,8 +48,10 @@ const sanitizeEntity = (dataSource, options) => { } // Relations - const relation = attribute && (attribute.model || attribute.collection || attribute.component); - if (relation) { + const isRelation = attribute && attribute.type === 'relation'; + if (isRelation) { + const relation = attribute && attribute.target; + if (_.isNil(value)) { return { ...acc, [key]: value }; } @@ -69,23 +71,18 @@ const sanitizeEntity = (dataSource, options) => { }; let sanitizeFn; - if (relation === '*') { + if (attribute.relation && attribute.relation.toLowerCase().includes('morph')) { sanitizeFn = entity => { - if (_.isNil(entity) || !_.has(entity, '__contentType')) { + if (_.isNil(entity) || !_.has(entity, '__type')) { return entity; } - return sanitizeEntity(entity, { - model: strapi.getModel(entity.__contentType), - ...baseOptions, - }); + return sanitizeEntity(entity, { model: strapi.getModel(entity.__type), ...baseOptions }); }; } else { - sanitizeFn = entity => - sanitizeEntity(entity, { - model: strapi.getModel(relation, attribute.plugin), - ...baseOptions, - }); + sanitizeFn = entity => { + return sanitizeEntity(entity, { model: strapi.getModel(relation), ...baseOptions }); + }; } const nextVal = Array.isArray(value) ? value.map(sanitizeFn) : sanitizeFn(value);