Skip to content

Commit 419f471

Browse files
authored
Add support for nullable (#393)
1 parent 2241c0b commit 419f471

6 files changed

Lines changed: 49 additions & 15 deletions

File tree

demo/examples/petstore.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,7 @@ components:
10371037
required:
10381038
- name
10391039
- photoUrls
1040+
- tags
10401041
discriminator:
10411042
propertyName: petType
10421043
mapping:
@@ -1059,10 +1060,12 @@ components:
10591060
description: The name given to a pet
10601061
type: string
10611062
example: Guru
1063+
nullable: true
10621064
photoUrls:
10631065
description: The list of URL to a cute photos featuring pet
10641066
type: array
10651067
maxItems: 20
1068+
nullable: true
10661069
xml:
10671070
name: photoUrl
10681071
wrapped: true
@@ -1076,6 +1079,7 @@ components:
10761079
description: Tags attached to the pet
10771080
type: array
10781081
minItems: 1
1082+
nullable: true
10791083
xml:
10801084
name: tag
10811085
wrapped: true
@@ -1100,6 +1104,7 @@ components:
11001104
Tag:
11011105
title: tag
11021106
type: object
1107+
nullable: true
11031108
properties:
11041109
id:
11051110
description: Tag ID

packages/docusaurus-plugin-openapi-docs/src/markdown/createRequestSchema.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,15 @@ function createDetailsNode(
414414
style: { opacity: "0.6" },
415415
children: ` ${schemaName}`,
416416
}),
417+
guard(schema.nullable && schema.nullable === true, () => [
418+
create("strong", {
419+
style: {
420+
fontSize: "var(--ifm-code-font-size)",
421+
color: "var(--openapi-nullable)",
422+
},
423+
children: " nullable",
424+
}),
425+
]),
417426
guard(
418427
Array.isArray(required)
419428
? required.includes(name)
@@ -597,11 +606,9 @@ function createEdges({
597606
collapsible: false,
598607
name,
599608
required: Array.isArray(required) ? required.includes(name) : required,
600-
deprecated: mergedSchemas.deprecated,
601-
schemaDescription: mergedSchemas.description,
602609
schemaName: schemaName,
603610
qualifierMessage: getQualifierMessage(schema),
604-
defaultValue: mergedSchemas.default,
611+
schema: mergedSchemas,
605612
});
606613
}
607614

@@ -631,11 +638,9 @@ function createEdges({
631638
collapsible: false,
632639
name,
633640
required: Array.isArray(required) ? required.includes(name) : required,
634-
deprecated: schema.deprecated,
635-
schemaDescription: schema.description,
636641
schemaName: schemaName,
637642
qualifierMessage: getQualifierMessage(schema),
638-
defaultValue: schema.default,
643+
schema: schema,
639644
});
640645
}
641646

packages/docusaurus-plugin-openapi-docs/src/markdown/createResponseSchema.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,15 @@ function createDetailsNode(
419419
style: { opacity: "0.6" },
420420
children: ` ${schemaName}`,
421421
}),
422+
guard(schema.nullable && schema.nullable === true, () => [
423+
create("strong", {
424+
style: {
425+
fontSize: "var(--ifm-code-font-size)",
426+
color: "var(--openapi-nullable)",
427+
},
428+
children: " nullable",
429+
}),
430+
]),
422431
guard(schema.required && schema.required === true, () => [
423432
create("strong", {
424433
style: {
@@ -597,11 +606,9 @@ function createEdges({
597606
collapsible: false,
598607
name,
599608
required: false,
600-
deprecated: mergedSchemas.deprecated,
601-
schemaDescription: mergedSchemas.description,
602609
schemaName: schemaName,
603610
qualifierMessage: getQualifierMessage(schema),
604-
defaultValue: mergedSchemas.default,
611+
schema: mergedSchemas,
605612
});
606613
}
607614

@@ -631,11 +638,9 @@ function createEdges({
631638
collapsible: false,
632639
name,
633640
required: false,
634-
deprecated: schema.deprecated,
635-
schemaDescription: schema.description,
636641
schemaName: schemaName,
637642
qualifierMessage: getQualifierMessage(schema),
638-
defaultValue: schema.default,
643+
schema: schema,
639644
});
640645
}
641646

packages/docusaurus-theme-openapi-docs/src/theme/SchemaItem/index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@ function SchemaItem({
2323
name,
2424
qualifierMessage,
2525
required,
26-
deprecated,
27-
schemaDescription,
2826
schemaName,
29-
defaultValue,
27+
schema,
3028
}) {
29+
let deprecated;
30+
let schemaDescription;
31+
let defaultValue;
32+
let nullable;
33+
if (schema) {
34+
deprecated = schema.deprecated;
35+
schemaDescription = schema.description;
36+
defaultValue = schema.default;
37+
nullable = schema.nullable;
38+
}
3139
const renderRequired = guard(
3240
Array.isArray(required) ? required.includes(name) : required,
3341
() => <strong className={styles.required}> required</strong>
@@ -37,6 +45,10 @@ function SchemaItem({
3745
<strong className={styles.deprecated}> deprecated</strong>
3846
));
3947

48+
const renderNullable = guard(nullable, () => (
49+
<strong className={styles.nullable}> nullable</strong>
50+
));
51+
4052
const renderSchemaDescription = guard(schemaDescription, (description) => (
4153
<div>
4254
<ReactMarkdown
@@ -80,6 +92,7 @@ function SchemaItem({
8092
<div>
8193
<strong className={deprecated && styles.strikethrough}>{name}</strong>
8294
<span className={styles.schemaName}> {schemaName}</span>
95+
{renderNullable}
8396
{!deprecated && renderRequired}
8497
{renderDeprecated}
8598
{renderQualifierMessage}

packages/docusaurus-theme-openapi-docs/src/theme/SchemaItem/styles.module.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
color: var(--openapi-deprecated);
2929
}
3030

31+
.nullable {
32+
font-size: var(--ifm-code-font-size);
33+
color: var(--openapi-nullable);
34+
}
35+
3136
.strikethrough {
3237
text-decoration: line-through;
3338
}

packages/docusaurus-theme-openapi-docs/src/theme/styles.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
:root {
99
--openapi-required: var(--ifm-color-danger);
1010
--openapi-deprecated: var(--ifm-color-warning);
11+
--openapi-nullable: var(--ifm-color-info);
1112
--openapi-code-blue: var(--ifm-color-info);
1213
--openapi-code-red: var(--ifm-color-danger);
1314
--openapi-code-orange: var(--ifm-color-warning);

0 commit comments

Comments
 (0)