Skip to content

Commit 45cb59e

Browse files
committed
[bugfix] Ensure 0 and false are guarded correctly and add deprecated support to params (#754)
* avoid guarding 0 values * support deprecated params * update utils tests to should guard false * always render required when true and apply strikethrough when deprecated
1 parent c3d0f72 commit 45cb59e

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function guard<T>(
3838
value: T | undefined,
3939
cb: (value: T) => Children
4040
): string {
41-
if (!!value) {
41+
if (!!value || value === 0) {
4242
const children = cb(value);
4343
return render(children);
4444
}

packages/docusaurus-theme-openapi-docs/src/markdown/utils.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ describe("guard", () => {
2222
expect(actual).toBe("");
2323
});
2424

25+
it("should guard false booleans", () => {
26+
const actual = guard(false, (value) => `${value}`);
27+
expect(actual).toBe("");
28+
});
29+
2530
it("should not guard strings", () => {
2631
const actual = guard("hello", (value) => value);
2732
expect(actual).toBe("hello");
@@ -37,10 +42,6 @@ describe("guard", () => {
3742
expect(actual).toBe("0");
3843
});
3944

40-
it("should not guard false booleans", () => {
41-
const actual = guard(false, (value) => `${value}`);
42-
expect(actual).toBe("false");
43-
});
4445
it("should not guard true booleans", () => {
4546
const actual = guard(true, (value) => `${value}`);
4647
expect(actual).toBe("true");

packages/docusaurus-theme-openapi-docs/src/markdown/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ export function guard<T>(
2626
value: T | undefined | string,
2727
cb: (value: T) => Children
2828
): string {
29-
if (value === undefined || value === "") {
30-
return "";
29+
if (!!value || value === 0) {
30+
const children = cb(value as T);
31+
return render(children);
3132
}
32-
const children = cb(value as T);
33-
return render(children);
33+
return "";
3434
}
3535

3636
export function render(children: Children): string {

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ import React from "react";
1010
import CodeBlock from "@theme/CodeBlock";
1111
import SchemaTabs from "@theme/SchemaTabs";
1212
import TabItem from "@theme/TabItem";
13+
/* eslint-disable import/no-extraneous-dependencies*/
14+
import clsx from "clsx";
15+
import { createDescription } from "docusaurus-theme-openapi-docs/lib/markdown/createDescription";
16+
/* eslint-disable import/no-extraneous-dependencies*/
17+
import {
18+
getQualifierMessage,
19+
getSchemaName,
20+
} from "docusaurus-theme-openapi-docs/lib/markdown/schema";
21+
/* eslint-disable import/no-extraneous-dependencies*/
22+
import {
23+
guard,
24+
toString,
25+
} from "docusaurus-theme-openapi-docs/lib/markdown/utils";
1326
import ReactMarkdown from "react-markdown";
1427
import rehypeRaw from "rehype-raw";
1528

@@ -40,7 +53,7 @@ export interface Props {
4053
}
4154

4255
function ParamsItem({
43-
param: { description, example, examples, name, required, schema },
56+
param: { description, example, examples, name, required, schema, deprecated },
4457
}: Props) {
4558
if (!schema || !schema?.type) {
4659
schema = { type: "any" };
@@ -54,6 +67,10 @@ function ParamsItem({
5467
<span className="openapi-schema__required">required</span>
5568
));
5669

70+
const renderDeprecated = guard(deprecated, () => (
71+
<span className="openapi-schema__deprecated">deprecated</span>
72+
));
73+
5774
const renderSchema = guard(getQualifierMessage(schema), (message) => (
5875
<div>
5976
<ReactMarkdown
@@ -134,10 +151,19 @@ function ParamsItem({
134151
return (
135152
<div className="openapi-params__list-item">
136153
<span className="openapi-schema__container">
137-
<strong className="openapi-schema__property">{name}</strong>
154+
<strong
155+
className={clsx("openapi-schema__property", {
156+
"openapi-schema__strikethrough": deprecated,
157+
})}
158+
>
159+
{name}
160+
</strong>
138161
{renderSchemaName}
139-
{required && <span className="openapi-schema__divider"></span>}
162+
{(required || deprecated) && (
163+
<span className="openapi-schema__divider"></span>
164+
)}
140165
{renderSchemaRequired}
166+
{renderDeprecated}
141167
</span>
142168
{renderSchema}
143169
{renderDefaultValue}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export default function SchemaItem({
111111
<span className="openapi-schema__divider"></span>
112112
)}
113113
{renderNullable}
114-
{!deprecated && renderRequired}
114+
{renderRequired}
115115
{renderDeprecated}
116116
</span>
117117
{renderQualifierMessage}

0 commit comments

Comments
 (0)