Skip to content

Commit 2543a06

Browse files
✨ add func for update type to real obj
1 parent b55b90e commit 2543a06

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

Diff for: fastapi_jsonapi/schema_builder.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,13 @@ def _get_info_from_schema_for_building(
272272
resource_id_field = (str, Field(None), None, {})
273273

274274
for name, field in (schema.model_fields or {}).items():
275-
if isinstance(field.default, RelationshipInfo):
275+
if field.json_schema_extra and isinstance(field.json_schema_extra.get("relationship"), RelationshipInfo):
276276
if includes is not_passed:
277277
pass
278278
elif name not in includes:
279279
# if includes are passed, skip this if name not present!
280280
continue
281-
relationship: RelationshipInfo = field.json_schema_extra["relationship"]
281+
relationship: RelationshipInfo = field.json_schema_extra.get("relationship")
282282
relationship_schema = self.create_relationship_data_schema(
283283
field_name=name,
284284
base_name=base_name,
@@ -295,7 +295,7 @@ def _get_info_from_schema_for_building(
295295
relationships_schema_fields[name] = (relationship_schema, relationship_field)
296296
# works both for to-one and to-many
297297
included_schemas.append((name, field.annotation, relationship.resource_type))
298-
elif name == "id":
298+
elif field.json_schema_extra and name == "id":
299299
id_validators = extract_field_validators(
300300
schema,
301301
include_for_field_names={"id"},
@@ -307,7 +307,7 @@ def _get_info_from_schema_for_building(
307307

308308
# todo: support for union types?
309309
# support custom cast func
310-
resource_id_field = (str, Field(default=field.default), field.annotation, id_validators)
310+
resource_id_field = (str, Field(**field.json_schema_extra), field.annotation, id_validators)
311311
else:
312312
attributes_schema_fields[name] = (field.annotation, field.default)
313313
ConfigOrmMode = ConfigDict(from_attributes=True)
@@ -478,6 +478,17 @@ def find_all_included_schemas(
478478

479479
return can_be_included_schemas
480480

481+
@staticmethod
482+
def string_to_schema(schema) -> Type[BaseModel]:
483+
import importlib
484+
485+
module_class_str = (str(schema).strip("[]").split("["))[-1]
486+
module_name, class_name = module_class_str.rsplit(".", 1)
487+
module = importlib.import_module(module_name)
488+
class_obj = getattr(module, class_name)
489+
schema = class_obj
490+
return schema
491+
481492
def create_jsonapi_object_schemas(
482493
self,
483494
schema: Type[BaseModel],
@@ -490,6 +501,7 @@ def create_jsonapi_object_schemas(
490501
if use_schema_cache and schema in self.object_schemas_cache and includes is not_passed:
491502
return self.object_schemas_cache[schema]
492503

504+
schema = self.string_to_schema(schema) if not hasattr(schema, "model_rebuild") else schema
493505
schema.model_rebuild(_types_namespace=registry.schemas)
494506
base_name = base_name or schema.__name__
495507

0 commit comments

Comments
 (0)