🐛 fix(schema.py): fix the transformation of tags in ListComponentResponse to return a list of TagResponse objects

🔥 chore(service.py): remove unnecessary code that flattens the tags in ListComponentResponse
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-10-27 11:05:02 -03:00
commit dfabb80567
2 changed files with 17 additions and 5 deletions

View file

@ -1,5 +1,5 @@
from datetime import datetime
from pydantic import BaseModel
from pydantic import BaseModel, validator
from typing import Optional, List
from uuid import UUID
@ -45,9 +45,21 @@ class ListComponentResponse(BaseModel):
is_component: Optional[bool]
metadata: Optional[dict]
user_created: Optional[dict]
tags: Optional[List[TagsIdResponse]] = None
tags: Optional[List[TagResponse]] = None
downloads_count: Optional[int]
# tags comes as a TagsIdResponse but we want to return a list of TagResponse
@validator("tags", pre=True)
def tags_to_list(cls, v):
# Check if all values are have id and name
# if so, return v else transform to TagResponse
if all(["id" in tag and "name" in tag for tag in v]):
return v
else:
return [
TagResponse(**tag.get("tags_id")) for tag in v if tag.get("tags_id")
]
class DownloadComponentResponse(BaseModel):
id: UUID

View file

@ -188,9 +188,9 @@ class StoreService(Service):
results = self._get(self.components_url, api_key, params)
results_objects = [ListComponentResponse(**component) for component in results]
# Flatten the tags
for component in results_objects:
if component.tags:
component.tags = [tags_id.tags_id for tags_id in component.tags]
# for component in results_objects:
# if component.tags:
# component.tags = [tags_id.tags_id for tags_id in component.tags]
return results_objects
def download(self, api_key: str, component_id: str) -> DownloadComponentResponse: