Skip to content

Commit

Permalink
Add example model referencing list of objects
Browse files Browse the repository at this point in the history
  • Loading branch information
liviuba committed Aug 6, 2024
1 parent 86dfaa7 commit a7bf304
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
27 changes: 18 additions & 9 deletions api/api/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ async def get_descendents(
uuid: shared_data_models.UUID, db: Repository = Depends()
) -> List[source_type]:
return await db.get_docs(
doc_filter={source_attribute: uuid}, doc_type=source_type
# !!!!
# source_attribute has list values sometimes (for models that reference a list of other objects)
# mongo queries just so happen have the semantics we want
# a.i. list_attribute: some_val means "any value in list_attribute is equal to some_val"
doc_filter={source_attribute: uuid},
doc_type=source_type,
)

return get_descendents
Expand All @@ -60,14 +65,18 @@ async def get_descendents(
if len(uuid_field_type.metadata):
parent_type = uuid_field_type.metadata[0] # convention

router.add_api_route(
f"/{to_snake(parent_type.__name__)}/{{uuid}}/{to_snake(model.__name__)}",
response_model=List[model],
operation_id=f"get{model.__name__}In{parent_type.__name__}",
summary=f"Get {model.__name__} In {parent_type.__name__}",
methods=["GET"],
endpoint=make_reverse_link_handler(uuid_field_name, model),
)
# List validators (e.g. MinLength) are also set in the type.metadata list
# -> only generate backlinks for types that have metadata that is also one of the exposed types
# ? Maybe find a better way to wrap "foreign key"-type relationships, like a custom generic similar to List so we can also drop the [0] convention?
if parent_type in models_public:
router.add_api_route(
f"/{to_snake(parent_type.__name__)}/{{uuid}}/{to_snake(model.__name__)}",
response_model=List[model],
operation_id=f"get{model.__name__}In{parent_type.__name__}",
summary=f"Get {model.__name__} In {parent_type.__name__}",
methods=["GET"],
endpoint=make_reverse_link_handler(uuid_field_name, model),
)


def make_router() -> APIRouter:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ def fields_by_type(cls, field_type: Any) -> Dict[str, FieldInfo]:
fields_filtered = {}

for field_name, field_info in cls.model_fields.items():
if field_info.annotation is field_type:
# conditions split for clarity
field_type_exact = field_info.annotation is field_type
field_type_container_list = (
hasattr(field_info.annotation, "__origin__")
and field_info.annotation.__origin__ is list
and field_info.annotation.__args__[0] is field_type
)
if field_type_exact or field_type_container_list:
fields_filtered[field_name] = field_info

return fields_filtered
Expand Down Expand Up @@ -131,7 +138,7 @@ class ExperimentalImagingDataset(

class Specimen(semantic_models.Specimen, DocumentMixin):
imaging_preparation_protocol_uuid: List[UUID] = Field(min_length=1)
sample_of_uuid: List[UUID] = Field(min_length=1)
sample_of_uuid: Annotated[List[UUID], BioSample] = Field(min_length=1)
growth_protocol_uuid: List[UUID] = Field()

model_config = ConfigDict(model_version_latest=1)
Expand Down

0 comments on commit a7bf304

Please sign in to comment.