Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix tests after model update with version and model version, and extend forbidding extra fields to nested nodes #135

Merged
merged 4 commits into from
Aug 1, 2024

Conversation

sherwoodf
Copy link
Contributor

Fixes tests broken by: #128 (review)

Also moves the configuration that forbids extra fields into the semantic models file to be inherited instead of BaseModel. This stops extra fields from being passed into models that govern 'nested'/'embedded' json objects inside other json objects

Also updates the filter model dictionary function to work with/expect optional fields.

@@ -155,5 +155,5 @@ def persist(object_list: List, object_path: str, sumbission_accno: str):

def filter_model_dictionary(dictionary: dict, target_model: Type[BaseModel]):
accepted_fields = target_model.model_fields.keys()
result_dict = {key: dictionary[key] for key in accepted_fields}
result_dict = {key: dictionary[key] for key in accepted_fields if key in dictionary}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix to cope with optional/missing fields in the original dictionary


class ConfiguredBaseModel(BaseModel):
# Throw error if you try to validate/create model from a dictionary with keys that aren't a field in the model
model_config = ConfigDict(extra="forbid")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to the semantic models to make sure there isn't an edge case where e.g. a study can't have extra fields, but a contributor in a study can

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw nice find, we've literally had this bug in the api for nested objects since forever

@@ -41,6 +38,7 @@ def __init__(self, *args, **data):
)
model_metadata_existing = data.get("model", None)
if model_metadata_existing:
model_metadata_existing = ModelMetadata(**model_metadata_existing)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix to the error checking - as it was currently trying to compare a dictionary with a model. This was in the original API models but was missed when copied over to here.

Copy link
Contributor

@kbab kbab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@sherwoodf sherwoodf removed the request for review from liviuba August 1, 2024 11:59
Copy link
Contributor

@liviuba liviuba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@sherwoodf sherwoodf merged commit 2618a5c into main Aug 1, 2024
33 checks passed
ctr26 pushed a commit that referenced this pull request Aug 2, 2024
…nd forbidding extra fields to nested nodes (#135)

* fix tests after model update with version and model version, and extend forbidding extra fields to nested nodes

* Update experimental_imaging_dataset.py

* Update utils.py
ctr26 pushed a commit that referenced this pull request Aug 2, 2024
…nd forbidding extra fields to nested nodes (#135)

* fix tests after model update with version and model version, and extend forbidding extra fields to nested nodes

* Update experimental_imaging_dataset.py

* Update utils.py
@sherwoodf sherwoodf deleted the fix_tests branch August 9, 2024 12:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants