213 lines
6.0 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');
/**
2022-03-17 16:54:37 +01:00
* @description - Converts types found on attributes to OpenAPI acceptable data types
*
2021-09-02 11:25:24 +02:00
* @param {object} attributes - The attributes found on a contentType
* @param {{ typeMap: Map, isRequest: boolean }} opts
2021-09-02 11:25:24 +02:00
* @returns Attributes using OpenAPI acceptable data types
*/
const cleanSchemaAttributes = (attributes, { typeMap = new Map(), isRequest = false } = {}) => {
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) {
2021-10-19 10:55:11 +02:00
case 'password': {
if (!isRequest) {
delete attributesCopy[prop];
break;
}
attributesCopy[prop] = { type: 'string', format: 'password', example: '*******' };
break;
}
case 'email': {
attributesCopy[prop] = { type: 'string', format: 'email' };
break;
}
case 'string':
2021-10-13 12:39:34 -04:00
case 'text':
2021-10-19 10:55:11 +02:00
case 'richtext': {
2021-09-02 11:25:24 +02:00
attributesCopy[prop] = { type: 'string' };
break;
}
2021-10-19 10:55:11 +02:00
case 'timestamp': {
attributesCopy[prop] = { type: 'string', format: 'timestamp', example: Date.now() };
break;
}
case 'time': {
attributesCopy[prop] = { type: 'string', format: 'time', example: '12:54.000' };
break;
}
case 'date': {
attributesCopy[prop] = { type: 'string', format: 'date' };
break;
}
case 'datetime': {
attributesCopy[prop] = { type: 'string', format: 'date-time' };
break;
}
case 'boolean': {
attributesCopy[prop] = { type: 'boolean' };
break;
}
case 'enumeration': {
attributesCopy[prop] = { type: 'string', enum: attribute.enum };
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;
}
2021-10-19 10:55:11 +02:00
case 'biginteger': {
attributesCopy[prop] = { type: 'string', pattern: '^\\d*$', example: '123456789' };
break;
}
2021-09-02 11:25:24 +02:00
case 'json': {
attributesCopy[prop] = {};
break;
}
case 'uid': {
2021-10-19 10:55:11 +02:00
attributesCopy[prop] = { type: 'string' };
2021-09-02 11:25:24 +02:00
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: {
...(isRequest ? {} : { id: { type: 'string' } }),
...cleanSchemaAttributes(componentAttributes, { typeMap, isRequest }),
2021-10-13 12:39:34 -04:00
},
},
};
} else {
attributesCopy[prop] = {
type: 'object',
properties: {
...(isRequest ? {} : { id: { type: 'string' } }),
...cleanSchemaAttributes(componentAttributes, {
typeMap,
isRequest,
}),
},
};
}
break;
}
case 'dynamiczone': {
const components = attribute.components.map(component => {
const componentAttributes = strapi.components[component].attributes;
return {
type: 'object',
properties: {
...(isRequest ? {} : { id: { type: 'string' } }),
__component: { type: 'string' },
...cleanSchemaAttributes(componentAttributes, { typeMap, isRequest }),
2021-10-13 12:39:34 -04:00
},
};
});
attributesCopy[prop] = {
type: 'array',
items: {
anyOf: components,
},
};
break;
}
case 'media': {
const imageAttributes = strapi.contentType('plugin::upload.file').attributes;
const isListOfEntities = attribute.multiple;
if (isRequest) {
const oneOfType = {
oneOf: [{ type: 'integer' }, { type: 'string' }],
example: 'string or id',
};
attributesCopy[prop] = isListOfEntities ? { type: 'array', items: oneOfType } : oneOfType;
break;
2021-10-13 12:39:34 -04:00
}
attributesCopy[prop] = {
type: 'object',
properties: {
data: getSchemaData(isListOfEntities, cleanSchemaAttributes(imageAttributes)),
},
};
2021-09-02 11:25:24 +02:00
break;
}
2021-09-02 11:25:24 +02:00
case 'relation': {
const isListOfEntities = attribute.relation.includes('ToMany');
if (isRequest) {
const oneOfType = {
oneOf: [{ type: 'integer' }, { type: 'string' }],
example: 'string or id',
};
attributesCopy[prop] = isListOfEntities ? { type: 'array', items: oneOfType } : oneOfType;
break;
}
2022-05-05 12:32:22 +02:00
if (prop === 'localizations') {
attributesCopy[prop] = {
type: 'array',
items: { type: 'object', properties: {} },
};
break;
}
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, isRequest })
),
2021-09-02 11:25:24 +02:00
},
};
break;
}
default: {
throw new Error(`Invalid type ${attribute.type} while generating open api schema.`);
2021-10-13 12:39:34 -04:00
}
2021-09-02 11:25:24 +02:00
}
}
return attributesCopy;
};
module.exports = cleanSchemaAttributes;