openapi-typescript
Advanced tools
Comparing version 5.4.1 to 5.4.2
@@ -27,2 +27,3 @@ #!/usr/bin/env node | ||
--default-non-nullable (optional) If a schema object has a default value set, don’t mark it as nullable | ||
--x-nullable-as-nullable (optional) If a schema object has \`x-nullable\` set, treat it as nullable (like \`nullable\` in OpenAPI 3.0.x) | ||
--prettier-config, -c (optional) specify path to Prettier config file | ||
@@ -52,2 +53,3 @@ --raw-schema (optional) Parse as partial schema (raw components) | ||
"defaultNonNullable", | ||
"xNullableAsNullable", | ||
"immutableTypes", | ||
@@ -103,2 +105,3 @@ "contentNever", | ||
defaultNonNullable: flags.defaultNonNullable, | ||
xNullableAsNullable: flags.xNullableAsNullable, | ||
immutableTypes: flags.immutableTypes, | ||
@@ -105,0 +108,0 @@ prettierConfig: flags.prettierConfig, |
@@ -24,2 +24,3 @@ import path from "path"; | ||
defaultNonNullable: options.defaultNonNullable || false, | ||
xNullableAsNullable: options.xNullableAsNullable || false, | ||
formatter: options && typeof options.formatter === "function" ? options.formatter : undefined, | ||
@@ -26,0 +27,0 @@ immutableTypes: options.immutableTypes || false, |
import type { GlobalContext } from "../types.js"; | ||
interface TransformSchemaObjOptions extends GlobalContext { | ||
export interface TransformSchemaObjOptions extends GlobalContext { | ||
required: Set<string>; | ||
@@ -9,3 +9,3 @@ } | ||
export declare function transformOneOf(oneOf: any, options: TransformSchemaObjOptions): string; | ||
export declare function isNodeNullable(node: any, options: TransformSchemaObjOptions): boolean; | ||
export declare function transformSchemaObj(node: any, options: TransformSchemaObjOptions): string; | ||
export {}; |
@@ -12,3 +12,3 @@ import { prepareComment, nodeType, tsArrayOf, tsIntersectionOf, tsPartial, tsReadonly, tsTupleOf, tsUnionOf, parseSingleSimpleValue, } from "../utils.js"; | ||
const v = obj[k]; | ||
const comment = prepareComment(v); | ||
const comment = prepareComment(v, options); | ||
if (comment) | ||
@@ -51,2 +51,5 @@ output += comment; | ||
} | ||
export function isNodeNullable(node, options) { | ||
return node.nullable || (options.xNullableAsNullable && node["x-nullable"]); | ||
} | ||
export function transformSchemaObj(node, options) { | ||
@@ -57,3 +60,3 @@ var _a; | ||
const overriddenType = options.formatter && options.formatter(node); | ||
if (node.nullable) { | ||
if (isNodeNullable(node, options)) { | ||
output += "("; | ||
@@ -78,3 +81,3 @@ } | ||
case "const": { | ||
output += parseSingleSimpleValue(node.const, node.nullable); | ||
output += parseSingleSimpleValue(node.const, isNodeNullable(node, options)); | ||
break; | ||
@@ -85,3 +88,3 @@ } | ||
node.enum.forEach((item) => { | ||
const value = parseSingleSimpleValue(item, node.nullable); | ||
const value = parseSingleSimpleValue(item, isNodeNullable(node, options)); | ||
items.push(value); | ||
@@ -168,3 +171,3 @@ }); | ||
} | ||
if (node.nullable) { | ||
if (isNodeNullable(node, options)) { | ||
output += ") | null"; | ||
@@ -171,0 +174,0 @@ } |
@@ -117,2 +117,3 @@ /// <reference types="node" /> | ||
defaultNonNullable?: boolean; | ||
xNullableAsNullable?: boolean; | ||
prettierConfig?: string; | ||
@@ -135,2 +136,3 @@ rawSchema?: boolean; | ||
defaultNonNullable: boolean; | ||
xNullableAsNullable: boolean; | ||
formatter?: SchemaFormatter; | ||
@@ -137,0 +139,0 @@ immutableTypes: boolean; |
import type { OpenAPI2, OpenAPI3, ReferenceObject } from "./types.js"; | ||
import { type TransformSchemaObjOptions } from "./transform/schema.js"; | ||
declare type CommentObject = { | ||
@@ -11,6 +12,7 @@ const?: boolean; | ||
nullable?: boolean; | ||
"x-nullable"?: boolean; | ||
title?: string; | ||
type: string; | ||
}; | ||
export declare function prepareComment(v: CommentObject): string | void; | ||
export declare function prepareComment(v: CommentObject, options: TransformSchemaObjOptions): string | void; | ||
export declare function comment(text: string): string; | ||
@@ -17,0 +19,0 @@ export declare function parseRef(ref: string): { |
@@ -0,1 +1,2 @@ | ||
import { isNodeNullable } from "./transform/schema.js"; | ||
const COMMENT_RE = /\*\//g; | ||
@@ -9,3 +10,3 @@ const LB_RE = /\r?\n/g; | ||
const FS_RE = /\//g; | ||
export function prepareComment(v) { | ||
export function prepareComment(v, options) { | ||
const commentsArray = []; | ||
@@ -34,3 +35,3 @@ if (v.title) | ||
if (v.enum) { | ||
const canBeNull = v.nullable ? `|${null}` : ""; | ||
const canBeNull = isNodeNullable(v, options) ? `|${null}` : ""; | ||
commentsArray.push(`@enum {${v.type}${canBeNull}}`); | ||
@@ -37,0 +38,0 @@ } |
{ | ||
"name": "openapi-typescript", | ||
"description": "Generate TypeScript types from Swagger OpenAPI specs", | ||
"version": "5.4.1", | ||
"version": "5.4.2", | ||
"engines": { | ||
@@ -6,0 +6,0 @@ "node": ">= 14.0.0" |
@@ -44,2 +44,3 @@ import type { GlobalContext, OpenAPI2, OpenAPI3, SchemaObject, SwaggerToTSOptions } from "./types.js"; | ||
defaultNonNullable: options.defaultNonNullable || false, | ||
xNullableAsNullable: options.xNullableAsNullable || false, | ||
formatter: options && typeof options.formatter === "function" ? options.formatter : undefined, | ||
@@ -46,0 +47,0 @@ immutableTypes: options.immutableTypes || false, |
@@ -15,3 +15,3 @@ import type { GlobalContext } from "../types.js"; | ||
interface TransformSchemaObjOptions extends GlobalContext { | ||
export interface TransformSchemaObjOptions extends GlobalContext { | ||
required: Set<string>; | ||
@@ -36,3 +36,3 @@ } | ||
// 1. Add comment in jsdoc notation | ||
const comment = prepareComment(v); | ||
const comment = prepareComment(v, options); | ||
if (comment) output += comment; | ||
@@ -91,2 +91,6 @@ | ||
export function isNodeNullable(node: any, options: TransformSchemaObjOptions): boolean { | ||
return node.nullable || (options.xNullableAsNullable && node["x-nullable"]); | ||
} | ||
/** Convert schema object to TypeScript */ | ||
@@ -102,3 +106,3 @@ export function transformSchemaObj(node: any, options: TransformSchemaObjOptions): string { | ||
// open nullable | ||
if (node.nullable) { | ||
if (isNodeNullable(node, options)) { | ||
output += "("; | ||
@@ -124,3 +128,3 @@ } | ||
case "const": { | ||
output += parseSingleSimpleValue(node.const, node.nullable); | ||
output += parseSingleSimpleValue(node.const, isNodeNullable(node, options)); | ||
break; | ||
@@ -131,3 +135,3 @@ } | ||
(node.enum as unknown[]).forEach((item) => { | ||
const value = parseSingleSimpleValue(item, node.nullable); | ||
const value = parseSingleSimpleValue(item, isNodeNullable(node, options)); | ||
items.push(value); | ||
@@ -230,3 +234,3 @@ }); | ||
// close nullable | ||
if (node.nullable) { | ||
if (isNodeNullable(node, options)) { | ||
output += ") | null"; | ||
@@ -233,0 +237,0 @@ } |
@@ -133,2 +133,4 @@ import type { URL } from "url"; | ||
defaultNonNullable?: boolean; | ||
/** (optional) Schemas with `x-nullable: true` should generate with `| null`, like `nullable` in OpenAPI 3.0.x */ | ||
xNullableAsNullable?: boolean; | ||
/** (optional) Path to Prettier config */ | ||
@@ -184,2 +186,3 @@ prettierConfig?: string; | ||
defaultNonNullable: boolean; | ||
xNullableAsNullable: boolean; | ||
formatter?: SchemaFormatter; | ||
@@ -186,0 +189,0 @@ immutableTypes: boolean; |
import type { OpenAPI2, OpenAPI3, ReferenceObject } from "./types.js"; | ||
import { isNodeNullable, type TransformSchemaObjOptions } from "./transform/schema.js"; | ||
@@ -12,2 +13,3 @@ type CommentObject = { | ||
nullable?: boolean; // Node information | ||
"x-nullable"?: boolean; // Node information | ||
title?: string; // not jsdoc | ||
@@ -31,3 +33,3 @@ type: string; // Type of node | ||
*/ | ||
export function prepareComment(v: CommentObject): string | void { | ||
export function prepareComment(v: CommentObject, options: TransformSchemaObjOptions): string | void { | ||
const commentsArray: Array<string> = []; | ||
@@ -63,3 +65,3 @@ | ||
if (v.enum) { | ||
const canBeNull = v.nullable ? `|${null}` : ""; | ||
const canBeNull = isNodeNullable(v, options) ? `|${null}` : ""; | ||
commentsArray.push(`@enum {${v.type}${canBeNull}}`); | ||
@@ -66,0 +68,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
197936
2910