Skip to content

Commit 21e6ede

Browse files
authored
fix(params): display const value in parameter description and Try It form (#1367)
- ParamsItem now renders "Constant value: <X>" for parameters whose schema uses const, reusing the existing CONSTANT_VALUE translation ID - getSchemaEnum now treats schema.const as a single-value enum, routing const parameters to the existing ParamSelectFormItem dropdown instead of a free-form text input Closes #1307
1 parent 5fdee72 commit 21e6ede

File tree

2 files changed

+30
-0
lines changed
  • packages/docusaurus-theme-openapi-docs/src/theme

2 files changed

+30
-0
lines changed

packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/ParamOptions/index.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ export function getSchemaEnum(schema: any): any[] | undefined {
4848
}
4949
}
5050

51+
// const is semantically a single-value enum
52+
if (schema?.const !== undefined) {
53+
return [schema.const];
54+
}
55+
5156
return undefined;
5257
}
5358

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,34 @@ function ParamsItem({ param, ...rest }: Props) {
102102
</span>
103103
));
104104

105+
const constValue = schema?.const;
106+
105107
const renderQualifier = guard(getQualifierMessage(schema), (qualifier) => (
106108
<Markdown>{qualifier}</Markdown>
107109
));
108110

111+
function renderConstValue() {
112+
if (constValue === undefined) {
113+
return undefined;
114+
}
115+
const label = translate({
116+
id: OPENAPI_SCHEMA_ITEM.CONSTANT_VALUE,
117+
message: "Constant value:",
118+
});
119+
return (
120+
<div>
121+
<strong>{label} </strong>
122+
<span>
123+
<code>
124+
{typeof constValue === "string"
125+
? constValue
126+
: JSON.stringify(constValue)}
127+
</code>
128+
</span>
129+
</div>
130+
);
131+
}
132+
109133
const renderDescription = guard(description, (description) => (
110134
<Markdown>{description}</Markdown>
111135
));
@@ -173,6 +197,7 @@ function ParamsItem({ param, ...rest }: Props) {
173197
{renderDeprecated}
174198
</span>
175199
{renderQualifier}
200+
{renderConstValue()}
176201
{renderDescription}
177202
{renderEnumDescriptions}
178203
{renderDefaultValue()}

0 commit comments

Comments
 (0)