Skip to content

Commit d1fc3b3

Browse files
Fix enum without explicit type rendering as null (issue #64)
When a schema uses 'enum' without a 'type' field (valid JSON Schema), get_example_from_schema fell through to 'return None' because there was no type to dispatch to a ScalarExampleHandler. The enum check in ScalarExampleHandler.get_example was never reached. Fix: add an early enum check in get_example_from_schema itself, after the 'examples' check and before the type dispatch, so typeless enums also return their first value. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent fda4c0e commit d1fc3b3

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

openapidocs/mk/v3/examples.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ def get_example_from_schema(schema) -> Any:
140140
if isinstance(examples, list) and examples:
141141
return examples[0]
142142

143+
# enum without explicit type - return the first value directly
144+
enum = schema.get("enum")
145+
if isinstance(enum, list) and enum:
146+
return enum[0]
147+
143148
# does it have a type?
144149
handlers_types: List[Type[SchemaExampleHandler]] = list(
145150
get_subclasses(SchemaExampleHandler)

tests/test_oas31.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ class TestGetExampleFromSchemaAnnotations:
187187
({"type": "boolean", "enum": [False]}, False),
188188
# enum on string (pre-existing behaviour still works)
189189
({"type": "string", "enum": ["active", "inactive"]}, "active"),
190+
# enum without explicit type (valid JSON Schema)
191+
({"enum": ["active", "inactive"]}, "active"),
192+
({"enum": [1, 2, 3]}, 1),
190193
],
191194
)
192195
def test_examples_and_enum(self, schema, expected):

0 commit comments

Comments
 (0)