Skip to content

Commit 6a466fe

Browse files
authored
fix: render inline enum values in anyOf schemas (#1285) (#1286)
Handle enum-only schemas (without explicit type) in anyOf/oneOf by treating them as primitives. This ensures the enum values are displayed correctly in the schema documentation.
1 parent 47258df commit 6a466fe

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

demo/examples/tests/anyOf.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ paths:
2020
- type: integer
2121
- type: boolean
2222
- type: null
23+
- enum:
24+
- dealValue
25+
- price
26+
- userRating
27+
- popularityScore
2328
```
29+
30+
Note: The last option is an inline enum without an explicit type,
31+
which is valid JSON Schema. The enum values should be displayed.
2432
responses:
2533
"200":
2634
description: Successful response
@@ -32,6 +40,11 @@ paths:
3240
- type: integer
3341
- type: boolean
3442
- type: "null"
43+
- enum:
44+
- dealValue
45+
- price
46+
- userRating
47+
- popularityScore
3548

3649
/anyof-oneof:
3750
get:

packages/docusaurus-plugin-openapi-docs/src/markdown/schema.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
import { SchemaObject } from "../openapi/types";
99

1010
function prettyName(schema: SchemaObject, circular?: boolean) {
11+
// Handle enum-only schemas (valid in JSON Schema)
12+
// When enum is present without explicit type, treat as string
13+
if (schema.enum && !schema.type) {
14+
return "string";
15+
}
16+
1117
if (schema.format) {
1218
if (schema.type) {
1319
return `${schema.type}<${schema.format}>`;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import { OPENAPI_SCHEMA_ITEM } from "../theme/translationIds";
1111
import { SchemaObject } from "../types";
1212

1313
function prettyName(schema: SchemaObject, circular?: boolean) {
14+
// Handle enum-only schemas (valid in JSON Schema)
15+
// When enum is present without explicit type, treat as string
16+
if (schema.enum && !schema.type) {
17+
return "string";
18+
}
19+
1420
if (schema.format) {
1521
return schema.format;
1622
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,5 +999,10 @@ const PRIMITIVE_TYPES: Record<PrimitiveSchemaType, true> = {
999999
} as const;
10001000

10011001
const isPrimitive = (schema: SchemaObject) => {
1002+
// Enum-only schemas (without explicit type) should be treated as primitives
1003+
// This is valid JSON Schema where enum values define the constraints
1004+
if (schema.enum && !schema.type) {
1005+
return true;
1006+
}
10021007
return PRIMITIVE_TYPES[schema.type as PrimitiveSchemaType];
10031008
};

0 commit comments

Comments
 (0)