Skip to content

Commit

Permalink
Extends fix (#198)
Browse files Browse the repository at this point in the history
* generate=False with field set is behaving correctly

* Better error messages in model validation

* Fixing NPE for elements with read & write=False

* Allowing sample data to be array of values in validation

* When loading extended model, passing all the options from settings model section
  • Loading branch information
mesemus authored Jul 10, 2023
1 parent dfba81e commit 4d6b35c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ def marshmallow_field(
section = datatype.section_marshmallow
f = []
super().marshmallow_field(datatype, fields=f)
if not f:
return
fld: MarshmallowField = f[0]
fld.reference = MarshmallowReference(reference=section.config.get("class"))
fields.append(fld)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def ui_marshmallow_field(
section = datatype.section_ui
f = []
super().ui_marshmallow_field(datatype, fields=f)
if not f:
return
fld: MarshmallowField = f[0]
fld.reference = MarshmallowReference(
reference=section.config["marshmallow"].get("class")
Expand Down
19 changes: 19 additions & 0 deletions oarepo_model_builder/datatypes/components/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ class SampleSchema(StrictSchema):
)
params = ma.fields.Raw(metadata={"doc": "Params for the faker"})

def load(
self,
data,
*,
many=None,
partial=None,
unknown=None,
):
if isinstance(data, (list, tuple)):
return data
return super().load(data, many=many, partial=partial, unknown=unknown)

def dump(self, obj, *, many=None):
if many:
return [self.dump(x, many=False) for x in obj]
if isinstance(obj, (list, tuple)):
return obj
return super().dump(obj, many=False)


class RegularSampleComponent(DataTypeComponent):
class ModelSchema(ma.Schema):
Expand Down
5 changes: 4 additions & 1 deletion oarepo_model_builder/profiles/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ def handle_extend(
3. the resulting model will be merged as if use: was used
"""

loaded_schema = {"model": {"use": [extended_schema]}}
loaded_schema = {
"record": {"use": [extended_schema]},
"settings": model.settings,
}

extended_model = load_model(
extended_schema.split("#", maxsplit=1)[0],
Expand Down
1 change: 1 addition & 0 deletions oarepo_model_builder/validation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def validate_model(model):
for err, path in flatten_errors(e.messages_dict, ""):
if path.endswith("._schema") and err == "Unknown field.":
continue
path = path.replace(".value.", ".")
msg.append(f"{path}: {err}")
msg = "\n ".join(msg)
raise InvalidModelException(f"Invalid model: \n {msg}")
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = oarepo-model-builder
version = 4.0.23
version = 4.0.24
description = A utility library that generates OARepo required data model files from a JSON specification file
authors = Miroslav Bauer <[email protected]>, Miroslav Simek <[email protected]>
readme = README.md
Expand Down

0 comments on commit 4d6b35c

Please sign in to comment.