From fcc1484fb425092bc481ebb890caf0332c00800c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Barthelet?= Date: Tue, 9 Mar 2021 14:41:54 +0100 Subject: [PATCH 1/2] Update json-schema-to-typescript version --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index 7e6d7cf..99975b5 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,6 @@ "url": "https://github.com/serverless/typescript/issues" }, "homepage": "https://github.com/serverless/typescript#readme", - "dependencies": {}, "devDependencies": { "@types/json-schema": "^7.0.6", "@typescript-eslint/eslint-plugin": "^4.10.0", @@ -35,7 +34,7 @@ "eslint-config-prettier": "^7.1.0", "eslint-plugin-import": "^2.22.1", "eslint-plugin-prettier": "^3.3.0", - "json-schema-to-typescript": "^10.0.2", + "json-schema-to-typescript": "^10.1.3", "prettier": "^2.2.1", "serverless": "*", "typescript": "^4.0.5" From a46a20aef72c039faf05732b10ce27a0bc9c2926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Barthelet?= Date: Tue, 9 Mar 2021 15:33:51 +0100 Subject: [PATCH 2/2] Use deep clone before compiling --- package.json | 1 + src/plugin.ts | 36 +++--------------------------------- 2 files changed, 4 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index 99975b5..e92e7b2 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "eslint-plugin-import": "^2.22.1", "eslint-plugin-prettier": "^3.3.0", "json-schema-to-typescript": "^10.1.3", + "lodash": "^4.17.21", "prettier": "^2.2.1", "serverless": "*", "typescript": "^4.0.5" diff --git a/src/plugin.ts b/src/plugin.ts index dc3a5f9..b83fac2 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1,6 +1,7 @@ import { compile } from "json-schema-to-typescript"; import fs from "fs"; import type { JSONSchema4 } from "json-schema"; +import * as _ from "lodash"; interface Serverless { configSchemaHandler: { @@ -12,7 +13,7 @@ class ConfigSchemaHandlerTypescriptDefinitionsPlugin { private schema: JSONSchema4; constructor(serverless: Serverless) { - this.schema = serverless.configSchemaHandler.schema; + this.schema = _.cloneDeep(serverless.configSchemaHandler.schema); } commands = { @@ -27,45 +28,14 @@ class ConfigSchemaHandlerTypescriptDefinitionsPlugin { }; async generateSchema() { - /** - * https://github.com/serverless/typescript/issues/4 - * JSON Schema v6 `const` keyword converted to `enum` - */ - const normalizedSchema = replaceAllConstForEnum(this.schema); - /** * ignoreMinAndMaxItems: true -> maxItems: 100 in provider.s3.corsConfiguration definition is generating 100 tuples */ - const compiledDefinitions = await compile(normalizedSchema, "AWS", { + const compiledDefinitions = await compile(this.schema, "AWS", { ignoreMinAndMaxItems: true, - unreachableDefinitions: true, }); fs.writeFileSync("index.d.ts", compiledDefinitions); } } -const replaceAllConstForEnum = (schema: JSONSchema4): JSONSchema4 => { - if ("object" !== typeof schema) { - return schema; - } - - return Object.fromEntries( - Object.entries(schema).map(([key, value]) => { - if (key === "const") { - return ["enum", [value]]; - } - - if (Array.isArray(value)) { - return [key, value.map(replaceAllConstForEnum)]; - } - - if ("object" === typeof value && value !== null) { - return [key, replaceAllConstForEnum(value)]; - } - - return [key, value]; - }) - ); -}; - module.exports = ConfigSchemaHandlerTypescriptDefinitionsPlugin;