openapi-typescript
Advanced tools
Comparing version 7.4.1 to 7.4.2
# openapi-typescript | ||
## 7.4.2 | ||
### Patch Changes | ||
- [#1873](https://github.com/openapi-ts/openapi-typescript/pull/1873) [`c2c396d`](https://github.com/openapi-ts/openapi-typescript/commit/c2c396d8282692f3ac2df50656ebcd9e8615a685) Thanks [@DanDeMicco](https://github.com/DanDeMicco)! - Support for generating enums when enums definition has null value | ||
## 7.4.1 | ||
@@ -4,0 +10,0 @@ |
@@ -35,3 +35,4 @@ import { parseRef } from "@redocly/openapi-core/lib/ref-utils.js"; | ||
!("additionalProperties" in schemaObject)) { | ||
if (options.ctx.enum && schemaObject.enum.every((v) => typeof v === "string" || typeof v === "number")) { | ||
if (options.ctx.enum && | ||
schemaObject.enum.every((v) => typeof v === "string" || typeof v === "number" || v === null)) { | ||
let enumName = parseRef(options.path ?? "").pointer.join("/"); | ||
@@ -43,3 +44,11 @@ enumName = enumName.replace("components/schemas", ""); | ||
})); | ||
const enumType = tsEnum(enumName, schemaObject.enum, metadata, { | ||
let hasNull = false; | ||
const validSchemaEnums = schemaObject.enum.filter((enumValue) => { | ||
if (enumValue === null) { | ||
hasNull = true; | ||
return false; | ||
} | ||
return true; | ||
}); | ||
const enumType = tsEnum(enumName, validSchemaEnums, metadata, { | ||
shouldCache: options.ctx.dedupeEnums, | ||
@@ -51,3 +60,4 @@ export: true, | ||
} | ||
return ts.factory.createTypeReferenceNode(enumType.name); | ||
const ref = ts.factory.createTypeReferenceNode(enumType.name); | ||
return hasNull ? tsUnion([ref, NULL]) : ref; | ||
} | ||
@@ -54,0 +64,0 @@ const enumType = schemaObject.enum.map(tsLiteral); |
{ | ||
"name": "openapi-typescript", | ||
"description": "Convert OpenAPI 3.0 & 3.1 schemas to TypeScript", | ||
"version": "7.4.1", | ||
"version": "7.4.2", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Drew Powers", |
@@ -97,3 +97,6 @@ import { parseRef } from "@redocly/openapi-core/lib/ref-utils.js"; | ||
// hoist enum to top level if string/number enum and option is enabled | ||
if (options.ctx.enum && schemaObject.enum.every((v) => typeof v === "string" || typeof v === "number")) { | ||
if ( | ||
options.ctx.enum && | ||
schemaObject.enum.every((v) => typeof v === "string" || typeof v === "number" || v === null) | ||
) { | ||
let enumName = parseRef(options.path ?? "").pointer.join("/"); | ||
@@ -106,3 +109,14 @@ // allow #/components/schemas to have simpler names | ||
})); | ||
const enumType = tsEnum(enumName, schemaObject.enum as (string | number)[], metadata, { | ||
// enums can contain null values, but dont want to output them | ||
let hasNull = false; | ||
const validSchemaEnums = schemaObject.enum.filter((enumValue) => { | ||
if (enumValue === null) { | ||
hasNull = true; | ||
return false; | ||
} | ||
return true; | ||
}); | ||
const enumType = tsEnum(enumName, validSchemaEnums as (string | number)[], metadata, { | ||
shouldCache: options.ctx.dedupeEnums, | ||
@@ -115,3 +129,4 @@ export: true, | ||
} | ||
return ts.factory.createTypeReferenceNode(enumType.name); | ||
const ref = ts.factory.createTypeReferenceNode(enumType.name); | ||
return hasNull ? tsUnion([ref, NULL]) : ref; | ||
} | ||
@@ -118,0 +133,0 @@ const enumType = schemaObject.enum.map(tsLiteral); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
565483
10575