Skip to content

Commit fe2d709

Browse files
committed
require object id on update
1 parent 679f2f8 commit fe2d709

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

fastapi_jsonapi/schema_builder.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ def create_schemas(
135135
schemas_in_patch = self.build_schema_in(
136136
schema_in=schema_in_patch,
137137
schema_name_suffix=schema_name_in_patch_suffix,
138+
id_field_required=True,
138139
)
139140

140141
return BuiltSchemasDTO(
@@ -149,6 +150,7 @@ def build_schema_in(
149150
schema_in: Type[BaseModel],
150151
schema_name_suffix: str = "",
151152
non_optional_relationships: bool = False,
153+
id_field_required: bool = False,
152154
) -> Type[BaseJSONAPIDataInSchema]:
153155
base_schema_name = schema_in.__name__.removesuffix("Schema") + schema_name_suffix
154156

@@ -167,6 +169,7 @@ def build_schema_in(
167169
includes=not_passed,
168170
model_base=BaseJSONAPIItemInSchema,
169171
relationships_required=dto.has_required_relationship,
172+
id_field_required=id_field_required,
170173
)
171174

172175
wrapped_object_jsonapi_schema = pydantic.create_model(
@@ -259,6 +262,8 @@ def _get_info_from_schema_for_building(
259262
if not field.field_info.extra.get("client_can_set_id"):
260263
continue
261264

265+
# todo: support for union types?
266+
# support custom cast func
262267
resource_id_field = (str, Field(**field.field_info.extra), field.outer_type_)
263268
else:
264269
attributes_schema_fields[name] = (field.outer_type_, field.field_info)
@@ -348,6 +353,7 @@ def _build_jsonapi_object(
348353
model_base: Type[JSONAPIObjectSchemaType] = JSONAPIObjectSchema,
349354
use_schema_cache: bool = True,
350355
relationships_required: bool = False,
356+
id_field_required: bool = False,
351357
) -> Type[JSONAPIObjectSchemaType]:
352358
if use_schema_cache and base_name in self.base_jsonapi_object_schemas_cache:
353359
return self.base_jsonapi_object_schemas_cache[base_name]
@@ -362,7 +368,7 @@ def _build_jsonapi_object(
362368

363369
object_jsonapi_schema_fields = {
364370
"attributes": (attributes_schema, ...),
365-
"id": (str, Field(None, **id_field_kw)),
371+
"id": (str, Field(... if id_field_required else None, **id_field_kw)),
366372
}
367373
if includes:
368374
object_jsonapi_schema_fields.update(

0 commit comments

Comments
 (0)