Skip to content

Proper handling of synthetic properties from all view model creation contexts #597

@bencap

Description

@bencap

During view model validation, we often have synthetic pydantic properties generated from other model properties. These can be found by searching for Unable to create {cls.__name__} without attribute: {exc}.. These are unproblematic when generating data from ORM contexts where synthetic fields exist, but become a problem when attempting to generate valid view models (objects returned and thus validated by the API) from non-ORM contexts.

This package purports to offer view models to ease the burden of constructing valid objects, so not being able to construct valid view models from objects returned from the API should be considered a bug.

These synthetic constructors protect fields that are unset when constructing objects from ORM models, but that are guaranteed to be set when constructing objects from non-ORM contexts (or at least, from non-ORM contexts where we have previously validated the model). We should ensure that these view models can be constructed from both contexts, since it significantly degrades the utility of these models to be unable to generate the view model corresponding to an endpoint from the endpoint response itself.

Take the following example with score sets. If we construct the score set in the API, we will not have the fields primary_publication_identifiers set on the score set ORM object, so we generate it from the publication_identifier_associations property on the ORM model. Now, the property is set on the output but attempting to regenerate the model from the dumped ScoreSet view model will yield an error: Value error, Unable to create ScoreSet without attribute: 'dict' object has no attribute 'publication_identifier_associations'.

Metadata

Metadata

Assignees

Labels

app: backendTask implementation touches the backendtype: bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions