Skip to content

Commit c1cf5eb

Browse files
authored
Merge pull request #79 from Ensembl/bugfix-explain-type
Fix null type in explain endpoint
2 parents cddf5e4 + 91a32db commit c1cf5eb

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

app/api/models/genome.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
AliasPath,
1010
field_serializer,
1111
)
12+
from pydantic.functional_validators import model_validator
1213

1314
from core.logging import InterceptHandler
1415
from core.config import ASSEMBLY_URLS
@@ -87,6 +88,30 @@ class BaseGenomeDetails(BaseModel):
8788
assembly: AssemblyInGenome = None
8889
release: Release = None
8990

91+
@model_validator(mode="before")
92+
@classmethod
93+
def inject_type_from_organism(cls, data):
94+
"""
95+
Build a 'type' object from organism.strainType and organism.strain
96+
before normal field parsing.
97+
"""
98+
if not isinstance(data, dict):
99+
return data
100+
101+
org = data.get("organism") or {}
102+
strain_type = org.get("strainType")
103+
strain = org.get("strain")
104+
105+
# only inject if we actually have both strain_type and strain
106+
if strain_type is not None and strain is not None and "type" not in data:
107+
data = dict(data) # shallow copy to avoid mutating original
108+
data["type"] = {
109+
"kind": strain_type,
110+
"value": strain,
111+
}
112+
113+
return data
114+
90115
@validator("species_taxonomy_id", pre=True)
91116
def convert_int_to_str(cls, value):
92117
return str(value)
@@ -137,11 +162,6 @@ def parse_date(cls, value):
137162
return year + "-" + month
138163

139164
def __init__(self, **data):
140-
if data.get("organism", {}).get("strainType", None):
141-
data["type"] = {
142-
"kind": data.get("organism", {}).get("strainType", ""),
143-
"value": data.get("organism", {}).get("strain", ""),
144-
}
145165
if data.get("attributesInfo", {}).get("AssemblyProviderName", None):
146166
data["assembly_provider"] = {
147167
"name": data.get("attributesInfo", {}).get(

0 commit comments

Comments
 (0)