From ed463485d4d89e0b6a25704dc3ce83917e0ac295 Mon Sep 17 00:00:00 2001 From: brymko <47984664+brymko@users.noreply.github.com> Date: Tue, 5 Dec 2023 04:52:50 +0100 Subject: [PATCH 1/2] Prevent crash in createExample when a multi variant type is being parsed. All variants are concatinated with '|'. Also added a "null" type wich is not the """null""" type, made sure to extra check against this. --- .../src/openapi/createExample.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/docusaurus-plugin-openapi/src/openapi/createExample.ts b/packages/docusaurus-plugin-openapi/src/openapi/createExample.ts index df85aaca..b4108441 100644 --- a/packages/docusaurus-plugin-openapi/src/openapi/createExample.ts +++ b/packages/docusaurus-plugin-openapi/src/openapi/createExample.ts @@ -43,6 +43,9 @@ const primitives: Primitives = { default: (schema) => typeof schema.default === "boolean" ? schema.default : true, }, + null: { + default: () => "null", + }, object: {}, array: {}, }; @@ -118,11 +121,21 @@ export const sampleFromSchema = (schema: SchemaObject = {}): any => { function primitive(schema: SchemaObject = {}) { let { type, format } = schema; - if (type === undefined) { + if (type instanceof Array) { + return type + .map(type => primitive({ type, format })) + .reduce((acc, cur) => acc ? `${acc} | ${cur}` : `${cur}`, null) + } + + if (type === undefined || type === null ) { return; } - let fn = primitives[type].default; + let fn = primitives[type]?.default; + + if (fn === undefined) { + return "Unknown Type: " + type; + } if (format !== undefined) { fn = primitives[type][format] || fn; From a604750fc5fc459342a4b4922ee4aea9e7a846b8 Mon Sep 17 00:00:00 2001 From: brymko <47984664+brymko@users.noreply.github.com> Date: Tue, 5 Dec 2023 05:41:03 +0100 Subject: [PATCH 2/2] fixed tsc compile & lint for previous commit --- .../src/markdown/schema.ts | 17 +++++++++++++++-- .../src/openapi/createExample.ts | 11 ++++++----- .../src/openapi/types.ts | 10 +++++++++- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/packages/docusaurus-plugin-openapi/src/markdown/schema.ts b/packages/docusaurus-plugin-openapi/src/markdown/schema.ts index 09b9f6da..3bf42927 100644 --- a/packages/docusaurus-plugin-openapi/src/markdown/schema.ts +++ b/packages/docusaurus-plugin-openapi/src/markdown/schema.ts @@ -5,7 +5,20 @@ * LICENSE file in the root directory of this source tree. * ========================================================================== */ -import { SchemaObject } from "../openapi/types"; +import { SchemaObject, PrimitiveSchemaObjectTypes } from "../openapi/types"; + +function typeToString( + type: PrimitiveSchemaObjectTypes | PrimitiveSchemaObjectTypes[] | undefined +): string { + if (type === undefined) { + return "schema.type is not defined"; + } + if (type instanceof Array) { + return type.reduce((acc, cur) => (acc ? `${acc} | ${cur}` : cur), ""); + } + + return type; +} function prettyName(schema: SchemaObject, circular?: boolean) { if (schema.$ref) { @@ -26,7 +39,7 @@ function prettyName(schema: SchemaObject, circular?: boolean) { return schema.xml?.name ?? schema.type; } - return schema.title ?? schema.type; + return schema.title ?? typeToString(schema.type); } export function getSchemaName( diff --git a/packages/docusaurus-plugin-openapi/src/openapi/createExample.ts b/packages/docusaurus-plugin-openapi/src/openapi/createExample.ts index b4108441..bdbba2cd 100644 --- a/packages/docusaurus-plugin-openapi/src/openapi/createExample.ts +++ b/packages/docusaurus-plugin-openapi/src/openapi/createExample.ts @@ -14,6 +14,7 @@ interface OASTypeToTypeMap { boolean: boolean; object: any; array: any[]; + null: string; } type Primitives = { @@ -118,17 +119,17 @@ export const sampleFromSchema = (schema: SchemaObject = {}): any => { return primitive(schema); }; -function primitive(schema: SchemaObject = {}) { +function primitive(schema: SchemaObject = {}): string { let { type, format } = schema; if (type instanceof Array) { return type - .map(type => primitive({ type, format })) - .reduce((acc, cur) => acc ? `${acc} | ${cur}` : `${cur}`, null) + .map((type) => primitive({ type, format })) + .reduce((acc, cur) => (acc ? `${acc} | ${cur}` : `${cur}`), ""); } - if (type === undefined || type === null ) { - return; + if (type === undefined || type === null) { + return ""; } let fn = primitives[type]?.default; diff --git a/packages/docusaurus-plugin-openapi/src/openapi/types.ts b/packages/docusaurus-plugin-openapi/src/openapi/types.ts index 2ac2ad07..44f483e1 100644 --- a/packages/docusaurus-plugin-openapi/src/openapi/types.ts +++ b/packages/docusaurus-plugin-openapi/src/openapi/types.ts @@ -308,6 +308,14 @@ export interface ReferenceObject { } export type JSONSchema = JSONSchema4 | JSONSchema6 | JSONSchema7; +export type PrimitiveSchemaObjectTypes = + | "string" + | "number" + | "integer" + | "boolean" + | "object" + | "array" + | "null"; export type SchemaObject = Omit< JSONSchema, | "type" @@ -320,7 +328,7 @@ export type SchemaObject = Omit< | "additionalProperties" > & { // OpenAPI specific overrides - type?: "string" | "number" | "integer" | "boolean" | "object" | "array"; + type?: PrimitiveSchemaObjectTypes | PrimitiveSchemaObjectTypes[]; allOf?: SchemaObject[]; oneOf?: SchemaObject[]; anyOf?: SchemaObject[];