Fix sanitizeEntity

This commit is contained in:
Alexandre Bodin 2021-09-24 18:52:49 +02:00
parent 87d79388c1
commit c1369c7960
3 changed files with 18 additions and 17 deletions

View File

@ -9,10 +9,12 @@ const createRouteScopeGenerator = namespace => route => {
if (typeof route.handler === 'string') { if (typeof route.handler === 'string') {
const [controller, action] = route.handler.split('.'); const [controller, action] = route.handler.split('.');
_.defaultsDeep(route.config, { _.defaultsDeep(route, {
config: {
auth: { auth: {
scope: `${prefix}${controller}.${toLower(action)}`, scope: `${prefix}${controller}.${toLower(action)}`,
}, },
},
}); });
} }
}; };

View File

@ -60,7 +60,9 @@ describe('Sanitize Entity', () => {
attributes: { attributes: {
...userModel.attributes, ...userModel.attributes,
article: { article: {
collection: 'article', type: 'relation',
relation: 'oneToOne',
target: 'article',
}, },
}, },
}; };

View File

@ -48,8 +48,10 @@ const sanitizeEntity = (dataSource, options) => {
} }
// Relations // Relations
const relation = attribute && (attribute.model || attribute.collection || attribute.component); const isRelation = attribute && attribute.type === 'relation';
if (relation) { if (isRelation) {
const relation = attribute && attribute.target;
if (_.isNil(value)) { if (_.isNil(value)) {
return { ...acc, [key]: value }; return { ...acc, [key]: value };
} }
@ -69,23 +71,18 @@ const sanitizeEntity = (dataSource, options) => {
}; };
let sanitizeFn; let sanitizeFn;
if (relation === '*') { if (attribute.relation && attribute.relation.toLowerCase().includes('morph')) {
sanitizeFn = entity => { sanitizeFn = entity => {
if (_.isNil(entity) || !_.has(entity, '__contentType')) { if (_.isNil(entity) || !_.has(entity, '__type')) {
return entity; return entity;
} }
return sanitizeEntity(entity, { return sanitizeEntity(entity, { model: strapi.getModel(entity.__type), ...baseOptions });
model: strapi.getModel(entity.__contentType),
...baseOptions,
});
}; };
} else { } else {
sanitizeFn = entity => sanitizeFn = entity => {
sanitizeEntity(entity, { return sanitizeEntity(entity, { model: strapi.getModel(relation), ...baseOptions });
model: strapi.getModel(relation, attribute.plugin), };
...baseOptions,
});
} }
const nextVal = Array.isArray(value) ? value.map(sanitizeFn) : sanitizeFn(value); const nextVal = Array.isArray(value) ? value.map(sanitizeFn) : sanitizeFn(value);