strapi/packages/plugins/documentation/server/utils/clean-schema-attributes.js

134 lines
3.6 KiB
JavaScript
Raw Normal View History

2021-09-02 11:25:24 +02:00
'use strict';
const _ = require('lodash');
const getSchemaData = require('./get-schema-data');
/**
* @description - Converts types found on attributes to OpenAPI specific data types
*
* @param {object} attributes - The attributes found on a contentType
* @returns Attributes using OpenAPI acceptable data types
*/
const cleanSchemaAttributes = (attributes, typeMap = new Map()) => {
2021-09-02 11:25:24 +02:00
const attributesCopy = _.cloneDeep(attributes);
for (const prop in attributesCopy) {
const attribute = attributesCopy[prop];
if (attribute.default) {
delete attributesCopy[prop].default;
}
switch (attribute.type) {
case 'password':
case 'email':
case 'date':
2021-10-13 12:39:34 -04:00
case 'text':
2021-09-02 11:25:24 +02:00
case 'datetime': {
attributesCopy[prop] = { type: 'string' };
break;
}
2021-10-13 12:39:34 -04:00
case 'decimal':
case 'float': {
2021-09-02 11:25:24 +02:00
attributesCopy[prop] = { type: 'number', format: 'float' };
break;
}
case 'integer': {
attributesCopy[prop] = { type: 'integer' };
break;
}
case 'json': {
attributesCopy[prop] = {};
break;
}
case 'uid': {
attributesCopy[prop] = { type: 'string', format: 'uuid' };
break;
}
case 'media': {
const imageAttributes = strapi.plugin('upload').contentType('file').attributes;
const isListOfEntities = attribute.multiple;
attributesCopy[prop] = {
type: 'object',
properties: {
data: getSchemaData(isListOfEntities, cleanSchemaAttributes(imageAttributes)),
},
};
break;
}
case 'component': {
const componentAttributes = strapi.components[attribute.component].attributes;
2021-10-13 12:39:34 -04:00
if (attribute.repeatable) {
attributesCopy[prop] = {
type: 'array',
items: {
type: 'object',
properties: {
id: { type: 'string' },
...cleanSchemaAttributes(componentAttributes),
},
},
};
} else {
attributesCopy[prop] = {
type: 'object',
properties: {
id: { type: 'string' },
...cleanSchemaAttributes(componentAttributes),
},
};
}
2021-09-02 11:25:24 +02:00
break;
}
case 'relation': {
const isListOfEntities = attribute.relation.includes('ToMany');
if (!attribute.target || typeMap.has(attribute.target)) {
attributesCopy[prop] = {
type: 'object',
properties: { data: getSchemaData(isListOfEntities, {}) },
};
break;
}
typeMap.set(attribute.target, true);
const targetAttributes = strapi.contentType(attribute.target).attributes;
2021-09-02 11:25:24 +02:00
attributesCopy[prop] = {
type: 'object',
properties: {
data: getSchemaData(isListOfEntities, cleanSchemaAttributes(targetAttributes, typeMap)),
2021-09-02 11:25:24 +02:00
},
};
break;
}
2021-10-13 12:39:34 -04:00
case 'dynamiczone': {
const components = attribute.components.map(component => {
const componentAttributes = strapi.components[component].attributes;
return {
type: 'object',
properties: {
__component: { type: 'string' },
...cleanSchemaAttributes(componentAttributes),
},
};
});
attributesCopy[prop] = {
type: 'array',
items: {
anyOf: components,
},
};
}
2021-09-02 11:25:24 +02:00
}
}
return attributesCopy;
};
module.exports = cleanSchemaAttributes;