Skip to content

Commit

Permalink
fix: 修正 nullable 的升级兼容
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudcome committed Aug 5, 2024
1 parent 68557d5 commit c565fdc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
21 changes: 19 additions & 2 deletions src/migrations/openapi-3_0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,34 @@ import { isArray, isBoolean, isUndefined } from '../utils/type-is';

function migSchema(schema: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject): OpenAPIV3_1.SchemaObject | OpenAPIV3_1.ReferenceObject {
if (isUndefined(schema)) return schema;
if ('$ref' in schema) return schema;

if ('$ref' in schema) {
if (schema.nullable) {
return {
oneOf: [{ $ref: schema.$ref }, { type: 'null' }],
};
}

return schema;
}

const { type, nullable, properties, additionalProperties, allOf, oneOf, anyOf, ...rest } = schema;

if (allOf || oneOf || anyOf) {
return {
const schemaOf = {
...rest,
allOf: allOf && allOf.map(migSchema),
oneOf: oneOf && oneOf.map(migSchema),
anyOf: anyOf && anyOf.map(migSchema),
};

if (nullable) {
return {
oneOf: [schemaOf, { type: 'null' }],
};
}

return schemaOf;
}

if (type === 'array') {
Expand Down
12 changes: 6 additions & 6 deletions src/printer/Schemata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class Schemata {
const { type, allOf, oneOf, anyOf } = schema;
const comments = JsDoc.fromSchema(schema);

if (allOf) {
if (allOf && allOf.length > 0) {
return {
comments,
type: withGroup(
Expand All @@ -41,7 +41,7 @@ export class Schemata {
};
}

if (oneOf) {
if (oneOf && oneOf.length > 0) {
return {
comments,
type: withGroup(
Expand All @@ -52,14 +52,14 @@ export class Schemata {
};
}

if (anyOf) {
if (anyOf && anyOf.length > 0) {
return {
comments,
type: withGroup(
anyOf.map((s) => this.toString(s)),
',',
'AnyOf<',
'>',
'AnyOf<[',
']>',
),
required: false,
};
Expand Down Expand Up @@ -196,7 +196,7 @@ export class Schemata {
maxItems,
},
// type: withNullable(explicit ? `[${subTypes.join(',')}]` : `((${subTypes.join('|')})[])`, schema.nullable),
type: this.toString(schema.items),
type: this.toString(schema.items) + '[]',
required: false,
};
}
Expand Down
1 change: 1 addition & 0 deletions src/types/openapi-3_0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export interface XMLObject {
}
export interface ReferenceObject {
$ref: string;
nullable?: boolean;
}
export interface ExampleObject {
summary?: string;
Expand Down

0 comments on commit c565fdc

Please sign in to comment.