Skip to content

Commit 936b6f2

Browse files
CosmoVmahenzon
authored andcommitted
fixed openapi
1 parent 95dfa90 commit 936b6f2

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

fastapi_jsonapi/data_layers/sqla_orm.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,11 @@ def _apply_client_generated_id(
154154

155155
extra = data_create.__fields__["id"].field_info.extra
156156
if extra.get("client_can_set_id"):
157-
model_kwargs["id"] = extra["cast"](data_create.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)
158162

159163
return model_kwargs
160164

fastapi_jsonapi/schema_builder.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class BuiltSchemasDTO:
7070
@dataclass(frozen=True)
7171
class SchemasInfoDTO:
7272
# id field
73-
resource_id_field: Tuple[Type, FieldInfo]
73+
resource_id_field: Tuple[Type, FieldInfo, Callable]
7474
# pre-built attributes
7575
attributes_schema: Type[BaseModel]
7676
# relationships
@@ -233,7 +233,7 @@ def _get_info_from_schema_for_building(
233233
relationships_schema_fields = {}
234234
included_schemas: List[Tuple[str, BaseModel, str]] = []
235235
has_required_relationship = False
236-
resource_id_field = (str, Field(None))
236+
resource_id_field = (str, Field(None), None)
237237

238238
for name, field in (schema.__fields__ or {}).items():
239239
if isinstance(field.field_info.extra.get("relationship"), RelationshipInfo):
@@ -259,7 +259,7 @@ def _get_info_from_schema_for_building(
259259
if not field.field_info.extra.get("client_can_set_id"):
260260
continue
261261

262-
resource_id_field = (str, Field(cast=field.outer_type_, **field.field_info.extra))
262+
resource_id_field = (str, Field(**field.field_info.extra), field.outer_type_)
263263
else:
264264
attributes_schema_fields[name] = (field.outer_type_, field.field_info)
265265

@@ -344,15 +344,15 @@ def _build_jsonapi_object(
344344
attributes_schema: Type[TypeSchema],
345345
relationships_schema: Type[TypeSchema],
346346
includes,
347-
resource_id_field: Tuple[Type, FieldInfo],
347+
resource_id_field: Tuple[Type, FieldInfo, Callable],
348348
model_base: Type[JSONAPIObjectSchemaType] = JSONAPIObjectSchema,
349349
use_schema_cache: bool = True,
350350
relationships_required: bool = False,
351351
) -> Type[JSONAPIObjectSchemaType]:
352352
if use_schema_cache and base_name in self.base_jsonapi_object_schemas_cache:
353353
return self.base_jsonapi_object_schemas_cache[base_name]
354354

355-
field_type, field_info = resource_id_field
355+
field_type, field_info, id_cast_func = resource_id_field
356356

357357
object_jsonapi_schema_fields = {
358358
"attributes": (attributes_schema, ...),
@@ -369,6 +369,9 @@ def _build_jsonapi_object(
369369
type=(str, Field(default=resource_type or self._resource_type, description="Resource type")),
370370
__base__=model_base,
371371
)
372+
if id_cast_func:
373+
object_jsonapi_schema._id_cast_func = id_cast_func
374+
372375
if use_schema_cache:
373376
self.base_jsonapi_object_schemas_cache[base_name] = object_jsonapi_schema
374377

0 commit comments

Comments
 (0)