From 5802a65a2d0f0b72abd8646fba7f48c03976c399 Mon Sep 17 00:00:00 2001 From: Alexandre Bodin Date: Mon, 18 Sep 2023 09:43:24 +0200 Subject: [PATCH] Fix some bugs introduced by the ts migration --- .../src/core-api/controller/transform.ts | 9 ++------ .../src/services/entity-service/index.ts | 21 +++++++------------ 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/packages/core/strapi/src/core-api/controller/transform.ts b/packages/core/strapi/src/core-api/controller/transform.ts index 709717b963..a3c67695fe 100644 --- a/packages/core/strapi/src/core-api/controller/transform.ts +++ b/packages/core/strapi/src/core-api/controller/transform.ts @@ -20,7 +20,7 @@ type Entry = { }; function isEntry(property: unknown): property is Entry | Entry[] { - return isPlainObject(property) || Array.isArray(property); + return property === null || isPlainObject(property) || Array.isArray(property); } function isDZEntries(property: unknown): property is (Entry & { __component: UID.Component })[] { @@ -102,12 +102,7 @@ function transformEntry( const property = properties[key]; const attribute = type && type.attributes[key]; - if ( - attribute && - contentTypeUtils.isRelationalAttribute(attribute) && - isEntry(property) && - 'target' in attribute - ) { + if (attribute && attribute.type === 'relation' && isEntry(property) && 'target' in attribute) { const data = transformEntry( property, strapi.contentType(attribute.target as Common.UID.ContentType) diff --git a/packages/core/strapi/src/services/entity-service/index.ts b/packages/core/strapi/src/services/entity-service/index.ts index 727372594f..1ec879efa7 100644 --- a/packages/core/strapi/src/services/entity-service/index.ts +++ b/packages/core/strapi/src/services/entity-service/index.ts @@ -54,12 +54,14 @@ const transformLoadParamsToQuery = ( params: Record, pagination = {} ) => { - const transformedParams = transformParamsToQuery(uid, params); + const query = transformParamsToQuery(uid, { populate: { [field]: params } as any }) as any; - return { - ...(_.get(transformedParams, ['populate', field]) ?? {}), + const res = { + ...query.populate[field], ...pagination, }; + + return res; }; const databaseErrorsToTransform = [ @@ -417,11 +419,7 @@ const createDefaultImplementation = ({ const loadedEntity = await db .query(uid) - .load( - entity, - field, - transformLoadParamsToQuery(uid, field, params as Record) - ); + .load(entity, field, transformLoadParamsToQuery(uid, field, params ?? {})); return this.wrapResult(loadedEntity, { uid, field, action: 'load' }); }, @@ -438,12 +436,7 @@ const createDefaultImplementation = ({ throw new Error(`Invalid load. Expected "${field}" to be an anyToMany relational attribute`); } - const query = transformLoadParamsToQuery( - uid, - field, - params as Record, - pagination - ); + const query = transformLoadParamsToQuery(uid, field, params ?? {}, pagination); const loadedPage: any = await db.query(uid).loadPages(entity, field, query);