From 25fb213bbaf73536d17fdb74a0271cf72a4978dc Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU <20847995+ardatan@users.noreply.github.com> Date: Wed, 12 Feb 2020 18:33:50 +0300 Subject: [PATCH] Fix broken type of ID scalar in generated swagger document (#280) --- example/swagger.json | 11 ++++------- example/swagger.yml | 10 ++++------ package.json | 2 +- src/open-api/index.ts | 6 +----- src/open-api/utils.ts | 3 +++ 5 files changed, 13 insertions(+), 19 deletions(-) diff --git a/example/swagger.json b/example/swagger.json index a968484b..b3707671 100644 --- a/example/swagger.json +++ b/example/swagger.json @@ -32,7 +32,7 @@ "name": "id", "required": true, "schema": { - "$ref": "#/components/schemas/ID" + "type": "string" } } ], @@ -141,7 +141,7 @@ "name": "id", "required": true, "schema": { - "$ref": "#/components/schemas/ID" + "type": "string" } } ], @@ -323,7 +323,7 @@ ], "properties": { "id": { - "type": "object" + "type": "string" }, "name": { "type": "string" @@ -365,7 +365,7 @@ "required": ["id", "title"], "properties": { "id": { - "type": "object" + "type": "string" }, "title": { "type": "string" @@ -410,9 +410,6 @@ "$ref": "#/components/schemas/Book" } } - }, - "ID": { - "type": "string" } } } diff --git a/example/swagger.yml b/example/swagger.yml index fd88c963..dc1ff9b1 100644 --- a/example/swagger.yml +++ b/example/swagger.yml @@ -23,7 +23,7 @@ paths: name: id required: true schema: - $ref: '#/components/schemas/ID' + type: string responses: '200': description: "" @@ -92,7 +92,7 @@ paths: name: id required: true schema: - $ref: '#/components/schemas/ID' + type: string responses: '200': description: "" @@ -208,7 +208,7 @@ components: - shelf properties: id: - type: object + type: string name: type: string favoritePizza: @@ -239,7 +239,7 @@ components: - title properties: id: - type: object + type: string title: type: string Salad: @@ -268,5 +268,3 @@ components: properties: onBook: $ref: '#/components/schemas/Book' - ID: - type: string diff --git a/package.json b/package.json index 3729cedd..68c9b6ac 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "yamljs": "0.3.0" }, "scripts": { - "start": "ts-node example/index.ts", + "start": "ts-node -O \"{\\\"module\\\": \\\"commonjs\\\"}\" example/index.ts", "clean": "rm -rf dist", "prebuild": "yarn clean", "build": "bob", diff --git a/src/open-api/index.ts b/src/open-api/index.ts index 2b24eb56..b99280ef 100644 --- a/src/open-api/index.ts +++ b/src/open-api/index.ts @@ -47,7 +47,7 @@ export function OpenAPI({ basePath?: string; } ) { - const basePath = (config && config.basePath) || ''; + const basePath = config?.basePath || ''; const path = basePath + info.path.replace( @@ -65,10 +65,6 @@ export function OpenAPI({ schema, useRequestBody: ['POST', 'PUT', 'PATCH'].includes(info.method), }); - - swagger.components.schemas.ID = { - type: 'string', - }; }, get() { return swagger; diff --git a/src/open-api/utils.ts b/src/open-api/utils.ts index c812e492..ac5b0f41 100644 --- a/src/open-api/utils.ts +++ b/src/open-api/utils.ts @@ -14,6 +14,9 @@ export function mapToPrimitive(type: string) { Boolean: { type: 'boolean', }, + ID: { + type: 'string', + }, }; if (formatMap[type]) {