Skip to content

Commit 6168043

Browse files
authored
feat: support const for OAS 3.1.1 (#1143)
1 parent 808e4a2 commit 6168043

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed

demo/examples/tests/const.yaml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
openapi: 3.1.1
2+
info:
3+
title: Const
4+
description: Demonstrates various const.
5+
version: 1.0.0
6+
tags:
7+
- name: const
8+
description: const tests
9+
paths:
10+
/const:
11+
get:
12+
tags:
13+
- const
14+
summary: const with primitives
15+
description: |
16+
Schema:
17+
```yaml
18+
type: object
19+
properties:
20+
type:
21+
type: string
22+
const: example
23+
title: Example
24+
description: |
25+
This is an example
26+
quality:
27+
type: string
28+
oneOf:
29+
- const: good
30+
title: Good
31+
description: |
32+
This is a good example
33+
- const: bad
34+
title: Bad
35+
description: |
36+
This is a bad example
37+
tags:
38+
type: array
39+
items:
40+
anyOf:
41+
- const: dog
42+
title: Dog
43+
description: |
44+
This is a dog
45+
- const: cat
46+
title: Cat
47+
description: |
48+
This is a cat
49+
required:
50+
- type
51+
- quality
52+
```
53+
responses:
54+
"200":
55+
description: Successful response
56+
content:
57+
application/json:
58+
schema:
59+
type: object
60+
properties:
61+
type:
62+
type: string
63+
const: constExample
64+
title: Const Example
65+
description: |
66+
Const example description
67+
quality:
68+
type: string
69+
oneOf:
70+
- const: good
71+
title: Good
72+
description: |
73+
This is a good example
74+
- const: bad
75+
title: Bad
76+
description: |
77+
This is a bad example
78+
tags:
79+
type: array
80+
items:
81+
type: string
82+
anyOf:
83+
- const: dog
84+
title: Dog
85+
description: |
86+
This is a dog
87+
- const: cat
88+
title: Cat
89+
description: |
90+
This is a cat
91+
required:
92+
- type
93+
- quality

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ const AnyOneOf: React.FC<SchemaProps> = ({ schema, schemaType }) => {
122122
value={`${index}-item-properties`}
123123
>
124124
{/* Handle primitive types directly */}
125-
{["string", "number", "integer", "boolean"].includes(
125+
{(["string", "number", "integer", "boolean"].includes(
126126
anyOneSchema.type
127-
) && (
127+
) ||
128+
anyOneSchema.const) && (
128129
<SchemaItem
129130
collapsible={false}
130131
name={undefined}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export default function SchemaItem(props: Props) {
6565
let example: string | undefined;
6666
let nullable;
6767
let enumDescriptions: [string, string][] = [];
68+
let constValue: string | undefined;
6869

6970
if (schema) {
7071
deprecated = schema.deprecated;
@@ -75,6 +76,7 @@ export default function SchemaItem(props: Props) {
7576
nullable =
7677
schema.nullable ||
7778
(Array.isArray(schema.type) && schema.type.includes("null")); // support JSON Schema nullable
79+
constValue = schema.const;
7880
}
7981

8082
const renderRequired = guard(
@@ -161,6 +163,30 @@ export default function SchemaItem(props: Props) {
161163
return undefined;
162164
}
163165

166+
function renderConstValue() {
167+
if (constValue !== undefined) {
168+
if (typeof constValue === "string") {
169+
return (
170+
<div>
171+
<strong>Constant value: </strong>
172+
<span>
173+
<code>{constValue}</code>
174+
</span>
175+
</div>
176+
);
177+
}
178+
return (
179+
<div>
180+
<strong>Constant value: </strong>
181+
<span>
182+
<code>{JSON.stringify(constValue)}</code>
183+
</span>
184+
</div>
185+
);
186+
}
187+
return undefined;
188+
}
189+
164190
const schemaContent = (
165191
<div>
166192
<span className="openapi-schema__container">
@@ -184,6 +210,7 @@ export default function SchemaItem(props: Props) {
184210
{renderSchemaDescription}
185211
{renderEnumDescriptions}
186212
{renderQualifierMessage}
213+
{renderConstValue()}
187214
{renderDefaultValue()}
188215
{renderExample()}
189216
{collapsibleSchemaContent ?? collapsibleSchemaContent}

0 commit comments

Comments
 (0)