Fix components relations in CTB and missing transform in REST API

This commit is contained in:
Alexandre Bodin 2021-09-28 14:34:26 +02:00
parent 12f88b5036
commit 1f121e5acf
5 changed files with 114 additions and 35 deletions

View File

@ -5,7 +5,8 @@
"displayName": "Kitchen Sink", "displayName": "Kitchen Sink",
"singularName": "kitchensink", "singularName": "kitchensink",
"pluralName": "kitchensinks", "pluralName": "kitchensinks",
"description": "" "description": "",
"name": "Kitchen Sink"
}, },
"options": { "options": {
"draftAndPublish": true "draftAndPublish": true
@ -100,7 +101,8 @@
"dynamiczone": { "dynamiczone": {
"type": "dynamiczone", "type": "dynamiczone",
"components": [ "components": [
"basic.simple" "basic.simple",
"basic.kitchensink"
] ]
}, },
"one_way_tag": { "one_way_tag": {
@ -145,6 +147,11 @@
"morph_to_many": { "morph_to_many": {
"type": "relation", "type": "relation",
"relation": "morphToMany" "relation": "morphToMany"
},
"compo": {
"type": "component",
"repeatable": false,
"component": "basic.kitchensink"
} }
} }
} }

View File

@ -0,0 +1,24 @@
{
"collectionName": "components_basic_kitchensinks",
"info": {
"name": "kitchensink",
"icon": "adjust",
"description": ""
},
"options": {},
"attributes": {
"address": {
"type": "relation",
"relation": "oneToOne",
"target": "api::address.address"
},
"countries": {
"type": "relation",
"relation": "oneToMany",
"target": "api::country.country"
},
"name": {
"type": "string"
}
}
}

View File

@ -9,7 +9,7 @@ const { isValidCategoryName, isValidIcon } = require('./common');
const createSchema = require('./model-schema'); const createSchema = require('./model-schema');
const { removeEmptyDefaults } = require('./data-transform'); const { removeEmptyDefaults } = require('./data-transform');
const VALID_RELATIONS = ['oneWay', 'manyWay']; const VALID_RELATIONS = ['oneToOne', 'oneToMany'];
const VALID_TYPES = [...DEFAULT_TYPES, 'component']; const VALID_TYPES = [...DEFAULT_TYPES, 'component'];
const componentSchema = createSchema(VALID_TYPES, VALID_RELATIONS, { const componentSchema = createSchema(VALID_TYPES, VALID_RELATIONS, {

View File

@ -49,6 +49,22 @@ const transformEntry = (entry, contentType) => {
const data = transformEntry(property, strapi.contentType(attribute.target)); const data = transformEntry(property, strapi.contentType(attribute.target));
attributeValues[key] = { data }; attributeValues[key] = { data };
} else if (attribute && attribute.type === 'component') {
const { id, attributes } = transformEntry(property, strapi.components[attribute.component]);
attributeValues[key] = { id, ...attributes };
} else if (attribute && attribute.type === 'dynamiczone') {
if (Array.isArray(property)) {
attributeValues[key] = property.map(subProperty => {
const { id, attributes } = transformEntry(
subProperty,
strapi.components[subProperty.__component]
);
return { id, ...attributes };
});
} else {
attributeValues[key] = property;
}
} else if (attribute && attribute.type === 'media') { } else if (attribute && attribute.type === 'media') {
const data = transformEntry(property, strapi.contentType('plugin::upload.file')); const data = transformEntry(property, strapi.contentType('plugin::upload.file'));

View File

@ -126,19 +126,27 @@ describe('Not required dynamiczone', () => {
id: expect.anything(), id: expect.anything(),
__component: 'default.one-media', __component: 'default.one-media',
media: { media: {
id: mediaId, data: {
url: expect.any(String), id: mediaId,
attributes: expect.objectContaining({
url: expect.any(String),
}),
},
}, },
}, },
{ {
id: expect.anything(), id: expect.anything(),
__component: 'default.many-media', __component: 'default.many-media',
media: expect.arrayContaining([ media: {
expect.objectContaining({ data: expect.arrayContaining([
id: mediaId, expect.objectContaining({
url: expect.any(String), id: mediaId,
}), attributes: expect.objectContaining({
]), url: expect.any(String),
}),
}),
]),
},
}, },
], ],
}, },
@ -209,19 +217,27 @@ describe('Not required dynamiczone', () => {
id: expect.anything(), id: expect.anything(),
__component: 'default.one-media', __component: 'default.one-media',
media: { media: {
id: newMediaId, data: {
url: expect.any(String), id: newMediaId,
attributes: expect.objectContaining({
url: expect.any(String),
}),
},
}, },
}, },
{ {
id: expect.anything(), id: expect.anything(),
__component: 'default.many-media', __component: 'default.many-media',
media: expect.arrayContaining([ media: {
expect.objectContaining({ data: expect.arrayContaining([
id: newMediaId, expect.objectContaining({
url: expect.any(String), id: newMediaId,
}), attributes: expect.objectContaining({
]), url: expect.any(String),
}),
}),
]),
},
}, },
], ],
}, },
@ -273,19 +289,27 @@ describe('Not required dynamiczone', () => {
id: expect.anything(), id: expect.anything(),
__component: 'default.one-media', __component: 'default.one-media',
media: { media: {
id: mediaId, data: {
url: expect.any(String), id: mediaId,
attributes: expect.objectContaining({
url: expect.any(String),
}),
},
}, },
}, },
{ {
id: expect.anything(), id: expect.anything(),
__component: 'default.many-media', __component: 'default.many-media',
media: expect.arrayContaining([ media: {
expect.objectContaining({ data: expect.arrayContaining([
id: mediaId, expect.objectContaining({
url: expect.any(String), id: mediaId,
}), attributes: expect.objectContaining({
]), url: expect.any(String),
}),
}),
]),
},
}, },
], ],
}, },
@ -333,17 +357,25 @@ describe('Not required dynamiczone', () => {
__component: 'default.with-nested', __component: 'default.with-nested',
singleMedia: { singleMedia: {
media: { media: {
id: mediaId, data: {
url: expect.any(String), id: mediaId,
attributes: expect.objectContaining({
url: expect.any(String),
}),
},
}, },
}, },
multipleMedia: { multipleMedia: {
media: expect.arrayContaining([ media: {
expect.objectContaining({ data: expect.arrayContaining([
id: mediaId, expect.objectContaining({
url: expect.any(String), id: mediaId,
}), attributes: expect.objectContaining({
]), url: expect.any(String),
}),
}),
]),
},
}, },
}, },
], ],