renamed addSchema to addComponentSchema and fixed a template string

This commit is contained in:
Andrei Wilkens 2022-08-01 13:44:21 +02:00
parent be96961ff9
commit f4519a89a1
2 changed files with 14 additions and 14 deletions

View File

@ -21,7 +21,7 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
let schemas = {}; let schemas = {};
let componentSchemas = {}; let componentSchemas = {};
// adds a ComponentSchema to the Schemas so it can be used as Ref // adds a ComponentSchema to the Schemas so it can be used as Ref
const addSchema = (schemaName, schema) => { const addComponentSchema = (schemaName, schema) => {
if (!Object.keys(schema) || !Object.keys(schema.properties)) { if (!Object.keys(schema) || !Object.keys(schema.properties)) {
return false; return false;
} }
@ -65,7 +65,7 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
[`${pascalCase(uniqueName)}LocalizationRequest`]: { [`${pascalCase(uniqueName)}LocalizationRequest`]: {
required: [...requiredAttributes, 'locale'], required: [...requiredAttributes, 'locale'],
type: 'object', type: 'object',
properties: cleanSchemaAttributes(attributesForRequest, { isRequest: true, addSchema }), properties: cleanSchemaAttributes(attributesForRequest, { isRequest: true, addComponentSchema }),
}, },
}; };
} }
@ -80,7 +80,7 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
data: { data: {
required: requiredAttributes, required: requiredAttributes,
type: 'object', type: 'object',
properties: cleanSchemaAttributes(attributesForRequest, { isRequest: true, addSchema }), properties: cleanSchemaAttributes(attributesForRequest, { isRequest: true, addComponentSchema }),
}, },
}, },
}, },
@ -94,7 +94,7 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
type: 'object', type: 'object',
properties: { properties: {
id: { type: 'string' }, id: { type: 'string' },
...cleanSchemaAttributes(attributes, { addSchema }), ...cleanSchemaAttributes(attributes, { addComponentSchema }),
}, },
}, },
}; };
@ -113,7 +113,7 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
attributes: { attributes: {
type: 'object', type: 'object',
properties: cleanSchemaAttributes(attributes, { properties: cleanSchemaAttributes(attributes, {
addSchema, addComponentSchema,
componentSchemaRefName: `#/components/schemas/${pascalCase( componentSchemaRefName: `#/components/schemas/${pascalCase(
uniqueName uniqueName
)}ListResponseDataItemLocalized`, )}ListResponseDataItemLocalized`,
@ -127,7 +127,7 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
id: { type: 'string' }, id: { type: 'string' },
attributes: { attributes: {
type: 'object', type: 'object',
properties: cleanSchemaAttributes(attributes, { addSchema }), properties: cleanSchemaAttributes(attributes, { addComponentSchema }),
}, },
}, },
}, },
@ -167,7 +167,7 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
attributes: { attributes: {
type: 'object', type: 'object',
properties: cleanSchemaAttributes(attributes, { properties: cleanSchemaAttributes(attributes, {
addSchema, addComponentSchema,
componentSchemaRefName: `#/components/schemas/${pascalCase( componentSchemaRefName: `#/components/schemas/${pascalCase(
uniqueName uniqueName
)}ResponseDataObjectLocalized`, )}ResponseDataObjectLocalized`,
@ -181,7 +181,7 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
id: { type: 'string' }, id: { type: 'string' },
attributes: { attributes: {
type: 'object', type: 'object',
properties: cleanSchemaAttributes(attributes, { addSchema }), properties: cleanSchemaAttributes(attributes, { addComponentSchema }),
}, },
}, },
}, },

View File

@ -7,12 +7,12 @@ const pascalCase = require('./pascal-case');
* @description - Converts types found on attributes to OpenAPI acceptable data types * @description - Converts types found on attributes to OpenAPI acceptable data types
* *
* @param {object} attributes - The attributes found on a contentType * @param {object} attributes - The attributes found on a contentType
* @param {{ typeMap: Map, isRequest: boolean, addSchema: function, componentSchemaRefName: string }} opts * @param {{ typeMap: Map, isRequest: boolean, addComponentSchema: function, componentSchemaRefName: string }} opts
* @returns Attributes using OpenAPI acceptable data types * @returns Attributes using OpenAPI acceptable data types
*/ */
const cleanSchemaAttributes = ( const cleanSchemaAttributes = (
attributes, attributes,
{ typeMap = new Map(), isRequest = false, addSchema = () => {}, componentSchemaRefName = '' } = {} { typeMap = new Map(), isRequest = false, addComponentSchema = () => {}, componentSchemaRefName = '' } = {}
) => { ) => {
const attributesCopy = _.cloneDeep(attributes); const attributesCopy = _.cloneDeep(attributes);
@ -100,9 +100,9 @@ const cleanSchemaAttributes = (
}, },
}; };
const refComponentSchema = { const refComponentSchema = {
$ref: `#/components/schemas/${`${pascalCase(attribute.component)}Component`}`, $ref: `#/components/schemas/${pascalCase(attribute.component)}Component`,
}; };
const componentExists = addSchema( const componentExists = addComponentSchema(
`${pascalCase(attribute.component)}Component`, `${pascalCase(attribute.component)}Component`,
rawComponentSchema rawComponentSchema
); );
@ -125,11 +125,11 @@ const cleanSchemaAttributes = (
properties: { properties: {
...(isRequest ? {} : { id: { type: 'string' } }), ...(isRequest ? {} : { id: { type: 'string' } }),
__component: { type: 'string' }, __component: { type: 'string' },
...cleanSchemaAttributes(componentAttributes, { typeMap, isRequest, addSchema }), ...cleanSchemaAttributes(componentAttributes, { typeMap, isRequest, addComponentSchema }),
}, },
}; };
const refComponentSchema = { $ref: `#/components/schemas/${pascalCase(component)}` }; const refComponentSchema = { $ref: `#/components/schemas/${pascalCase(component)}` };
const componentExists = addSchema(pascalCase(component), rawComponentSchema); const componentExists = addComponentSchema(pascalCase(component), rawComponentSchema);
const finalComponentSchema = componentExists ? refComponentSchema : rawComponentSchema; const finalComponentSchema = componentExists ? refComponentSchema : rawComponentSchema;
return finalComponentSchema; return finalComponentSchema;
}); });