Skip to content

Commit e05c245

Browse files
committed
fix/refactor id_cast_func usage
1 parent 7cc6e60 commit e05c245

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

fastapi_jsonapi/data_layers/sqla_orm.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,10 @@ def _apply_client_generated_id(
154154

155155
extra = data_create.__fields__["id"].field_info.extra
156156
if extra.get("client_can_set_id"):
157-
# TODO: remove crutch, there is no possibility
158-
# to pass cast_func through the field_info.extra
159-
# because all extra kwargs placing in openapi schema and this
160-
# break down /docs endpoint
161-
model_kwargs["id"] = data_create._id_cast_func(data_create.id)
157+
id_value = data_create.id
158+
if cast_type := extra.get("id_cast_func"):
159+
id_value = cast_type(id_value)
160+
model_kwargs["id"] = id_value
162161

163162
return model_kwargs
164163

fastapi_jsonapi/schema_builder.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,15 @@ def _build_jsonapi_object(
354354

355355
field_type, field_info, id_cast_func = resource_id_field
356356

357+
id_field_kw = {
358+
**field_info.extra,
359+
}
360+
if id_cast_func:
361+
id_field_kw.update(id_cast_func=id_cast_func)
362+
357363
object_jsonapi_schema_fields = {
358364
"attributes": (attributes_schema, ...),
359-
"id": (str, Field(None, **field_info.extra)),
365+
"id": (str, Field(None, **id_field_kw)),
360366
}
361367
if includes:
362368
object_jsonapi_schema_fields.update(
@@ -369,8 +375,6 @@ def _build_jsonapi_object(
369375
type=(str, Field(default=resource_type or self._resource_type, description="Resource type")),
370376
__base__=model_base,
371377
)
372-
if id_cast_func:
373-
object_jsonapi_schema._id_cast_func = id_cast_func
374378

375379
if use_schema_cache:
376380
self.base_jsonapi_object_schemas_cache[base_name] = object_jsonapi_schema

0 commit comments

Comments
 (0)