Skip to content

Commit

Permalink
[bugfix] Ensure 0 and false are guarded correctly and add deprecated …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
sserrata authored Mar 20, 2024
1 parent 089ae57 commit 69068f7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function guard<T>(
value: T | undefined,
cb: (value: T) => Children
): string {
if (!!value) {
if (!!value || value === 0) {
const children = cb(value);
return render(children);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ describe("guard", () => {
expect(actual).toBe("");
});

it("should guard false booleans", () => {
const actual = guard(false, (value) => `${value}`);
expect(actual).toBe("");
});

it("should not guard strings", () => {
const actual = guard("hello", (value) => value);
expect(actual).toBe("hello");
Expand All @@ -37,10 +42,6 @@ describe("guard", () => {
expect(actual).toBe("0");
});

it("should not guard false booleans", () => {
const actual = guard(false, (value) => `${value}`);
expect(actual).toBe("false");
});
it("should not guard true booleans", () => {
const actual = guard(true, (value) => `${value}`);
expect(actual).toBe("true");
Expand Down
8 changes: 4 additions & 4 deletions packages/docusaurus-theme-openapi-docs/src/markdown/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export function guard<T>(
value: T | undefined | string,
cb: (value: T) => Children
): string {
if (value === undefined || value === "") {
return "";
if (!!value || value === 0) {
const children = cb(value as T);
return render(children);
}
const children = cb(value as T);
return render(children);
return "";
}

export function render(children: Children): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import CodeBlock from "@theme/CodeBlock";
import SchemaTabs from "@theme/SchemaTabs";
import TabItem from "@theme/TabItem";
/* eslint-disable import/no-extraneous-dependencies*/
import clsx from "clsx";
import { createDescription } from "docusaurus-theme-openapi-docs/lib/markdown/createDescription";
/* eslint-disable import/no-extraneous-dependencies*/
import {
Expand All @@ -26,8 +27,10 @@ import ReactMarkdown from "react-markdown";
import rehypeRaw from "rehype-raw";

function ParamsItem({
param: { description, example, examples, name, required, schema },
param: { description, example, examples, name, required, schema, deprecated },
}) {
console.log(name, required);

if (!schema || !schema?.type) {
schema = { type: "any" };
}
Expand All @@ -40,6 +43,10 @@ function ParamsItem({
<span className="openapi-schema__required">required</span>
));

const renderDeprecated = guard(deprecated, () => (
<span className="openapi-schema__deprecated">deprecated</span>
));

const renderSchema = guard(getQualifierMessage(schema), (message) => (
<div>
<ReactMarkdown
Expand Down Expand Up @@ -119,10 +126,19 @@ function ParamsItem({
return (
<div className="openapi-params__list-item">
<span className="openapi-schema__container">
<strong className="openapi-schema__property">{name}</strong>
<strong
className={clsx("openapi-schema__property", {
"openapi-schema__strikethrough": deprecated,
})}
>
{name}
</strong>
{renderSchemaName}
{required && <span className="openapi-schema__divider"></span>}
{(required || deprecated) && (
<span className="openapi-schema__divider"></span>
)}
{renderSchemaRequired}
{renderDeprecated}
</span>
{renderSchema}
{renderDefaultValue}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function SchemaItem({
<span className="openapi-schema__divider"></span>
)}
{renderNullable}
{!deprecated && renderRequired}
{renderRequired}
{renderDeprecated}
</span>
{renderQualifierMessage}
Expand Down

0 comments on commit 69068f7

Please sign in to comment.