Skip to content

Commit

Permalink
✨ add func for update type to real obj
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidRomanovizc committed May 21, 2024
1 parent b55b90e commit 2543a06
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions fastapi_jsonapi/schema_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,13 @@ def _get_info_from_schema_for_building(
resource_id_field = (str, Field(None), None, {})

for name, field in (schema.model_fields or {}).items():
if isinstance(field.default, RelationshipInfo):
if field.json_schema_extra and isinstance(field.json_schema_extra.get("relationship"), RelationshipInfo):
if includes is not_passed:
pass
elif name not in includes:
# if includes are passed, skip this if name not present!
continue
relationship: RelationshipInfo = field.json_schema_extra["relationship"]
relationship: RelationshipInfo = field.json_schema_extra.get("relationship")
relationship_schema = self.create_relationship_data_schema(
field_name=name,
base_name=base_name,
Expand All @@ -295,7 +295,7 @@ def _get_info_from_schema_for_building(
relationships_schema_fields[name] = (relationship_schema, relationship_field)
# works both for to-one and to-many
included_schemas.append((name, field.annotation, relationship.resource_type))
elif name == "id":
elif field.json_schema_extra and name == "id":
id_validators = extract_field_validators(
schema,
include_for_field_names={"id"},
Expand All @@ -307,7 +307,7 @@ def _get_info_from_schema_for_building(

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

return can_be_included_schemas

@staticmethod
def string_to_schema(schema) -> Type[BaseModel]:
import importlib

module_class_str = (str(schema).strip("[]").split("["))[-1]
module_name, class_name = module_class_str.rsplit(".", 1)
module = importlib.import_module(module_name)
class_obj = getattr(module, class_name)
schema = class_obj
return schema

def create_jsonapi_object_schemas(
self,
schema: Type[BaseModel],
Expand All @@ -490,6 +501,7 @@ def create_jsonapi_object_schemas(
if use_schema_cache and schema in self.object_schemas_cache and includes is not_passed:
return self.object_schemas_cache[schema]

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

Expand Down

0 comments on commit 2543a06

Please sign in to comment.