diff --git a/package.json b/package.json index 8e92529a..aea54f86 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "openapi-typescript-codegen", - "version": "0.7.2", + "version": "0.8.0", "description": "Library that generates Typescript clients based on the OpenAPI specification.", "author": "Ferdi Koomen", "homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen", diff --git a/samples/spec/v2.json b/samples/spec/v2.json index 0869e8d2..46b4183a 100644 --- a/samples/spec/v2.json +++ b/samples/spec/v2.json @@ -978,6 +978,13 @@ "Order": { "type": "object", "properties": { + "bool": { + "description": "Simple boolean enum", + "type": "boolean", + "enum": [ + true + ] + }, "id": { "type": "integer", "format": "int64" diff --git a/src/openApi/v2/parser/getEnum.ts b/src/openApi/v2/parser/getEnum.ts index be285179..650228bf 100644 --- a/src/openApi/v2/parser/getEnum.ts +++ b/src/openApi/v2/parser/getEnum.ts @@ -18,7 +18,7 @@ export function getEnum(values?: (string | number)[]): Enum[] { }; } return { - name: value + name: String(value) .replace(/\W+/g, '_') .replace(/^(\d+)/g, '_$1') .replace(/([a-z])([A-Z]+)/g, '$1_$2') diff --git a/src/openApi/v2/parser/getModel.ts b/src/openApi/v2/parser/getModel.ts index 407dda28..81ce19c2 100644 --- a/src/openApi/v2/parser/getModel.ts +++ b/src/openApi/v2/parser/getModel.ts @@ -53,7 +53,7 @@ export function getModel(openApi: OpenApi, definition: OpenApiSchema, isDefiniti return model; } - if (definition.enum) { + if (definition.enum && definition.type !== 'boolean') { const enumerators = getEnum(definition.enum); const extendedEnumerators = extendEnum(enumerators, definition); if (extendedEnumerators.length) { diff --git a/src/openApi/v3/parser/getEnum.ts b/src/openApi/v3/parser/getEnum.ts index be285179..650228bf 100644 --- a/src/openApi/v3/parser/getEnum.ts +++ b/src/openApi/v3/parser/getEnum.ts @@ -18,7 +18,7 @@ export function getEnum(values?: (string | number)[]): Enum[] { }; } return { - name: value + name: String(value) .replace(/\W+/g, '_') .replace(/^(\d+)/g, '_$1') .replace(/([a-z])([A-Z]+)/g, '$1_$2') diff --git a/src/openApi/v3/parser/getModel.ts b/src/openApi/v3/parser/getModel.ts index 063bfe65..d6b84b48 100644 --- a/src/openApi/v3/parser/getModel.ts +++ b/src/openApi/v3/parser/getModel.ts @@ -55,7 +55,7 @@ export function getModel(openApi: OpenApi, definition: OpenApiSchema, isDefiniti return model; } - if (definition.enum) { + if (definition.enum && definition.type !== 'boolean') { const enumerators = getEnum(definition.enum); const extendedEnumerators = extendEnum(enumerators, definition); if (extendedEnumerators.length) { diff --git a/test/spec/v2.json b/test/spec/v2.json index dc5f53dd..35dba559 100644 --- a/test/spec/v2.json +++ b/test/spec/v2.json @@ -490,6 +490,71 @@ } } }, + "/api/v{api-version}/collectionFormat": { + "get": { + "tags": [ + "CollectionFormat" + ], + "operationId": "CollectionFormat", + "parameters": [ + { + "description": "This is an array parameter that is send as csv format (comma-separated values)", + "name": "parameterArrayCSV", + "in": "query", + "required": true, + "type": "array", + "items": { + "type": "string" + }, + "collectionFormat": "csv" + }, + { + "description": "This is an array parameter that is send as ssv format (space-separated values)", + "name": "parameterArraySSV", + "in": "query", + "required": true, + "type": "array", + "items": { + "type": "string" + }, + "collectionFormat": "ssv" + }, + { + "description": "This is an array parameter that is send as tsv format (tab-separated values)", + "name": "parameterArrayTSV", + "in": "query", + "required": true, + "type": "array", + "items": { + "type": "string" + }, + "collectionFormat": "tsv" + }, + { + "description": "This is an array parameter that is send as pipes format (pipe-separated values)", + "name": "parameterArrayMulti", + "in": "query", + "required": true, + "type": "array", + "items": { + "type": "string" + }, + "collectionFormat": "pipes" + }, + { + "description": "This is an array parameter that is send as multi format (multiple parameter instances)", + "name": "parameterArrayMulti", + "in": "query", + "required": true, + "type": "array", + "items": { + "type": "string" + }, + "collectionFormat": "multi" + } + ] + } + }, "/api/v{api-version}/types": { "get": { "tags": [ @@ -932,6 +997,13 @@ "500 foo.bar", "600 foo&bar" ] + }, + "bool": { + "description": "Simple boolean enum", + "type": "boolean", + "enum": [ + true + ] } } }, diff --git a/test/spec/v3.json b/test/spec/v3.json index fb605ac3..bc2b1e41 100644 --- a/test/spec/v3.json +++ b/test/spec/v3.json @@ -712,6 +712,86 @@ } } }, + "/api/v{api-version}/collectionFormat": { + "get": { + "tags": [ + "CollectionFormat" + ], + "operationId": "CollectionFormat", + "parameters": [ + { + "description": "This is an array parameter that is send as csv format (comma-separated values)", + "name": "parameterArrayCSV", + "in": "query", + "required": true, + "nullable": true, + "schema": { + "type": "array", + "items": { + "type": "string" + } + }, + "collectionFormat": "csv" + }, + { + "description": "This is an array parameter that is send as ssv format (space-separated values)", + "name": "parameterArraySSV", + "in": "query", + "required": true, + "nullable": true, + "schema": { + "type": "array", + "items": { + "type": "string" + } + }, + "collectionFormat": "ssv" + }, + { + "description": "This is an array parameter that is send as tsv format (tab-separated values)", + "name": "parameterArrayTSV", + "in": "query", + "required": true, + "nullable": true, + "schema": { + "type": "array", + "items": { + "type": "string" + } + }, + "collectionFormat": "tsv" + }, + { + "description": "This is an array parameter that is send as pipes format (pipe-separated values)", + "name": "parameterArrayMulti", + "in": "query", + "required": true, + "nullable": true, + "schema": { + "type": "array", + "items": { + "type": "string" + } + }, + "collectionFormat": "pipes" + }, + { + "description": "This is an array parameter that is send as multi format (multiple parameter instances)", + "name": "parameterArrayMulti", + "in": "query", + "required": true, + "nullable": true, + "schema": { + "type": "array", + "items": { + "type": "string" + } + }, + "collectionFormat": "multi" + } + ] + } + }, "/api/v{api-version}/types": { "get": { "tags": [ @@ -1390,6 +1470,13 @@ "500 foo.bar", "600 foo&bar" ] + }, + "bool": { + "description": "Simple boolean enum", + "type": "boolean", + "enum": [ + true + ] } } },