|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const path = require('path') |
| 4 | + |
| 5 | +const $RefParser = require("@apidevtools/json-schema-ref-parser") |
| 6 | +const SchemaConvertor = require('json-schema-for-openapi') |
| 7 | +const isEqual = require('lodash.isequal') |
| 8 | +const { v4: uuid } = require('uuid') |
| 9 | + |
| 10 | +class SchemaHandler { |
| 11 | + constructor(serverless, openAPI) { |
| 12 | + this.documentation = serverless.service.custom.documentation |
| 13 | + this.openAPI = openAPI |
| 14 | + |
| 15 | + this.modelReferences = {} |
| 16 | + |
| 17 | + this.__standardiseModels() |
| 18 | + |
| 19 | + try { |
| 20 | + this.refParserOptions = require(path.resolve('options', 'ref-parser.js')) |
| 21 | + } catch (err) { |
| 22 | + this.refParserOptions = {} |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Standardises the models to a specific format |
| 28 | + */ |
| 29 | + __standardiseModels() { |
| 30 | + const standardModel = (model) => { |
| 31 | + if (model.schema) { |
| 32 | + return model |
| 33 | + } |
| 34 | + |
| 35 | + const contentType = Object.keys(model.content)[0] |
| 36 | + model.contentType = contentType |
| 37 | + model.schema = model.content[contentType].schema |
| 38 | + |
| 39 | + return model |
| 40 | + } |
| 41 | + |
| 42 | + const standardisedModels = this.documentation?.models?.map(standardModel) || [] |
| 43 | + const standardisedModelsList = this.documentation?.modelsList?.map(standardModel) || [] |
| 44 | + |
| 45 | + this.models = standardisedModels.length ? standardisedModels.concat(standardisedModelsList) : standardisedModelsList |
| 46 | + } |
| 47 | + |
| 48 | + async addModelsToOpenAPI() { |
| 49 | + for (const model of this.models) { |
| 50 | + const modelName = model.name |
| 51 | + const modelSchema = model.schema |
| 52 | + |
| 53 | + const dereferencedSchema = await this.__dereferenceSchema(modelSchema) |
| 54 | + .catch(err => { |
| 55 | + if(err.errors) { |
| 56 | + for (const error of err?.errors) { |
| 57 | + if (error.message.includes('HTTP ERROR')) { |
| 58 | + throw err |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + return modelSchema |
| 63 | + }) |
| 64 | + |
| 65 | + const convertedSchemas = SchemaConvertor.convert(dereferencedSchema, modelName) |
| 66 | + |
| 67 | + for (const [schemaName, schemaValue] of Object.entries(convertedSchemas.schemas)) { |
| 68 | + if (schemaName === modelName) { |
| 69 | + this.modelReferences[schemaName] = `#/components/schemas/${modelName}` |
| 70 | + } |
| 71 | + |
| 72 | + this.__addToComponents('schemas', schemaValue, schemaName) |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + async createSchema(name, schema) { |
| 78 | + let originalName = name; |
| 79 | + let finalName = name; |
| 80 | + |
| 81 | + if (this.modelReferences[name] && schema === undefined) { |
| 82 | + return this.modelReferences[name] |
| 83 | + } |
| 84 | + |
| 85 | + const dereferencedSchema = await this.__dereferenceSchema(schema) |
| 86 | + .catch(err => { |
| 87 | + throw err |
| 88 | + }) |
| 89 | + |
| 90 | + const convertedSchemas = SchemaConvertor.convert(dereferencedSchema, name) |
| 91 | + |
| 92 | + for (const [schemaName, schemaValue] of Object.entries(convertedSchemas.schemas)) { |
| 93 | + if (this.__existsInComponents(schemaName)) { |
| 94 | + if (this.__isTheSameSchema(schemaValue, schemaName) === false) { |
| 95 | + if (schemaName === originalName) { |
| 96 | + finalName = `${schemaName}-${uuid()}` |
| 97 | + this.__addToComponents('schemas', schemaValue, finalName) |
| 98 | + } else { |
| 99 | + this.__addToComponents('schemas', schemaValue, schemaName) |
| 100 | + } |
| 101 | + } |
| 102 | + } else { |
| 103 | + this.__addToComponents('schemas', schemaValue, schemaName) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return `#/components/schemas/${finalName}` |
| 108 | + } |
| 109 | + |
| 110 | + async __dereferenceSchema(schema) { |
| 111 | + const bundledSchema = await $RefParser.bundle(schema, this.refParserOptions) |
| 112 | + .catch(err => { |
| 113 | + throw err |
| 114 | + }) |
| 115 | + |
| 116 | + let deReferencedSchema = await $RefParser.dereference(bundledSchema, this.refParserOptions) |
| 117 | + .catch(err => { |
| 118 | + throw err |
| 119 | + }) |
| 120 | + |
| 121 | + // deal with schemas that have been de-referenced poorly: naive |
| 122 | + if (deReferencedSchema?.$ref === '#') { |
| 123 | + const oldRef = bundledSchema.$ref |
| 124 | + const path = oldRef.split('/') |
| 125 | + |
| 126 | + const pathTitle = path[path.length - 1] |
| 127 | + const referencedProperties = deReferencedSchema.definitions[pathTitle] |
| 128 | + |
| 129 | + Object.assign(deReferencedSchema, { ...referencedProperties }) |
| 130 | + |
| 131 | + delete deReferencedSchema.$ref |
| 132 | + deReferencedSchema = await this.__dereferenceSchema(deReferencedSchema) |
| 133 | + .catch((err) => { |
| 134 | + throw err |
| 135 | + }) |
| 136 | + } |
| 137 | + |
| 138 | + return deReferencedSchema |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @function existsInComponents |
| 143 | + * @param {string} name - The name of the Schema |
| 144 | + * @returns {boolean} Whether it exists in components already |
| 145 | + */ |
| 146 | + __existsInComponents(name) { |
| 147 | + return Boolean(this.openAPI?.components?.schemas?.[name]) |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * @function isTheSameSchema |
| 152 | + * @param {object} schema - The schema value |
| 153 | + * @param {string} otherSchemaName - The name of the schema |
| 154 | + * @returns {boolean} Whether the schema provided is the same one as in components already |
| 155 | + */ |
| 156 | + __isTheSameSchema(schema, otherSchemaName) { |
| 157 | + return isEqual(schema, this.openAPI.components.schemas[otherSchemaName]) |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * @function addToComponents |
| 162 | + * @param {string} type - The component type |
| 163 | + * @param {object} schema - The schema |
| 164 | + * @param {string} name - The name of the schema |
| 165 | + */ |
| 166 | + __addToComponents(type, schema, name) { |
| 167 | + const schemaObj = { |
| 168 | + [name]: schema |
| 169 | + } |
| 170 | + |
| 171 | + if (this.openAPI?.components) { |
| 172 | + if (this.openAPI.components[type]) { |
| 173 | + Object.assign(this.openAPI.components[type], schemaObj) |
| 174 | + } else { |
| 175 | + Object.assign(this.openAPI.components, { [type]: schemaObj }) |
| 176 | + } |
| 177 | + } else { |
| 178 | + const components = { |
| 179 | + components: { |
| 180 | + [type]: schemaObj |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + Object.assign(this.openAPI, components) |
| 185 | + } |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +module.exports = SchemaHandler; |
0 commit comments