Skip to content

Commit

Permalink
Merge pull request #951 from omonk/ollie.monk/x-enumDescriptions
Browse files Browse the repository at this point in the history
Render x-enumDescriptions in table
  • Loading branch information
sserrata authored Sep 10, 2024
2 parents d31f4e2 + d0b5160 commit 961aecd
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 17 deletions.
13 changes: 13 additions & 0 deletions demo/examples/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ paths:
- available
- pending
- sold
x-enumDescriptions:
available: When the pet is available
pending: When the pet is being sold
sold: When the pet has been sold
default: available
responses:
"200":
Expand Down Expand Up @@ -1114,6 +1118,15 @@ components:
- available
- pending
- sold
x-enumDescriptions:
available: When the pet is available
pending: When the pet is being sold
sold: |
When the pet has been sold.
These descriptions can contain line
breaks and also [links](https://docusaurus.io/)
petType:
description: Type of a pet
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,17 @@ export function createParamsDetails({ parameters, type }: Props) {
create("div", {
children: [
create("ul", {
children: params.map((param) =>
create("ParamsItem", {
children: params.map((param) => {
return create("ParamsItem", {
className: "paramsItem",
param: param,
})
),
param: {
...param,
enumDescriptions: Object.entries(
param?.schema?.items?.["x-enumDescriptions"] ?? {}
),
},
});
}),
}),
],
}),
Expand Down
2 changes: 2 additions & 0 deletions packages/docusaurus-plugin-openapi-docs/src/openapi/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export interface ParameterObject {
param?: Object;
// ignoring stylings: matrix, label, form, simple, spaceDelimited,
// pipeDelimited and deepObject
"x-enumDescriptions"?: Record<string, string>;
}

export interface ParameterObjectWithRef {
Expand Down Expand Up @@ -353,6 +354,7 @@ export type SchemaObject = Omit<
example?: any;
deprecated?: boolean;
"x-tags"?: string[];
"x-enumDescriptions"?: Record<string, string>;
};

export type SchemaObjectWithRef = Omit<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ export class OpenAPIParser {
const {
type,
enum: enumProperty,
"x-enumDescription": enumDescription,
properties,
items,
required,
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-theme-openapi-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"react-modal": "^3.15.1",
"react-redux": "^7.2.0",
"rehype-raw": "^6.1.1",
"remark-gfm": "3.0.1",
"sass": "^1.58.1",
"sass-loader": "^13.3.2",
"webpack": "^5.61.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import TabItem from "@theme/TabItem";
import clsx from "clsx";
import ReactMarkdown from "react-markdown";
import rehypeRaw from "rehype-raw";
import remarkGfm from "remark-gfm";

import { createDescription } from "../../markdown/createDescription";
import { getQualifierMessage, getSchemaName } from "../../markdown/schema";
Expand All @@ -39,12 +40,38 @@ export interface Props {
required: boolean;
deprecated: boolean;
schema: any;
enumDescriptions?: [string, string][];
};
}

function ParamsItem({
param: { description, example, examples, name, required, schema, deprecated },
}: Props) {
const getEnumDescriptionMarkdown = (enumDescriptions?: [string, string][]) => {
if (enumDescriptions?.length) {
return `| Enum Value | Description |
| ---- | ----- |
${enumDescriptions
.map((desc) => {
return `| ${desc[0]} | ${desc[1]} | `.replaceAll("\n", "<br/>");
})
.join("\n")}
`;
}

return "";
};

function ParamsItem({ param, ...rest }: Props) {
const {
description,
example,
examples,
name,
required,
deprecated,
enumDescriptions,
} = param;

let schema = param.schema;

if (!schema || !schema?.type) {
schema = { type: "any" };
}
Expand Down Expand Up @@ -91,6 +118,19 @@ function ParamsItem({
</div>
));

const renderEnumDescriptions = guard(
getEnumDescriptionMarkdown(enumDescriptions),
(value) => {
return (
<ReactMarkdown
rehypePlugins={[rehypeRaw]}
remarkPlugins={[remarkGfm]}
children={value}
/>
);
}
);

const renderDefaultValue = guard(
schema && schema.items
? schema.items.default
Expand Down Expand Up @@ -158,6 +198,7 @@ function ParamsItem({
{renderSchema}
{renderDefaultValue}
{renderDescription}
{renderEnumDescriptions}
{renderExample}
{renderExamples}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import CodeBlock from "@theme/CodeBlock";
import clsx from "clsx";
import ReactMarkdown from "react-markdown";
import rehypeRaw from "rehype-raw";
import remarkGfm from "remark-gfm";

import { createDescription } from "../../markdown/createDescription";
import { guard } from "../../markdown/utils";
Expand All @@ -27,22 +28,51 @@ export interface Props {
discriminator: boolean;
}

export default function SchemaItem({
children: collapsibleSchemaContent,
collapsible,
name,
qualifierMessage,
required,
schemaName,
schema,
}: Props) {
const transformEnumDescriptions = (
enumDescriptions?: Record<string, string>
) => {
if (enumDescriptions) {
return Object.entries(enumDescriptions);
}

return [];
};

const getEnumDescriptionMarkdown = (enumDescriptions?: [string, string][]) => {
if (enumDescriptions?.length) {
return `| Enum Value | Description |
| ---- | ----- |
${enumDescriptions
.map((desc) => {
return `| ${desc[0]} | ${desc[1]} | `.replaceAll("\n", "<br/>");
})
.join("\n")}
`;
}

return "";
};

export default function SchemaItem(props: Props) {
const {
children: collapsibleSchemaContent,
collapsible,
name,
qualifierMessage,
required,
schemaName,
schema,
} = props;
let deprecated;
let schemaDescription;
let defaultValue;
let nullable;
let enumDescriptions: [string, string][] = [];

if (schema) {
deprecated = schema.deprecated;
schemaDescription = schema.description;
enumDescriptions = transformEnumDescriptions(schema["x-enumDescriptions"]);
defaultValue = schema.default;
nullable = schema.nullable;
}
Expand All @@ -60,6 +90,19 @@ export default function SchemaItem({
<span className="openapi-schema__nullable">nullable</span>
));

const renderEnumDescriptions = guard(
getEnumDescriptionMarkdown(enumDescriptions),
(value) => {
return (
<ReactMarkdown
remarkPlugins={[remarkGfm]}
rehypePlugins={[rehypeRaw]}
children={value}
/>
);
}
);

const renderSchemaDescription = guard(schemaDescription, (description) => (
<div>
<ReactMarkdown
Expand Down Expand Up @@ -117,6 +160,7 @@ export default function SchemaItem({
{renderQualifierMessage}
{renderDefaultValue}
{renderSchemaDescription}
{renderEnumDescriptions}
{collapsibleSchemaContent ?? collapsibleSchemaContent}
</div>
);
Expand Down

0 comments on commit 961aecd

Please sign in to comment.