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",
"singularName": "kitchensink",
"pluralName": "kitchensinks",
"description": ""
"description": "",
"name": "Kitchen Sink"
},
"options": {
"draftAndPublish": true
@ -100,7 +101,8 @@
"dynamiczone": {
"type": "dynamiczone",
"components": [
"basic.simple"
"basic.simple",
"basic.kitchensink"
]
},
"one_way_tag": {
@ -145,6 +147,11 @@
"morph_to_many": {
"type": "relation",
"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 { removeEmptyDefaults } = require('./data-transform');
const VALID_RELATIONS = ['oneWay', 'manyWay'];
const VALID_RELATIONS = ['oneToOne', 'oneToMany'];
const VALID_TYPES = [...DEFAULT_TYPES, 'component'];
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));
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') {
const data = transformEntry(property, strapi.contentType('plugin::upload.file'));

View File

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