Skip to content

Commit 319d55e

Browse files
Fix #64 enums without declared type (#77)
* 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> * Update CHANGELOG.md * Update __init__.py --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent fda4c0e commit 319d55e

File tree

4 files changed

+13
-1
lines changed

4 files changed

+13
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.4.1] - 2026-03-??
9+
10+
- Fix handling of `enum` without declared `type`, like reported in [#64](https://github.com/Neoteroi/essentials-openapi/issues/64) by @jan-ldwg.
11+
812
## [1.4.0] - 2026-03-07 :copilot:
913

1014
- Add OAS 3.1 support, cross-version warnings, and fix nullable spacing, by @dcode.

openapidocs/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version__ = "1.4.0"
1+
__version__ = "1.4.1"
22
VERSION = __version__

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)