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,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)}`,
},
},
});
}

View File

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

View File

@ -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);