Skip to content

Commit 689120d

Browse files
authored
Hide required labels on response schemas (#230)
1 parent 121a75e commit 689120d

File tree

4 files changed

+57
-37
lines changed

4 files changed

+57
-37
lines changed

packages/docusaurus-plugin-openapi/src/markdown/createRequestBodyTable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ interface Props {
1313
}
1414

1515
export function createRequestBodyTable({ title, body }: Props) {
16-
return createSchemaTable({ title, body });
16+
return createSchemaTable({ title, body, type: "request" });
1717
}

packages/docusaurus-plugin-openapi/src/markdown/createSchemaTable.ts

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ interface RowProps {
3838
name: string;
3939
schema: SchemaObject;
4040
required: boolean;
41+
type: "request" | "response";
4142
}
4243

43-
function createRow({ name, schema, required }: RowProps) {
44+
function createRow({ name, schema, required, type }: RowProps) {
4445
return create("tr", {
4546
children: create("td", {
4647
children: [
@@ -49,19 +50,7 @@ function createRow({ name, schema, required }: RowProps) {
4950
style: { opacity: "0.6" },
5051
children: ` ${getSchemaName(schema, true)}`,
5152
}),
52-
guard(required, () => [
53-
create("span", {
54-
style: { opacity: "0.6" },
55-
children: " — ",
56-
}),
57-
create("strong", {
58-
style: {
59-
fontSize: "var(--ifm-code-font-size)",
60-
color: "var(--openapi-required)",
61-
},
62-
children: " REQUIRED",
63-
}),
64-
]),
53+
...parseTitleLabel({ required, type }),
6554
guard(getQualifierMessage(schema), (message) =>
6655
create("div", {
6756
style: { marginTop: "var(--ifm-table-cell-padding)" },
@@ -74,17 +63,17 @@ function createRow({ name, schema, required }: RowProps) {
7463
children: createDescription(description),
7564
})
7665
),
77-
createRows({ schema: schema }),
66+
createRows({ schema: schema, type }),
7867
],
7968
}),
8069
});
8170
}
8271

83-
interface RowsProps {
72+
interface RowsProps extends Pick<RowProps, "type"> {
8473
schema: SchemaObject;
8574
}
8675

87-
function createRows({ schema }: RowsProps): string | undefined {
76+
function createRows({ schema, type }: RowsProps): string | undefined {
8877
// object
8978
if (schema.properties !== undefined) {
9079
return createFullWidthTable({
@@ -100,6 +89,7 @@ function createRows({ schema }: RowsProps): string | undefined {
10089
required: Array.isArray(schema.required)
10190
? schema.required.includes(key)
10291
: false,
92+
type,
10393
})
10494
),
10595
}),
@@ -120,6 +110,7 @@ function createRows({ schema }: RowsProps): string | undefined {
120110
name: key,
121111
schema: val,
122112
required: Array.isArray(required) ? required.includes(key) : false,
113+
type,
123114
})
124115
),
125116
}),
@@ -128,18 +119,18 @@ function createRows({ schema }: RowsProps): string | undefined {
128119

129120
// array
130121
if (schema.items !== undefined) {
131-
return createRows({ schema: schema.items });
122+
return createRows({ schema: schema.items, type });
132123
}
133124

134125
// primitive
135126
return undefined;
136127
}
137128

138-
interface RowsRootProps {
129+
interface RowsRootProps extends Pick<RowProps, "type"> {
139130
schema: SchemaObject;
140131
}
141132

142-
function createRowsRoot({ schema }: RowsRootProps) {
133+
function createRowsRoot({ schema, type }: RowsRootProps) {
143134
// object
144135
if (schema.properties !== undefined) {
145136
return Object.entries(schema.properties).map(([key, val]) =>
@@ -149,6 +140,7 @@ function createRowsRoot({ schema }: RowsRootProps) {
149140
required: Array.isArray(schema.required)
150141
? schema.required.includes(key)
151142
: false,
143+
type,
152144
})
153145
);
154146
}
@@ -161,6 +153,7 @@ function createRowsRoot({ schema }: RowsRootProps) {
161153
name: key,
162154
schema: val,
163155
required: Array.isArray(required) ? required.includes(key) : false,
156+
type,
164157
})
165158
);
166159
}
@@ -174,7 +167,7 @@ function createRowsRoot({ schema }: RowsRootProps) {
174167
style: { opacity: "0.6" },
175168
children: ` ${getSchemaName(schema, true)}`,
176169
}),
177-
createRows({ schema: schema.items }),
170+
createRows({ schema: schema.items, type }),
178171
],
179172
}),
180173
});
@@ -215,9 +208,10 @@ interface Props {
215208
description?: string;
216209
required?: boolean;
217210
};
211+
type: "request" | "response";
218212
}
219213

220-
export function createSchemaTable({ title, body, ...rest }: Props) {
214+
export function createSchemaTable({ title, body, type, ...rest }: Props) {
221215
if (body === undefined || body.content === undefined) {
222216
return undefined;
223217
}
@@ -249,19 +243,7 @@ export function createSchemaTable({ title, body, ...rest }: Props) {
249243
style: { textAlign: "left" },
250244
children: [
251245
`${title} `,
252-
guard(body.required, () => [
253-
create("span", {
254-
style: { opacity: "0.6" },
255-
children: " — ",
256-
}),
257-
create("strong", {
258-
style: {
259-
fontSize: "var(--ifm-code-font-size)",
260-
color: "var(--openapi-required)",
261-
},
262-
children: " REQUIRED",
263-
}),
264-
]),
246+
...parseTitleLabel({ required: body.required, type }),
265247
create("div", {
266248
children: createDescription(body.description),
267249
}),
@@ -270,8 +252,43 @@ export function createSchemaTable({ title, body, ...rest }: Props) {
270252
}),
271253
}),
272254
create("tbody", {
273-
children: createRowsRoot({ schema: firstBody }),
255+
children: createRowsRoot({ schema: firstBody, type }),
274256
}),
275257
],
276258
});
277259
}
260+
261+
const parseTitleLabel = ({
262+
required,
263+
type,
264+
}: {
265+
required?: boolean;
266+
type: Props["type"];
267+
}) => [
268+
guard(required && type === "request", () => [
269+
create("span", {
270+
style: { opacity: "0.6" },
271+
children: " — ",
272+
}),
273+
create("strong", {
274+
style: {
275+
fontSize: "var(--ifm-code-font-size)",
276+
color: "var(--openapi-required)",
277+
},
278+
children: " REQUIRED",
279+
}),
280+
]),
281+
guard(!required && type === "response", () => [
282+
create("span", {
283+
style: { opacity: "0.6" },
284+
children: " — ",
285+
}),
286+
create("strong", {
287+
style: {
288+
fontSize: "var(--ifm-code-font-size)",
289+
color: "var(--openapi-optional)",
290+
},
291+
children: " OPTIONAL",
292+
}),
293+
]),
294+
];

packages/docusaurus-plugin-openapi/src/markdown/createStatusCodesTable.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export function createStatusCodesTable({ responses }: Props) {
6464
body: {
6565
content: responses[code].content,
6666
},
67+
type: "response",
6768
}),
6869
}),
6970
],

packages/docusaurus-theme-openapi/src/theme/ApiPage/styles.module.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
--api-sidebar-hidden-width: 30px;
3434

3535
--openapi-required: var(--openapi-code-red);
36+
--openapi-optional: var(--openapi-code-dim);
3637

3738
--openapi-card-background-color: #f6f8fa;
3839
--openapi-card-border-radius: var(--ifm-pre-border-radius);
@@ -62,6 +63,7 @@ html[data-theme="dark"] {
6263
--openapi-monaco-border-color: #606770;
6364

6465
--openapi-required: var(--openapi-code-red);
66+
--openapi-optional: var(--openapi-code-dim);
6567

6668
--openapi-card-background-color: var(--ifm-card-background-color);
6769

0 commit comments

Comments
 (0)