@@ -272,13 +272,13 @@ def _get_info_from_schema_for_building(
272
272
resource_id_field = (str , Field (None ), None , {})
273
273
274
274
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 ):
276
276
if includes is not_passed :
277
277
pass
278
278
elif name not in includes :
279
279
# if includes are passed, skip this if name not present!
280
280
continue
281
- relationship : RelationshipInfo = field .json_schema_extra [ "relationship" ]
281
+ relationship : RelationshipInfo = field .json_schema_extra . get ( "relationship" )
282
282
relationship_schema = self .create_relationship_data_schema (
283
283
field_name = name ,
284
284
base_name = base_name ,
@@ -295,7 +295,7 @@ def _get_info_from_schema_for_building(
295
295
relationships_schema_fields [name ] = (relationship_schema , relationship_field )
296
296
# works both for to-one and to-many
297
297
included_schemas .append ((name , field .annotation , relationship .resource_type ))
298
- elif name == "id" :
298
+ elif field . json_schema_extra and name == "id" :
299
299
id_validators = extract_field_validators (
300
300
schema ,
301
301
include_for_field_names = {"id" },
@@ -307,7 +307,7 @@ def _get_info_from_schema_for_building(
307
307
308
308
# todo: support for union types?
309
309
# 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 )
311
311
else :
312
312
attributes_schema_fields [name ] = (field .annotation , field .default )
313
313
ConfigOrmMode = ConfigDict (from_attributes = True )
@@ -478,6 +478,17 @@ def find_all_included_schemas(
478
478
479
479
return can_be_included_schemas
480
480
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
+
481
492
def create_jsonapi_object_schemas (
482
493
self ,
483
494
schema : Type [BaseModel ],
@@ -490,6 +501,7 @@ def create_jsonapi_object_schemas(
490
501
if use_schema_cache and schema in self .object_schemas_cache and includes is not_passed :
491
502
return self .object_schemas_cache [schema ]
492
503
504
+ schema = self .string_to_schema (schema ) if not hasattr (schema , "model_rebuild" ) else schema
493
505
schema .model_rebuild (_types_namespace = registry .schemas )
494
506
base_name = base_name or schema .__name__
495
507
0 commit comments