Skip to content

Commit 49ea82d

Browse files
committed
Add readme and tidy up marshmallow_jsonschema
1 parent dc696dd commit 49ea82d

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/labthings/json/marshmallow_jsonschema/README

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,22 @@ This submodule is a modified version of fuhrysteve/marshmallow-jsonschema, with
33
This will convert any Marshmallow schema into a JSON schema, without any schema references or additional properties.
44
It will create a basic, inline schema only.
55

6+
It has also been modified (with the addition of `.base.convert_type_list_to_oneof`) to avoid returning list values for
7+
the type of a schema (this is valid JSON schema, but is not permitted in Thing Description syntax). Instead, we expand
8+
the list, by creating a copy of the schema for each type, and combining them using `oneOf`. This means that
9+
`fields.String(allow_none=True)`, which would previously be rendered as:
10+
```json
11+
{"type": ["string", "null"]}
12+
```
13+
will be dumped as
14+
```json
15+
{
16+
"oneOf": [
17+
{"type": "string"},
18+
{"type": "null"}
19+
]
20+
}
21+
```
22+
This is also valid JSONSchema, though clearly less elegant. However, it's required by the thing description.
23+
624
https://github.com/fuhrysteve/marshmallow-jsonschema

src/labthings/json/marshmallow_jsonschema/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ def convert_type_list_to_oneof(schema):
7979
that use a list of types into a series of schemas each with
8080
one type. Those schemas are then nested together using
8181
OneOf.
82+
83+
Schemas that do not have a type that is a list are returned
84+
unmodified.
8285
"""
8386
if "type" not in schema or type(schema["type"]) != list:
8487
return schema
@@ -223,8 +226,7 @@ def _get_schema_for_field(self, obj, field):
223226
)
224227
if base_class is not None and base_class in FIELD_VALIDATORS:
225228
schema = FIELD_VALIDATORS[base_class](schema, field, validator, obj)
226-
if "type" in schema and type(schema["type"]) == list:
227-
schema = convert_type_list_to_oneof(schema)
229+
schema = convert_type_list_to_oneof(schema)
228230
return schema
229231

230232
def _from_nested_schema(self, _, field):

0 commit comments

Comments
 (0)