Skip to content

Commit 66163db

Browse files
authored
feat: support null type (PaloAltoNetworks#1155)
1 parent 912f7d6 commit 66163db

File tree

6 files changed

+40
-8
lines changed

6 files changed

+40
-8
lines changed

demo/examples/tests/anyOf.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ paths:
1919
- type: string
2020
- type: integer
2121
- type: boolean
22+
- type: null
2223
```
2324
responses:
2425
"200":
@@ -30,6 +31,7 @@ paths:
3031
- type: string
3132
- type: integer
3233
- type: boolean
34+
- type: "null"
3335

3436
/anyof-oneof:
3537
get:

demo/examples/tests/oneOf.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ paths:
2222
- type: string
2323
- type: number
2424
- type: boolean
25+
- type: null
2526
```
2627
responses:
2728
"200":
@@ -36,6 +37,7 @@ paths:
3637
- type: string
3738
- type: number
3839
- type: boolean
40+
- type: "null"
3941

4042
/oneof-complex-types:
4143
get:

packages/docusaurus-plugin-openapi-docs/src/openapi/createRequestExample.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ interface OASTypeToTypeMap {
1818
boolean: boolean;
1919
object: any;
2020
array: any[];
21+
null: string | null;
2122
}
2223

2324
type Primitives = {
@@ -50,6 +51,9 @@ const primitives: Primitives = {
5051
},
5152
object: {},
5253
array: {},
54+
null: {
55+
default: () => "null",
56+
},
5357
};
5458

5559
function sampleRequestFromProp(name: string, prop: any, obj: any): any {

packages/docusaurus-plugin-openapi-docs/src/openapi/createResponseExample.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ interface OASTypeToTypeMap {
1818
boolean: boolean;
1919
object: any;
2020
array: any[];
21+
null: string | null;
2122
}
2223

2324
type Primitives = {
@@ -50,6 +51,9 @@ const primitives: Primitives = {
5051
},
5152
object: {},
5253
array: {},
54+
null: {
55+
default: () => "null",
56+
},
5357
};
5458

5559
function sampleResponseFromProp(name: string, prop: any, obj: any): any {

packages/docusaurus-plugin-openapi-docs/src/openapi/types.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
* LICENSE file in the root directory of this source tree.
66
* ========================================================================== */
77

8-
import type { JSONSchema4, JSONSchema6, JSONSchema7 } from "json-schema";
8+
import type {
9+
JSONSchema4,
10+
JSONSchema6,
11+
JSONSchema7,
12+
JSONSchema7TypeName,
13+
} from "json-schema";
914

1015
interface Map<T> {
1116
[key: string]: T;
@@ -325,6 +330,7 @@ export interface ReferenceObject {
325330
}
326331

327332
export type JSONSchema = JSONSchema4 | JSONSchema6 | JSONSchema7;
333+
export type SchemaType = JSONSchema7TypeName;
328334
export type SchemaObject = Omit<
329335
JSONSchema,
330336
| "type"
@@ -337,7 +343,7 @@ export type SchemaObject = Omit<
337343
| "additionalProperties"
338344
> & {
339345
// OpenAPI specific overrides
340-
type?: "string" | "number" | "integer" | "boolean" | "object" | "array";
346+
type?: SchemaType;
341347
allOf?: SchemaObject[];
342348
oneOf?: SchemaObject[];
343349
anyOf?: SchemaObject[];
@@ -371,7 +377,7 @@ export type SchemaObjectWithRef = Omit<
371377
| "additionalProperties"
372378
> & {
373379
// OpenAPI specific overrides
374-
type?: "string" | "number" | "integer" | "boolean" | "object" | "array";
380+
type?: SchemaType;
375381
allOf?: (SchemaObject | ReferenceObject)[];
376382
oneOf?: (SchemaObject | ReferenceObject)[];
377383
anyOf?: (SchemaObject | ReferenceObject)[];

packages/docusaurus-theme-openapi-docs/src/theme/Schema/index.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import {
2121
getQualifierMessage,
2222
getSchemaName,
2323
} from "docusaurus-plugin-openapi-docs/lib/markdown/schema";
24-
import { SchemaObject } from "docusaurus-plugin-openapi-docs/lib/openapi/types";
24+
import {
25+
SchemaObject,
26+
SchemaType,
27+
} from "docusaurus-plugin-openapi-docs/lib/openapi/types";
2528
import isEmpty from "lodash/isEmpty";
2629

2730
// eslint-disable-next-line import/no-extraneous-dependencies
@@ -122,10 +125,7 @@ const AnyOneOf: React.FC<SchemaProps> = ({ schema, schemaType }) => {
122125
value={`${index}-item-properties`}
123126
>
124127
{/* Handle primitive types directly */}
125-
{(["string", "number", "integer", "boolean"].includes(
126-
anyOneSchema.type
127-
) ||
128-
anyOneSchema.const) && (
128+
{(isPrimitive(anyOneSchema) || anyOneSchema.const) && (
129129
<SchemaItem
130130
collapsible={false}
131131
name={undefined}
@@ -938,3 +938,17 @@ const SchemaNode: React.FC<SchemaProps> = ({ schema, schemaType }) => {
938938
};
939939

940940
export default SchemaNode;
941+
942+
type PrimitiveSchemaType = Exclude<SchemaType, "object" | "array">;
943+
944+
const PRIMITIVE_TYPES: Record<PrimitiveSchemaType, true> = {
945+
string: true,
946+
number: true,
947+
integer: true,
948+
boolean: true,
949+
null: true,
950+
} as const;
951+
952+
const isPrimitive = (schema: SchemaObject) => {
953+
return PRIMITIVE_TYPES[schema.type as PrimitiveSchemaType];
954+
};

0 commit comments

Comments
 (0)