openapi-typescript
Advanced tools
Comparing version 6.3.6 to 6.3.7
@@ -36,7 +36,8 @@ import { escObjKey, escStr, getEntries, getSchemaObjectComment, indent, parseRef, tsArrayOf, tsIntersectionOf, tsOmit, tsOneOf, tsOptionalProperty, tsReadonly, tsTupleOf, tsUnionOf, tsWithRequired } from "../utils.js"; | ||
} | ||
if (schemaObject.enum) { | ||
if (typeof schemaObject === "object" && !!schemaObject.enum && schemaObject.type !== "object") { | ||
let items = schemaObject.enum; | ||
if ("type" in schemaObject) { | ||
if (schemaObject.type === "string" || (Array.isArray(schemaObject.type) && schemaObject.type.includes("string"))) | ||
if (schemaObject.type === "string" || (Array.isArray(schemaObject.type) && schemaObject.type.includes("string"))) { | ||
items = items.map((t) => escStr(t)); | ||
} | ||
} | ||
@@ -48,7 +49,15 @@ else { | ||
} | ||
if ("oneOf" in schemaObject && !schemaObject.oneOf.some((t) => "$ref" in t && ctx.discriminators[t.$ref])) { | ||
const maybeTypes = schemaObject.oneOf.map((item) => transformSchemaObject(item, { path, ctx })); | ||
if (maybeTypes.some((t) => typeof t === "string" && t.includes("{"))) | ||
return tsOneOf(...maybeTypes); | ||
return tsUnionOf(...maybeTypes); | ||
const oneOf = ((typeof schemaObject === "object" && schemaObject.oneOf) || schemaObject.enum || undefined); | ||
if (oneOf && !oneOf.some((t) => "$ref" in t && ctx.discriminators[t.$ref])) { | ||
const oneOfNormalized = oneOf.map((item) => transformSchemaObject(item, { path, ctx })); | ||
const oneOfTypes = oneOfNormalized.some((t) => typeof t === "string" && t.includes("{")) ? tsOneOf(...oneOfNormalized) : tsUnionOf(...oneOfNormalized); | ||
if ("type" in schemaObject && schemaObject.type === "object") { | ||
const coreSchema = { ...schemaObject }; | ||
delete coreSchema.oneOf; | ||
delete coreSchema.enum; | ||
return tsIntersectionOf(transformSchemaObject(coreSchema, { path, ctx }), oneOfTypes); | ||
} | ||
else { | ||
return oneOfTypes; | ||
} | ||
} | ||
@@ -55,0 +64,0 @@ if ("type" in schemaObject) { |
@@ -223,2 +223,3 @@ /// <reference types="node" resolution-mode="require"/> | ||
type: "string"; | ||
enum?: (string | ReferenceObject)[]; | ||
} | ||
@@ -229,2 +230,3 @@ export interface NumberSubtype { | ||
maximum?: number; | ||
enum?: (number | ReferenceObject)[]; | ||
} | ||
@@ -235,2 +237,3 @@ export interface IntegerSubtype { | ||
maximum?: number; | ||
enum?: (number | ReferenceObject)[]; | ||
} | ||
@@ -243,5 +246,7 @@ export interface ArraySubtype { | ||
maxItems?: number; | ||
enum?: (SchemaObject | ReferenceObject)[]; | ||
} | ||
export interface BooleanSubtype { | ||
type: "boolean"; | ||
enum?: (boolean | ReferenceObject)[]; | ||
} | ||
@@ -260,2 +265,3 @@ export interface NullSubtype { | ||
anyOf?: (SchemaObject | ReferenceObject)[]; | ||
enum?: (SchemaObject | ReferenceObject)[]; | ||
} | ||
@@ -262,0 +268,0 @@ export interface DiscriminatorObject { |
{ | ||
"name": "openapi-typescript", | ||
"description": "Generate TypeScript types from Swagger OpenAPI specs", | ||
"version": "6.3.6", | ||
"version": "6.3.7", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Drew Powers", |
@@ -54,7 +54,9 @@ import type { GlobalContext, ReferenceObject, SchemaObject } from "../types.js"; | ||
// enum (valid for any type) | ||
if (schemaObject.enum) { | ||
let items = schemaObject.enum as any[]; | ||
// enum (valid for any type, but for objects, treat as oneOf below) | ||
if (typeof schemaObject === "object" && !!(schemaObject as any).enum && (schemaObject as any).type !== "object") { | ||
let items = (schemaObject as any).enum as any[]; | ||
if ("type" in schemaObject) { | ||
if (schemaObject.type === "string" || (Array.isArray(schemaObject.type) && schemaObject.type.includes("string" as any))) items = items.map((t) => escStr(t)); | ||
if (schemaObject.type === "string" || (Array.isArray(schemaObject.type) && schemaObject.type.includes("string" as any))) { | ||
items = items.map((t) => escStr(t)); | ||
} | ||
} | ||
@@ -69,6 +71,16 @@ // if no type, assume "string" | ||
// oneOf (no discriminator) | ||
if ("oneOf" in schemaObject && !schemaObject.oneOf.some((t) => "$ref" in t && ctx.discriminators[t.$ref])) { | ||
const maybeTypes = schemaObject.oneOf.map((item) => transformSchemaObject(item, { path, ctx })); | ||
if (maybeTypes.some((t) => typeof t === "string" && t.includes("{"))) return tsOneOf(...maybeTypes); // OneOf<> helper needed if any objects present ("{") | ||
return tsUnionOf(...maybeTypes); // otherwise, TS union works for primitives | ||
const oneOf = ((typeof schemaObject === "object" && (schemaObject as any).oneOf) || (schemaObject as any).enum || undefined) as (SchemaObject | ReferenceObject)[] | undefined; // note: for objects, treat enum as oneOf | ||
if (oneOf && !oneOf.some((t) => "$ref" in t && ctx.discriminators[t.$ref])) { | ||
const oneOfNormalized = oneOf.map((item) => transformSchemaObject(item, { path, ctx })); | ||
// OneOf<> helper needed if any objects present ("{") | ||
const oneOfTypes = oneOfNormalized.some((t) => typeof t === "string" && t.includes("{")) ? tsOneOf(...oneOfNormalized) : tsUnionOf(...oneOfNormalized); | ||
if ("type" in schemaObject && schemaObject.type === "object") { | ||
const coreSchema = { ...schemaObject }; | ||
delete (coreSchema as any).oneOf; | ||
delete coreSchema.enum; | ||
return tsIntersectionOf(transformSchemaObject(coreSchema, { path, ctx }), oneOfTypes); | ||
} else { | ||
return oneOfTypes; | ||
} | ||
} | ||
@@ -75,0 +87,0 @@ |
@@ -450,2 +450,3 @@ import type { URL } from "node:url"; | ||
type: "string"; | ||
enum?: (string | ReferenceObject)[]; | ||
} | ||
@@ -457,2 +458,3 @@ | ||
maximum?: number; | ||
enum?: (number | ReferenceObject)[]; | ||
} | ||
@@ -464,2 +466,3 @@ | ||
maximum?: number; | ||
enum?: (number | ReferenceObject)[]; | ||
} | ||
@@ -473,2 +476,3 @@ | ||
maxItems?: number; | ||
enum?: (SchemaObject | ReferenceObject)[]; | ||
} | ||
@@ -478,2 +482,3 @@ | ||
type: "boolean"; | ||
enum?: (boolean | ReferenceObject)[]; | ||
} | ||
@@ -492,2 +497,3 @@ | ||
anyOf?: (SchemaObject | ReferenceObject)[]; | ||
enum?: (SchemaObject | ReferenceObject)[]; | ||
} | ||
@@ -494,0 +500,0 @@ |
Sorry, the diff of this file is not supported yet
304327
4890