Skip to content

Commit

Permalink
Handle when a single item is found.
Browse files Browse the repository at this point in the history
Add a helper to allow for customise how a field is generated.
  • Loading branch information
timsavage committed Jun 27, 2023
1 parent 6ad1053 commit b73abfd
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/odin/contrib/json_schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
odin.validators.MaxLengthValidator: {},
odin.validators.MinLengthValidator: {},
}
JSON_SCHEMA_METHOD: Final[str] = "as_json_schema"


class JSONSchema:
Expand Down Expand Up @@ -108,7 +109,11 @@ def _field_type(
"""Get the type of a field."""

field_type = type(field)
if field_type in FIELD_SCHEMAS:

if method := getattr(field, JSON_SCHEMA_METHOD, None):
type_name, schema = method()

elif field_type in FIELD_SCHEMAS:
type_name, schema = FIELD_SCHEMAS[field_type]

elif isinstance(field, odin.EnumField):
Expand Down Expand Up @@ -140,12 +145,15 @@ def _composite_field_to_schema(self, field: odin.CompositeField) -> Dict[str, An
# Handle abstract resources
child_resources = get_child_resources(field.of)
if child_resources:
schema = {
"oneOf": [
self._schema_def(child_resource)
for child_resource in child_resources
]
}
if len(child_resources) == 1:
schema = self._schema_def(child_resources[0])
else:
schema = {
"oneOf": [
self._schema_def(child_resource)
for child_resource in child_resources
]
}
else:
schema = self._schema_def(field.of)

Expand Down

0 comments on commit b73abfd

Please sign in to comment.