diff --git a/packages/core/content-type-builder/server/utils/__tests__/attributes.test.js b/packages/core/content-type-builder/server/utils/__tests__/attributes.test.js deleted file mode 100644 index 66525d33ad..0000000000 --- a/packages/core/content-type-builder/server/utils/__tests__/attributes.test.js +++ /dev/null @@ -1,34 +0,0 @@ -'use strict'; - -const { formatAttribute } = require('../attributes'); - -describe('format attributes', () => { - it('replaces type customField with the underlying data type', () => { - const mockAttribute = { - type: 'customField', - customField: 'plugin::mycustomfields.color', - }; - - global.strapi = { - container: { - // mock container.get('custom-fields') - get: jest.fn(() => ({ - // mock container.get('custom-fields').get(uid) - get: jest.fn(() => ({ - name: 'color', - plugin: 'mycustomfields', - type: 'text', - })), - })), - }, - }; - - const formattedAttribute = formatAttribute('key', mockAttribute); - - const expected = { - type: 'text', - customField: 'plugin::mycustomfields.color', - }; - expect(formattedAttribute).toEqual(expected); - }); -}); diff --git a/packages/core/content-type-builder/server/utils/attributes.js b/packages/core/content-type-builder/server/utils/attributes.js index 620a537811..e90fc1fd1a 100644 --- a/packages/core/content-type-builder/server/utils/attributes.js +++ b/packages/core/content-type-builder/server/utils/attributes.js @@ -67,15 +67,6 @@ const formatAttribute = (key, attribute) => { }; } - if (attribute.type === 'customField') { - const customField = strapi.container.get('custom-fields').get(attribute.customField); - - return { - ...attribute, - type: customField.type, - }; - } - return attribute; }; diff --git a/packages/core/strapi/lib/Strapi.js b/packages/core/strapi/lib/Strapi.js index 91ad819181..19059fe207 100644 --- a/packages/core/strapi/lib/Strapi.js +++ b/packages/core/strapi/lib/Strapi.js @@ -40,6 +40,7 @@ const bootstrap = require('./core/bootstrap'); const loaders = require('./core/loaders'); const { destroyOnSignal } = require('./utils/signals'); const sanitizersRegistry = require('./core/registries/sanitizers'); +const convertCustomFieldType = require('./utils/convert-custom-field-type'); // TODO: move somewhere else const draftAndPublishSync = require('./migrations/draft-publish'); @@ -428,6 +429,8 @@ class Strapi { async load() { await this.register(); + // Swap type customField for underlying data type + convertCustomFieldType(this); await this.bootstrap(); this.isLoaded = true; diff --git a/packages/core/strapi/lib/utils/__tests__/convert-custom-field-type.test.js b/packages/core/strapi/lib/utils/__tests__/convert-custom-field-type.test.js new file mode 100644 index 0000000000..b4d255ef23 --- /dev/null +++ b/packages/core/strapi/lib/utils/__tests__/convert-custom-field-type.test.js @@ -0,0 +1,49 @@ +'use strict'; + +const convertCustomFieldType = require('../convert-custom-field-type'); + +describe('format attributes', () => { + it('replaces type customField with the underlying data type', () => { + global.strapi = { + container: { + // mock container.get('custom-fields') + get: jest.fn(() => ({ + // mock container.get('custom-fields').get(uid) + get: jest.fn(() => ({ + name: 'color', + plugin: 'mycustomfields', + type: 'text', + })), + })), + }, + contentTypes: { + test: { + attributes: { + color: { + type: 'customField', + customField: 'plugin::mycustomfields.color', + }, + }, + }, + }, + }; + + convertCustomFieldType(global.strapi); + + const expected = { + ...global.strapi, + contentTypes: { + test: { + attributes: { + color: { + type: 'text', + customField: 'plugin::mycustomfields.color', + }, + }, + }, + }, + }; + + expect(global.strapi).toEqual(expected); + }); +}); diff --git a/packages/core/strapi/lib/utils/convert-custom-field-type.js b/packages/core/strapi/lib/utils/convert-custom-field-type.js new file mode 100644 index 0000000000..ba41f74af2 --- /dev/null +++ b/packages/core/strapi/lib/utils/convert-custom-field-type.js @@ -0,0 +1,15 @@ +'use strict'; + +const convertCustomFieldType = strapi => { + const allSchemasAttributes = Object.values(strapi.contentTypes).map(schema => schema.attributes); + for (const schemaAttrbutes of allSchemasAttributes) { + for (const attribute of Object.values(schemaAttrbutes)) { + if (attribute.type === 'customField') { + const customField = strapi.container.get('custom-fields').get(attribute.customField); + attribute.type = customField.type; + } + } + } +}; + +module.exports = convertCustomFieldType; diff --git a/packages/plugins/documentation/server/services/helpers/utils/clean-schema-attributes.js b/packages/plugins/documentation/server/services/helpers/utils/clean-schema-attributes.js index b3fdc73925..0c8bc4a467 100644 --- a/packages/plugins/documentation/server/services/helpers/utils/clean-schema-attributes.js +++ b/packages/plugins/documentation/server/services/helpers/utils/clean-schema-attributes.js @@ -19,11 +19,6 @@ const cleanSchemaAttributes = (attributes, { typeMap = new Map(), isRequest = fa delete attributesCopy[prop].default; } - if (attribute.type === 'customField') { - const customField = strapi.container.get('custom-fields').get(attribute.customField); - attribute.type = customField.type; - } - switch (attribute.type) { case 'password': { if (!isRequest) {