Skip to content

Commit cc5067d

Browse files
authored
fix: Guard only undefined and empty strings (#725)
1 parent f01a0d8 commit cc5067d

File tree

4 files changed

+58
-12
lines changed

4 files changed

+58
-12
lines changed

demo/examples/petstore.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,7 @@ components:
996996
description: Average amount of honey produced per day in ounces
997997
example: 3.14
998998
multipleOf: .01
999+
default: 0
9991000
required:
10001001
- honeyPerDay
10011002
Id:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* ============================================================================
2+
* Copyright (c) Palo Alto Networks
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
* ========================================================================== */
7+
8+
import { guard } from "./utils";
9+
10+
describe("guard", () => {
11+
it("should guard empty strings", () => {
12+
const actual = guard("", (_) => {
13+
throw new Error("Should not be called");
14+
});
15+
expect(actual).toBe("");
16+
});
17+
18+
it("should guard undefined", () => {
19+
const actual = guard(undefined, (value) => {
20+
throw new Error("Should not be called");
21+
});
22+
expect(actual).toBe("");
23+
});
24+
25+
it("should not guard strings", () => {
26+
const actual = guard("hello", (value) => value);
27+
expect(actual).toBe("hello");
28+
});
29+
30+
it("should not guard numbers", () => {
31+
const actual = guard(1, (value) => `${value}`);
32+
expect(actual).toBe("1");
33+
});
34+
35+
it("should not guard numbers equals to 0", () => {
36+
const actual = guard(0, (value) => `${value}`);
37+
expect(actual).toBe("0");
38+
});
39+
40+
it("should not guard false booleans", () => {
41+
const actual = guard(false, (value) => `${value}`);
42+
expect(actual).toBe("false");
43+
});
44+
it("should not guard true booleans", () => {
45+
const actual = guard(true, (value) => `${value}`);
46+
expect(actual).toBe("true");
47+
});
48+
});

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ export function guard<T>(
2424
value: T | undefined | string,
2525
cb: (value: T) => Children
2626
): string {
27-
if (!!value) {
28-
const children = cb(value as T);
29-
return render(children);
27+
if (value === undefined || value === "") {
28+
return "";
3029
}
31-
return "";
30+
const children = cb(value as T);
31+
return render(children);
3232
}
3333

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

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,11 @@ function SchemaItem({
7979
</div>
8080
));
8181

82-
const renderDefaultValue = guard(
83-
typeof defaultValue === "boolean" ? defaultValue.toString() : defaultValue,
84-
(value) => (
85-
<div className="">
86-
<ReactMarkdown children={`**Default value:** \`${value}\``} />
87-
</div>
88-
)
89-
);
82+
const renderDefaultValue = guard(defaultValue, (value) => (
83+
<div className="">
84+
<ReactMarkdown children={`**Default value:** \`${value}\``} />
85+
</div>
86+
));
9087

9188
const schemaContent = (
9289
<div>

0 commit comments

Comments
 (0)