Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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. #267

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions packages/docusaurus-plugin-openapi/src/markdown/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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(
Expand Down
22 changes: 18 additions & 4 deletions packages/docusaurus-plugin-openapi/src/openapi/createExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface OASTypeToTypeMap {
boolean: boolean;
object: any;
array: any[];
null: string;
}

type Primitives = {
Expand Down Expand Up @@ -43,6 +44,9 @@ const primitives: Primitives = {
default: (schema) =>
typeof schema.default === "boolean" ? schema.default : true,
},
null: {
default: () => "null",
},
object: {},
array: {},
};
Expand Down Expand Up @@ -115,14 +119,24 @@ export const sampleFromSchema = (schema: SchemaObject = {}): any => {
return primitive(schema);
};

function primitive(schema: SchemaObject = {}) {
function primitive(schema: SchemaObject = {}): string {
let { type, format } = schema;

if (type === undefined) {
return;
if (type instanceof Array) {
return type
.map((type) => primitive({ type, format }))
.reduce((acc, cur) => (acc ? `${acc} | ${cur}` : `${cur}`), "");
}

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;
Expand Down
10 changes: 9 additions & 1 deletion packages/docusaurus-plugin-openapi/src/openapi/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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[];
Expand Down
Loading