From ae8d1920a24d6128acd4a12c28e376e2e40bd342 Mon Sep 17 00:00:00 2001 From: "takaoka.daisuke" Date: Thu, 29 May 2025 10:51:02 +0900 Subject: [PATCH 1/2] feat: support null type --- demo/examples/tests/anyOf.yaml | 2 ++ demo/examples/tests/oneOf.yaml | 2 ++ .../src/openapi/createRequestExample.ts | 4 ++++ .../src/openapi/createResponseExample.ts | 4 ++++ .../src/openapi/types.ts | 12 +++++++--- .../src/theme/Schema/index.tsx | 24 +++++++++++++++---- 6 files changed, 40 insertions(+), 8 deletions(-) diff --git a/demo/examples/tests/anyOf.yaml b/demo/examples/tests/anyOf.yaml index a19581e8f..aa4dcb333 100644 --- a/demo/examples/tests/anyOf.yaml +++ b/demo/examples/tests/anyOf.yaml @@ -19,6 +19,7 @@ paths: - type: string - type: integer - type: boolean + - type: null ``` responses: "200": @@ -30,6 +31,7 @@ paths: - type: string - type: integer - type: boolean + - type: "null" /anyof-oneof: get: diff --git a/demo/examples/tests/oneOf.yaml b/demo/examples/tests/oneOf.yaml index 13fea2d85..9b47efac7 100644 --- a/demo/examples/tests/oneOf.yaml +++ b/demo/examples/tests/oneOf.yaml @@ -22,6 +22,7 @@ paths: - type: string - type: number - type: boolean + - type: null ``` responses: "200": @@ -36,6 +37,7 @@ paths: - type: string - type: number - type: boolean + - type: "null" /oneof-complex-types: get: diff --git a/packages/docusaurus-plugin-openapi-docs/src/openapi/createRequestExample.ts b/packages/docusaurus-plugin-openapi-docs/src/openapi/createRequestExample.ts index 8e0547b0d..f5d4f38a6 100644 --- a/packages/docusaurus-plugin-openapi-docs/src/openapi/createRequestExample.ts +++ b/packages/docusaurus-plugin-openapi-docs/src/openapi/createRequestExample.ts @@ -18,6 +18,7 @@ interface OASTypeToTypeMap { boolean: boolean; object: any; array: any[]; + null: string | null; } type Primitives = { @@ -50,6 +51,9 @@ const primitives: Primitives = { }, object: {}, array: {}, + null: { + default: () => "null", + }, }; function sampleRequestFromProp(name: string, prop: any, obj: any): any { diff --git a/packages/docusaurus-plugin-openapi-docs/src/openapi/createResponseExample.ts b/packages/docusaurus-plugin-openapi-docs/src/openapi/createResponseExample.ts index fba987517..f33cf9d5e 100644 --- a/packages/docusaurus-plugin-openapi-docs/src/openapi/createResponseExample.ts +++ b/packages/docusaurus-plugin-openapi-docs/src/openapi/createResponseExample.ts @@ -18,6 +18,7 @@ interface OASTypeToTypeMap { boolean: boolean; object: any; array: any[]; + null: string | null; } type Primitives = { @@ -50,6 +51,9 @@ const primitives: Primitives = { }, object: {}, array: {}, + null: { + default: () => "null", + }, }; function sampleResponseFromProp(name: string, prop: any, obj: any): any { diff --git a/packages/docusaurus-plugin-openapi-docs/src/openapi/types.ts b/packages/docusaurus-plugin-openapi-docs/src/openapi/types.ts index b578866b2..317f3cfc2 100644 --- a/packages/docusaurus-plugin-openapi-docs/src/openapi/types.ts +++ b/packages/docusaurus-plugin-openapi-docs/src/openapi/types.ts @@ -5,7 +5,12 @@ * LICENSE file in the root directory of this source tree. * ========================================================================== */ -import type { JSONSchema4, JSONSchema6, JSONSchema7 } from "json-schema"; +import type { + JSONSchema4, + JSONSchema6, + JSONSchema7, + JSONSchema7TypeName, +} from "json-schema"; interface Map { [key: string]: T; @@ -325,6 +330,7 @@ export interface ReferenceObject { } export type JSONSchema = JSONSchema4 | JSONSchema6 | JSONSchema7; +export type SchemaType = JSONSchema7TypeName; export type SchemaObject = Omit< JSONSchema, | "type" @@ -337,7 +343,7 @@ export type SchemaObject = Omit< | "additionalProperties" > & { // OpenAPI specific overrides - type?: "string" | "number" | "integer" | "boolean" | "object" | "array"; + type?: SchemaType; allOf?: SchemaObject[]; oneOf?: SchemaObject[]; anyOf?: SchemaObject[]; @@ -371,7 +377,7 @@ export type SchemaObjectWithRef = Omit< | "additionalProperties" > & { // OpenAPI specific overrides - type?: "string" | "number" | "integer" | "boolean" | "object" | "array"; + type?: SchemaType; allOf?: (SchemaObject | ReferenceObject)[]; oneOf?: (SchemaObject | ReferenceObject)[]; anyOf?: (SchemaObject | ReferenceObject)[]; diff --git a/packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx b/packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx index 1694f5c6f..5da93f505 100644 --- a/packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx +++ b/packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx @@ -21,7 +21,10 @@ import { getQualifierMessage, getSchemaName, } from "docusaurus-plugin-openapi-docs/lib/markdown/schema"; -import { SchemaObject } from "docusaurus-plugin-openapi-docs/lib/openapi/types"; +import { + SchemaObject, + SchemaType, +} from "docusaurus-plugin-openapi-docs/lib/openapi/types"; import isEmpty from "lodash/isEmpty"; // eslint-disable-next-line import/no-extraneous-dependencies @@ -122,10 +125,7 @@ const AnyOneOf: React.FC = ({ schema, schemaType }) => { value={`${index}-item-properties`} > {/* Handle primitive types directly */} - {(["string", "number", "integer", "boolean"].includes( - anyOneSchema.type - ) || - anyOneSchema.const) && ( + {(isPrimitive(anyOneSchema) || anyOneSchema.const) && ( = ({ schema, schemaType }) => { }; export default SchemaNode; + +type PrimitiveSchemaType = Exclude; + +const PRIMITIVE_TYPES: Record = { + string: true, + number: true, + integer: true, + boolean: true, + null: true, +} as const; + +const isPrimitive = (schema: SchemaObject) => { + return PRIMITIVE_TYPES[schema.type as PrimitiveSchemaType]; +}; From 149dfecfbaf57f353e62158f1d24266168d5d0a4 Mon Sep 17 00:00:00 2001 From: "takaoka.daisuke" Date: Fri, 13 Jun 2025 14:24:32 +0900 Subject: [PATCH 2/2] fix: fix tab label --- .../docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx b/packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx index 2a6fa60a7..cc7bc3863 100644 --- a/packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx +++ b/packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx @@ -116,7 +116,7 @@ const AnyOneOf: React.FC = ({ schema, schemaType }) => { {schema[type]?.map((anyOneSchema: any, index: number) => { - const label = anyOneSchema.title || `MOD${index + 1}`; + const label = anyOneSchema.title || anyOneSchema.type; return ( // @ts-ignore