From 880c3c53a8889cec28b0c713b8ca05dd29e9be59 Mon Sep 17 00:00:00 2001 From: Convly Date: Mon, 15 Nov 2021 15:34:23 +0100 Subject: [PATCH] It handles the scenarios where body.data is not correctly defined (as an object) --- .../core/strapi/lib/core-api/service/collection-type.js | 7 ++++++- packages/core/strapi/lib/core-api/service/single-type.js | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/core/strapi/lib/core-api/service/collection-type.js b/packages/core/strapi/lib/core-api/service/collection-type.js index e2d478d56f..827b28ea8b 100644 --- a/packages/core/strapi/lib/core-api/service/collection-type.js +++ b/packages/core/strapi/lib/core-api/service/collection-type.js @@ -1,11 +1,12 @@ 'use strict'; -const { propOr } = require('lodash/fp'); +const { propOr, isObject } = require('lodash/fp'); const { hasDraftAndPublish, constants: { PUBLISHED_AT_ATTRIBUTE }, } = require('@strapi/utils').contentTypes; +const { ValidationError } = require('@strapi/utils').errors; const { getPaginationInfo, @@ -60,6 +61,10 @@ const createCollectionTypeService = ({ model, strapi, utils }) => { create(params = {}) { const { data } = params; + if (!isObject(data)) { + throw new ValidationError(`Expecting body.data to be an object but found '${typeof data}'`); + } + if (hasDraftAndPublish(model)) { setPublishedAt(data); } diff --git a/packages/core/strapi/lib/core-api/service/single-type.js b/packages/core/strapi/lib/core-api/service/single-type.js index 92e44ffe81..0bfbcccea2 100644 --- a/packages/core/strapi/lib/core-api/service/single-type.js +++ b/packages/core/strapi/lib/core-api/service/single-type.js @@ -1,5 +1,6 @@ 'use strict'; +const { isObject } = require('lodash/fp'); const { ValidationError } = require('@strapi/utils').errors; /** @@ -27,6 +28,10 @@ const createSingleTypeService = ({ model, strapi, utils }) => { async createOrUpdate({ data, ...params } = {}) { const entity = await this.find(params); + if (!isObject(data)) { + throw new ValidationError(`Expecting body.data to be an object but found '${typeof data}'`); + } + if (!entity) { const count = await strapi.query(uid).count(); if (count >= 1) {