Package fastapi_jsonapi index

fastapi_jsonapi.data_layers.fields.mixins module

Enum mixin module.

class fastapi_jsonapi.data_layers.fields.mixins.MixinEnum(new_class_name, /, names, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: Enum

Extension over enum class from standard library.

classmethod inverse()

Return all inverted items sequence.

classmethod keys()

Get all field keys from Enum.

classmethod names()

Get all field names.

classmethod value_to_enum(value)

Convert value to enum.

classmethod values()

Get all values from Enum.

fastapi_jsonapi.data_layers.sqla module

fastapi_jsonapi.data_layers.sqla.base_model module

class fastapi_jsonapi.data_layers.sqla.base_model.BaseSQLA

Bases: object

async classmethod all(session: AsyncSession, stmt: Select) Type[TypeModel] | Any
async classmethod count(session: AsyncSession, stmt: Select) int
async classmethod create(session: AsyncSession, model: TypeModel, resource_type: str, commit: bool = True, id_: str | None = None, **kwargs) TypeModel
async classmethod delete(session: AsyncSession, model: TypeModel, filters: list[BinaryExpression | bool], resource_type: str, commit: bool = True, id_: str | None = None, **kwargs) None
async classmethod one_or_raise(session: AsyncSession, model: TypeModel, filters: list[BinaryExpression | bool], stmt: Select) TypeModel | Any
classmethod query(model: TypeModel, fields: list | None = None, select_from: TypeModel | None = None, distinct_: bool = False, filters: list[BinaryExpression | bool] | None = None, for_update: dict | None = None, join: list[tuple[TypeModel, Any]] | None = None, jsonapi_join: list[RelationshipInfo] | None = None, number: int | None = None, options: Iterable = (), order: str | UnaryExpression | None = None, size: int | None = None, stmt: Select | None = None) Select
async classmethod update(session: AsyncSession, model: TypeModel, resource_type: str, commit: bool = True, id_: str | None = None, **kwargs) TypeModel

fastapi_jsonapi.data_layers.sqla.orm module

This module is a CRUD interface between resource managers and the sqlalchemy ORM

class fastapi_jsonapi.data_layers.sqla.orm.SqlalchemyDataLayer(model: Type[TypeModel], session: AsyncSession, resource_type: str, disable_collection_count: bool = False, default_collection_count: int = -1, id_name_field: str | None = None, url_id_field: str = 'id', eagerload_includes: bool = True, query: Select | None = None, auto_convert_id_to_column_type: bool = True, **kwargs: Any)

Bases: BaseDataLayer

Sqlalchemy data layer

async after_create_object(obj: TypeModel, model_kwargs: dict, view_kwargs: dict)

Provide additional data after object creation.

Parameters:
  • obj – an object from data layer.

  • model_kwargs – the data validated by pydantic.

  • view_kwargs – kwargs from the resource view.

async after_create_relationship(obj: Any, updated: bool, json_data: dict, relationship_field: str, related_id_field: str, view_kwargs: dict)

Make work after to create a relationship.

Parameters:
  • obj – an object from data layer.

  • updated – True if object was updated else False.

  • json_data – the request params.

  • relationship_field – the model attribute used for relationship.

  • related_id_field – the identifier field of the related model.

  • view_kwargs – kwargs from the resource view.

Return boolean:

True if relationship have changed else False.

async after_delete_object(obj: TypeModel, view_kwargs: dict)

Make work after delete object.

Parameters:
  • obj – an object from data layer.

  • view_kwargs – kwargs from the resource view.

async after_delete_objects(objects: list[TypeModel], view_kwargs: dict)

Any actions after deleting objects.

Parameters:
  • objects – an object from data layer.

  • view_kwargs – kwargs from the resource view.

async after_delete_relationship(obj: Any, updated: bool, json_data: dict, relationship_field: str, related_id_field: str, view_kwargs: dict)

Make work after to delete a relationship.

Parameters:
  • obj – an object from data layer.

  • updated – True if object was updated else False.

  • json_data – the request params.

  • relationship_field – the model attribute used for relationship.

  • related_id_field – the identifier field of the related model.

  • view_kwargs – kwargs from the resource view.

async after_get_collection(collection: Iterable, qs: QueryStringManager, view_kwargs: dict)

Make work after to retrieve a collection of objects.

Parameters:
  • collection – the collection of objects.

  • qs – a querystring manager to retrieve information from url.

  • view_kwargs – kwargs from the resource view.

async after_get_object(obj: Any, view_kwargs: dict)

Make work after to retrieve an object.

Parameters:
  • obj – an object from data layer.

  • view_kwargs – kwargs from the resource view.

async after_get_relationship(obj: Any, related_objects: Iterable, relationship_field: str, related_type_: str, related_id_field: str, view_kwargs: dict)

Make work after to get information about a relationship.

Parameters:
  • obj – an object from data layer.

  • related_objects – related objects of the object.

  • relationship_field – the model attribute used for relationship.

  • related_type – the related resource type.

  • related_id_field – the identifier field of the related model.

  • view_kwargs – kwargs from the resource view.

Return tuple:

the object and related object(s).

async after_update_object(obj: Any, model_kwargs: dict, view_kwargs: dict)

Make work after update object.

Parameters:
  • obj – an object from data layer.

  • model_kwargs – the data validated by schemas.

  • view_kwargs – kwargs from the resource view.

async after_update_relationship(obj: Any, updated: bool, json_data: dict, relationship_field: str, related_id_field: str, view_kwargs: dict)

Make work after to update a relationship.

Parameters:
  • obj – an object from data layer.

  • updated – True if object was updated else False.

  • json_data – the request params.

  • relationship_field – the model attribute used for relationship.

  • related_id_field – the identifier field of the related model.

  • view_kwargs – kwargs from the resource view.

Return boolean:

True if relationship have changed else False.

async apply_relationships(obj: TypeModel, data_create: BaseJSONAPIItemInSchema, action_trigger: Literal['create', 'update']) tuple[dict[str, TypeModel | None], dict[str, list[TypeModel]]]

Handles relationships passed in request

Parameters:
  • obj

  • data_create

  • action_trigger – indicates which one operation triggered relationships applying

Returns:

async atomic_end(success: bool = True, exception: Exception | None = None)
async atomic_start(previous_dl: SqlalchemyDataLayer | None = None)
async before_create_object(model_kwargs: dict, view_kwargs: dict)

Provide additional data before object creation.

Parameters:
  • model_kwargs – the data validated by pydantic.

  • view_kwargs – kwargs from the resource view.

async before_create_relationship(json_data: dict, relationship_field: str, related_id_field: str, view_kwargs: dict)

Make work before to create a relationship.

Parameters:
  • json_data – the request params.

  • relationship_field – the model attribute used for relationship.

  • related_id_field – the identifier field of the related model.

  • view_kwargs – kwargs from the resource view.

Return boolean:

True if relationship have changed else False.

async before_delete_object(obj: TypeModel, view_kwargs: dict)

Make checks before delete object.

Parameters:
  • obj – an object from data layer.

  • view_kwargs – kwargs from the resource view.

async before_delete_objects(objects: list[TypeModel], view_kwargs: dict)

Make checks before deleting objects.

Parameters:
  • objects – an object from data layer.

  • view_kwargs – kwargs from the resource view.

async before_delete_relationship(json_data: dict, relationship_field: str, related_id_field: str, view_kwargs: dict)

Make work before to delete a relationship.

Parameters:
  • json_data – the request params.

  • relationship_field – the model attribute used for relationship.

  • related_id_field – the identifier field of the related model.

  • view_kwargs – kwargs from the resource view.

async before_get_collection(qs: QueryStringManager, view_kwargs: dict)

Make work before to retrieve a collection of objects.

Parameters:
  • qs – a querystring manager to retrieve information from url.

  • view_kwargs – kwargs from the resource view.

async before_get_object(view_kwargs: dict)

Make work before to retrieve an object.

Parameters:

view_kwargs – kwargs from the resource view.

async before_get_relationship(relationship_field: str, related_type_: str, related_id_field: str, view_kwargs: dict)

Make work before to get information about a relationship.

Parameters:
  • relationship_field (str) – the model attribute used for relationship.

  • related_type (str) – the related resource type.

  • related_id_field (str) – the identifier field of the related model.

  • view_kwargs (dict) – kwargs from the resource view.

Return tuple:

the object and related object(s).

async before_update_object(obj: Any, model_kwargs: dict, view_kwargs: dict)

Make checks or provide additional data before update object.

Parameters:
  • obj – an object from data layer.

  • model_kwargs – the data validated by schemas.

  • view_kwargs – kwargs from the resource view.

async before_update_relationship(json_data: dict, relationship_field: str, related_id_field: str, view_kwargs: dict)

Make work before to update a relationship.

Parameters:
  • json_data – the request params.

  • relationship_field – the model attribute used for relationship.

  • related_id_field – the identifier field of the related model.

  • view_kwargs – kwargs from the resource view.

Return boolean:

True if relationship have changed else False.

async classmethod check_object_has_relationship_or_raise(obj: TypeModel, relation_name: str)

Checks that there is relationship with relation_name in obj

Parameters:
  • obj

  • relation_name

async create_object(data_create: BaseJSONAPIItemInSchema, view_kwargs: dict) TypeModel

Create an object through sqlalchemy.

Parameters:
  • data_create – the data validated by pydantic.

  • view_kwargs – kwargs from the resource view.

Returns:

async create_relationship(json_data: dict, relationship_field: str, related_id_field: str, view_kwargs: dict) bool

Create a relationship.

Parameters:
  • json_data – the request params.

  • relationship_field – the model attribute used for relationship.

  • related_id_field – the identifier field of the related model.

  • view_kwargs – kwargs from the resource view.

Returns:

True if relationship have changed else False.

async delete_object(obj: TypeModel, view_kwargs: dict)

Delete an object through sqlalchemy.

Parameters:
  • obj – an item from sqlalchemy.

  • view_kwargs – kwargs from the resource view.

async delete_objects(objects: list[TypeModel], view_kwargs: dict)
async delete_relationship(json_data: dict, relationship_field: str, related_id_field: str, view_kwargs: dict)

Delete a relationship.

Parameters:
  • json_data – the request params.

  • relationship_field – the model attribute used for relationship.

  • related_id_field – the identifier field of the related model.

  • view_kwargs – kwargs from the resource view.

eagerload_includes(qs: QueryStringManager)

Use eagerload feature of sqlalchemy to optimize data retrieval for include querystring parameter.

Parameters:

qs – a querystring manager to retrieve information from url.

Returns:

the query with includes eagerloaded.

async get_collection(qs: QueryStringManager, view_kwargs: dict | None = None, relationship_request_info: RelationshipRequestInfo | None = None) tuple[int, list]

Retrieve a collection of objects through sqlalchemy.

Parameters:
  • qs – a querystring manager to retrieve information from url.

  • view_kwargs – kwargs from the resource view.

  • relationship_request_info – indicates that method was called in fetch relationship request and contains some related data

Returns:

the number of object and the list of objects.

get_fields_options(resource_type: str, qs: QueryStringManager, required_to_load: set | None = None) set
get_filter_expressions(qs: QueryStringManager) list[BinaryExpression] | None
static get_load_only_options(resource_type: str, field_names: Iterable[str]) set
async get_object(view_kwargs: dict, qs: QueryStringManager | None = None, relationship_request_info: RelationshipRequestInfo | None = None) TypeModel

Retrieve an object through sqlalchemy.

Parameters:
  • view_kwargs – kwargs from the resource view

  • qs

  • relationship_request_info

Return DeclarativeMeta:

an object from sqlalchemy

Fetch related objects (many)

Parameters:
  • related_model

  • related_id_field

  • ids

Returns:

async get_relationship(relationship_field: str, related_type_: str, related_id_field: str, view_kwargs: dict) tuple[Any, Any]

Get a relationship.

Parameters:
  • relationship_field – the model attribute used for relationship.

  • related_type – the related resource type.

  • related_id_field – the identifier field of the related model.

  • view_kwargs – kwargs from the resource view.

Returns:

the object and related object(s).

get_relationship_request_filters(model_id_field: InstrumentedAttribute, parent_obj_id: Any, parent_resource_type: str, relationship_name: str) list[BinaryExpression]
get_sort_expressions(qs: QueryStringManager) list | None
prepare_id_value(col: InstrumentedAttribute, value: Any) Any

Convert value to the required python type.

Type is declared on the SQLA column.

Parameters:
  • col

  • value

Returns:

async update_object(obj: TypeModel, data_update: BaseJSONAPIItemInSchema, view_kwargs: dict) bool

Update an object through sqlalchemy.

Parameters:
  • obj – an object from sqlalchemy.

  • data_update – the data validated by pydantic.

  • view_kwargs – kwargs from the resource view.

Returns:

True if object have changed else False.

async update_relationship(json_data: dict, relationship_field: str, related_id_field: str, view_kwargs: dict) bool

Update a relationship

Parameters:
  • json_data – the request params.

  • relationship_field – the model attribute used for relationship.

  • related_id_field – the identifier field of the related model.

  • view_kwargs – kwargs from the resource view.

Returns:

True if relationship have changed else False.

fastapi_jsonapi.data_layers.sqla.query_building module

Helper to create sqlalchemy filters according to filter querystring parameter

class fastapi_jsonapi.data_layers.sqla.query_building.RelationshipInfo(*, target_schema: Type[TypeSchema], model: Type[TypeModel], aliased_model: AliasedClass, join_column: InstrumentedAttribute)

Bases: BaseModel

aliased_model: AliasedClass
join_column: InstrumentedAttribute
model: Type[TypeModel]
model_config: ClassVar[ConfigDict] = {'arbitrary_types_allowed': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

target_schema: Type[TypeSchema]
class fastapi_jsonapi.data_layers.sqla.query_building.RelationshipInfoStorage

Bases: object

get_info(resource_type: str, path: str) RelationshipInfo
has_info(resource_type: str, path: str) bool
set_info(resource_type: str, path: str, info: RelationshipInfo)
fastapi_jsonapi.data_layers.sqla.query_building.build_filter_expression(schema_field: FieldInfo, model_column: InstrumentedAttribute, operator: str, value: Any) BinaryExpression

Builds sqlalchemy filter expression, like YourModel.some_field == value

Custom sqlalchemy filtering logic can be created in a schemas field for any operator To implement a new filtering logic (override existing or create a new one) create a method inside a field following this pattern: _<your_op_name>_sql_filter_

Parameters:
  • schema_field – schemas field instance

  • model_column – sqlalchemy column instance

  • operator – your operator, for example: “eq”, “in”, “ilike_str_array”, …

  • value – filtering value

fastapi_jsonapi.data_layers.sqla.query_building.build_filter_expressions(filter_item: dict, target_schema: Type[TypeSchema], target_model: Type[TypeModel], entrypoint_resource_type: str) BinaryExpression

Return sqla expressions.

Builds sqlalchemy expression which can be use in where condition: query(Model).where(build_filter_expressions(…))

fastapi_jsonapi.data_layers.sqla.query_building.build_sort_expressions(sort_items: list[dict], target_schema: Type[TypeSchema], target_model: Type[TypeModel], entrypoint_resource_type: str)
fastapi_jsonapi.data_layers.sqla.query_building.build_terminal_node_filter_expressions(filter_item: dict, target_schema: Type[TypeSchema], target_model: Type[TypeModel], entrypoint_resource_type: str)
fastapi_jsonapi.data_layers.sqla.query_building.cast_value_with_schema(field_types: list[Type], value: Any) tuple[Any, list[str]]
fastapi_jsonapi.data_layers.sqla.query_building.gather_relationship_paths(item: dict | list[dict]) set[str]

Extracts relationship paths from query filter

fastapi_jsonapi.data_layers.sqla.query_building.gather_relationships(entrypoint_resource_type: str, entrypoint_model: Type[TypeModel], schema: Type[TypeSchema], relationship_paths: set[str]) set[str]
fastapi_jsonapi.data_layers.sqla.query_building.gather_relationships_info(model: Type[TypeModel], schema: Type[TypeSchema], entrypoint_resource_type: str, relationship_path: list[str], target_relationship_idx: int = 0, prev_aliased_model: Any | None = None) dict[str, RelationshipInfo]
fastapi_jsonapi.data_layers.sqla.query_building.get_model_column(model: Type[TypeModel], schema: Type[TypeSchema], field_name: str) InstrumentedAttribute
fastapi_jsonapi.data_layers.sqla.query_building.get_operator(model_column: InstrumentedAttribute, operator_name: str) str

Get the function operator from his name

Return callable:

a callable to make operation on a column

fastapi_jsonapi.data_layers.sqla.query_building.is_filtering_terminal_node(filter_item: dict) bool

If node shape is:

{

“name: …, “op: …, “val: …,

}

fastapi_jsonapi.data_layers.sqla.query_building.is_relationship_filter(name: str) bool
fastapi_jsonapi.data_layers.sqla.query_building.prepare_relationships_info(model: Type[TypeModel], schema: Type[TypeSchema], resource_type: str, filter_info: list, sorting_info: list) set[str]

Return set with request relationship paths in dot separated format.

Gathers information about all relationships involved to request and save them data for skip extra computations for the next time.

For the filter like this:
filter_info = [

{“field”: “foo.bar.field_name”, “op”: “eq”, “val”: “”}, {“field”: “baz.field_name”, “op”: “eq”, “val”: “”},

]

It returns:

(“foo.bar”, “baz”)

fastapi_jsonapi.data_layers.base module

The base class of a data layer.

If you want to create your own data layer you must inherit from this base class

class fastapi_jsonapi.data_layers.base.BaseDataLayer(request: Request, model: Type[TypeModel], schema: Type[TypeSchema], resource_type: str, url_id_field: str = 'id', disable_collection_count: bool = False, default_collection_count: int = -1, **kwargs)

Bases: object

Base class of a data layer

async after_create_object(obj, data, view_kwargs)

Provide additional data after object creation

Parameters:
  • obj – an object from data layer

  • data – the data validated by schemas

  • view_kwargs – kwargs from the resource view

async after_create_relationship(obj, updated, json_data, relationship_field, related_id_field, view_kwargs)

Make work after to create a relationship

Parameters:
  • obj – an object from data layer

  • updated (bool) – True if object was updated else False

  • json_data – the request params

  • relationship_field (str) – the model attribute used for relationship

  • related_id_field (str) – the identifier field of the related model

  • view_kwargs – kwargs from the resource view

Return boolean:

True if relationship have changed else False

async after_delete_object(obj: TypeModel, view_kwargs)

Make work after delete object

Parameters:
  • obj – an object from data layer

  • view_kwargs – kwargs from the resource view

async after_delete_objects(objects: list[TypeModel], view_kwargs: dict)

Any action after deleting objects.

Parameters:
  • objects – an object from data layer.

  • view_kwargs – kwargs from the resource view.

async after_delete_relationship(obj, updated, json_data, relationship_field, related_id_field, view_kwargs)

Make work after to delete a relationship

Parameters:
  • obj – an object from data layer

  • updated (bool) – True if object was updated else False

  • json_data – the request params

  • relationship_field (str) – the model attribute used for relationship

  • related_id_field (str) – the identifier field of the related model

  • view_kwargs – kwargs from the resource view

async after_get_collection(collection, qs, view_kwargs)

Make work after to retrieve a collection of objects

Parameters:
  • collection (iterable) – the collection of objects

  • qs – a querystring manager to retrieve information from url

  • view_kwargs – kwargs from the resource view

async after_get_object(obj, view_kwargs)

Make work after to retrieve an object

Parameters:
  • obj – an object from data layer

  • view_kwargs – kwargs from the resource view

async after_get_relationship(obj, related_objects, relationship_field, related_type_, related_id_field, view_kwargs)

Make work after to get information about a relationship

Parameters:
  • obj – an object from data layer

  • related_objects (iterable) – related objects of the object

  • relationship_field (str) – the model attribute used for relationship

  • related_type (str) – the related resource type

  • related_id_field (str) – the identifier field of the related model

  • view_kwargs – kwargs from the resource view

Return tuple:

the object and related object(s)

async after_update_object(obj: TypeModel, data, view_kwargs)

Make work after update object

Parameters:
  • obj – an object from data layer

  • data – the data validated by schemas

  • view_kwargs – kwargs from the resource view

async after_update_relationship(obj, updated, json_data, relationship_field, related_id_field, view_kwargs)

Make work after to update a relationship

Parameters:
  • obj – an object from data layer

  • updated (bool) – True if object was updated else False

  • json_data – the request params

  • relationship_field (str) – the model attribute used for relationship

  • related_id_field (str) – the identifier field of the related model

  • view_kwargs – kwargs from the resource view

Return boolean:

True if relationship have changed else False

async atomic_end(success: bool = True, exception: Exception | None = None)
async atomic_start(previous_dl: BaseDataLayer | None = None)
async before_create_object(data, view_kwargs)

Provide additional data before object creation

Parameters:
  • data – the data validated by schemas

  • view_kwargs – kwargs from the resource view

async before_create_relationship(json_data, relationship_field, related_id_field, view_kwargs)

Make work before to create a relationship

Parameters:
  • json_data – the request params

  • relationship_field (str) – the model attribute used for relationship

  • related_id_field (str) – the identifier field of the related model

  • view_kwargs – kwargs from the resource view

Return boolean:

True if relationship have changed else False

async before_delete_object(obj: TypeModel, view_kwargs)

Make checks before delete object

Parameters:
  • obj – an object from data layer

  • view_kwargs – kwargs from the resource view

async before_delete_objects(objects: list[TypeModel], view_kwargs: dict)

Make checks before deleting objects.

Parameters:
  • objects – an object from data layer.

  • view_kwargs – kwargs from the resource view.

async before_delete_relationship(json_data, relationship_field, related_id_field, view_kwargs)

Make work before to delete a relationship

Parameters:
  • json_data – the request params

  • relationship_field (str) – the model attribute used for relationship

  • related_id_field (str) – the identifier field of the related model

  • view_kwargs – kwargs from the resource view

async before_get_collection(qs, view_kwargs)

Make work before to retrieve a collection of objects

Parameters:
  • qs – a querystring manager to retrieve information from url

  • view_kwargs – kwargs from the resource view

async before_get_object(view_kwargs)

Make work before to retrieve an object

Parameters:

view_kwargs – kwargs from the resource view

async before_get_relationship(relationship_field, related_type_, related_id_field, view_kwargs)

Make work before to get information about a relationship

Parameters:
  • relationship_field (str) – the model attribute used for relationship

  • related_type (str) – the related resource type

  • related_id_field (str) – the identifier field of the related model

  • view_kwargs – kwargs from the resource view

Return tuple:

the object and related object(s)

async before_update_object(obj, data, view_kwargs)

Make checks or provide additional data before update object

Parameters:
  • obj – an object from data layer

  • data – the data validated by schemas

  • view_kwargs – kwargs from the resource view

async before_update_relationship(json_data, relationship_field, related_id_field, view_kwargs)

Make work before to update a relationship

Parameters:
  • json_data – the request params

  • relationship_field (str) – the model attribute used for relationship

  • related_id_field (str) – the identifier field of the related model

  • view_kwargs – kwargs from the resource view

Return boolean:

True if relationship have changed else False

async create_object(data_create: BaseJSONAPIItemInSchema, view_kwargs: dict) TypeModel

Create an object

Parameters:
  • data_create – validated data

  • view_kwargs – kwargs from the resource view

Return DeclarativeMeta:

an object

async create_relationship(json_data, relationship_field, related_id_field, view_kwargs)

Create a relationship

Parameters:
  • json_data – the request params

  • relationship_field (str) – the model attribute used for relationship

  • related_id_field (str) – the identifier field of the related model

  • view_kwargs – kwargs from the resource view

Return boolean:

True if relationship have changed else False

async delete_object(obj, view_kwargs)

Delete an item through the data layer

Parameters:
  • obj (DeclarativeMeta) – an object

  • view_kwargs – kwargs from the resource view

async delete_objects(objects: list[TypeModel], view_kwargs)
async delete_relationship(json_data, relationship_field, related_id_field, view_kwargs)

Delete a relationship

Parameters:
  • json_data – the request params

  • relationship_field (str) – the model attribute used for relationship

  • related_id_field (str) – the identifier field of the related model

  • view_kwargs – kwargs from the resource view

async get_collection(qs: QueryStringManager, view_kwargs: dict | None = None, relationship_request_info: RelationshipRequestInfo | None = None) tuple[int, list]

Retrieve a collection of objects

Parameters:
  • qs – a querystring manager to retrieve information from url

  • view_kwargs – kwargs from the resource view

  • relationship_request_info

Return tuple:

the number of object and the list of objects

async get_object(view_kwargs: dict, qs: QueryStringManager | None = None, relationship_request_info: RelationshipRequestInfo | None = None) TypeModel

Retrieve an object

Parameters:
  • view_kwargs – kwargs from the resource view

  • qs

  • relationship_request_info

Return DeclarativeMeta:

an object

Get related objects list.

Parameters:
  • related_model – Related ORM model class (not instance)

  • related_id_field – id field of the related model (usually it’s id)

  • ids – related object id values list

Returns:

a list of ORM objects

async get_relationship(relationship_field, related_type_, related_id_field, view_kwargs)

Get information about a relationship

Parameters:
  • relationship_field (str) – the model attribute used for relationship

  • related_type (str) – the related resource type

  • related_id_field (str) – the identifier field of the related model

  • view_kwargs – kwargs from the resource view

Return tuple:

the object and related object(s)

async update_object(obj, data_update: BaseJSONAPIItemInSchema, view_kwargs: dict)

Update an object

Parameters:
  • obj – an object

  • data_update – the data validated by schemas

  • view_kwargs – kwargs from the resource view

Return boolean:

True if object have changed else False

async update_relationship(json_data, relationship_field, related_id_field, view_kwargs)

Update a relationship

Parameters:
  • json_data – the request params

  • relationship_field (str) – the model attribute used for relationship

  • related_id_field (str) – the identifier field of the related model

  • view_kwargs – kwargs from the resource view

Return boolean:

True if relationship have changed else False

fastapi_jsonapi.data_typing module

fastapi_jsonapi.fastapi_jsonapi.misc.sqla.generics.base module

class fastapi_jsonapi.misc.sqla.generics.base.ViewBaseGeneric(*, request: Request, resource_type: str, operation: Operation, model: Type[TypeModel], schema: Type[TypeSchema], **options)

Bases: ViewBase

data_layer_cls

alias of SqlalchemyDataLayer

fastapi_jsonapi.storages module

fastapi_jsonapi.storages.storages.models_storage module

class fastapi_jsonapi.storages.models_storage.ModelsStorage

Bases: object

add_model(resource_type: str, model: Type[TypeModel], id_field_name: str)
get_model(resource_type: str) Type[TypeModel]
get_model_id_field_name(resource_type: str) str
get_object_id(db_object: TypeModel, resource_type: str) Any
get_object_id_field(resource_type: str) Any
register_search_handler(orm_mode: str, handler: Callable[[str, Type[TypeModel], str], Type[TypeModel]])
relationship_search_handlers: dict[str, Callable[[str, Type[TypeModel], str], Type[TypeModel]]]
search_relationship_model(resource_type: str, model: Type[TypeModel], field_name: str) Type[TypeModel]
set_orm_mode(orm_mode: str)
static sqla_search_relationship_model(resource_type: str, model: Type[TypeModel], field_name: str)

fastapi_jsonapi.storages.schemas_storage module

class fastapi_jsonapi.storages.schemas_storage.SchemasStorage

Bases: object

add_jsonapi_object_schema(source_schema: Type[TypeSchema], resource_type: str, jsonapi_object_schema: Type[TypeSchema])
add_relationship(from_resource_type: str, to_resource_type: str, operation_type: Literal['create', 'update', 'get'], field_name: str, relationship_schema: Type[TypeSchema], relationship_info: RelationshipInfo)
add_resource(builder, resource_type: str, operation_type: Literal['create', 'update', 'get'], source_schema: Type[TypeSchema], data_schema: Type[TypeSchema], attributes_schema: Type[TypeSchema], field_schemas: dict[str, Type[TypeSchema]], relationships_info: dict[str, tuple[RelationshipInfo, Any]], model_validators: dict, schema_in: Type[TypeSchema] | None = None)
get_attrs_schema(resource_type: str, operation_type: Literal['create', 'update', 'get']) TypeSchema | None
get_data_schema(resource_type: str, operation_type: Literal['create', 'update', 'get']) TypeSchema | None
get_field_schema(resource_type: str, operation_type: Literal['create', 'update', 'get'], field_name: str) TypeSchema | None
get_jsonapi_object_schema(source_schema: Type[TypeSchema], resource_type: str) JSONAPIObjectSchemas | None
get_model_validators(resource_type: str, operation_type: Literal['create', 'update', 'get']) tuple[dict, dict]
get_relationship_info(resource_type: str, operation_type: Literal['create', 'update', 'get'], field_name: str) RelationshipInfo | None
get_relationship_schema(from_resource_type: str, to_resource_type: str, operation_type: Literal['create', 'update', 'get'], field_name: str) TypeSchema | None
get_relationships_info(resource_type: str, operation_type: Literal['create', 'update', 'get']) dict[str, RelationshipInfo]
get_schema_in(resource_type: str, operation_type: Literal['create', 'update']) Type[TypeSchema]
get_source_relationship_pydantic_field(resource_type: str, operation_type: Literal['create', 'update', 'get'], field_name: str)
get_source_schema(resource_type: str)
has_operation(resource_type: str, operation_type: Literal['create', 'update', 'get']) bool
has_resource(resource_type: str) bool

fastapi_jsonapi.storages.views_storage module

class fastapi_jsonapi.storages.views_storage.ViewStorage

Bases: object

add_view(resource_type: str, view: Type[ViewBase])
get_view(resource_type: str) Type[ViewBase]
has_view(resource_type: str) bool

fastapi_jsonapi.api module

fastapi_jsonapi.api.application_builder module

class fastapi_jsonapi.api.application_builder.ApplicationBuilder(app: FastAPI, base_router: APIRouter | None = None, exception_handler: Callable | None = None, **base_router_include_kwargs)

Bases: object

add_resource(path: str, tags: Iterable[str], resource_type: str, view: Type[ViewBase], model: Type[TypeModel], schema: Type[BaseModel], router: APIRouter | None = None, schema_in_post: Type[BaseModel] | None = None, schema_in_patch: Type[BaseModel] | None = None, pagination_default_size: int | None = 25, pagination_default_number: int | None = 1, pagination_default_offset: int | None = None, pagination_default_limit: int | None = None, operations: Iterable[str] = (), ending_slash: bool = True, model_id_field_name: str = 'id', include_router_kwargs: dict | None = None)
initialize() FastAPI
exception fastapi_jsonapi.api.application_builder.ApplicationBuilderError

Bases: Exception

fastapi_jsonapi.api.endpoint_builder module

class fastapi_jsonapi.api.endpoint_builder.EndpointsBuilder(resource_type: str, data: ResourceData)

Bases: object

create_common_fastapi_endpoint(operation: Operation, **view_options) tuple[str, Callable]
create_delete_fastapi_endpoint(resource_type: str, operation: Operation, view: Type[ViewBase], model: Type[TypeModel], source_schema: Type[TypeSchema], **view_options)
create_delete_list_fastapi_endpoint(resource_type: str, operation: Operation, view: Type[ViewBase], model: Type[TypeModel], source_schema: Type[TypeSchema], **view_options)
create_get_fastapi_endpoint(resource_type: str, operation: Operation, view: Type[ViewBase], model: Type[TypeModel], source_schema: Type[TypeSchema], **view_options)
create_get_list_fastapi_endpoint(resource_type: str, operation: Operation, view: Type[ViewBase], model: Type[TypeModel], source_schema: Type[TypeSchema], **view_options)
create_get_relationship_fastapi_endpoint(resource_type: str, relationship_name: str, parent_resource_type: str, view: Type[ViewBase], model: Type[TypeModel], source_schema: Type[TypeSchema], **view_options)
create_get_relationship_list_fastapi_endpoint(resource_type: str, relationship_name: str, parent_resource_type: str, view: Type[ViewBase], model: Type[TypeModel], source_schema: Type[TypeSchema], **view_options)
create_post_fastapi_endpoint(resource_type: str, operation: Operation, view: Type[ViewBase], model: Type[TypeModel], source_schema: Type[TypeSchema], schema_in_post_data: Type[TypeSchema], **view_options)
create_relationship_endpoint(resource_type: str, operation: Operation, relationship_name: str, parent_resource_type: str, view: Type[ViewBase], model: Type[TypeModel], source_schema: Type[TypeSchema]) tuple[str, Callable]
create_update_fastapi_endpoint(resource_type: str, operation: Operation, view: Type[ViewBase], model: Type[TypeModel], source_schema: Type[TypeSchema], schema_in_patch_data: Type[TypeSchema], **view_options)
get_common_endpoint_name(resource_type: str, operation: Operation)

Generate endpoint name

static get_endpoint_kind(operation: Operation) str
get_relationship_endpoint_name(resource_type: str, relationship_name: str, operation: Operation)
class fastapi_jsonapi.api.endpoint_builder.OperationAlreadyHandled

Bases: object

fastapi_jsonapi.api.schemas module

class fastapi_jsonapi.api.schemas.ResourceData(*, path: str | list[str], tags: list[str], view: Type[ViewBase], model: Type[TypeModel], source_schema: Type[TypeSchema], schema_in_post: Type[BaseModel] | None, schema_in_post_data: Type[BaseModel], schema_in_patch: Type[BaseModel] | None, schema_in_patch_data: Type[BaseModel], detail_response_schema: Type[BaseModel], list_response_schema: Type[BaseModel], pagination_default_size: int | None = 25, pagination_default_number: int | None = 1, pagination_default_offset: int | None = None, pagination_default_limit: int | None = None, operations: Iterable[Operation] = (), ending_slash: bool = True)

Bases: BaseModel

detail_response_schema: Type[BaseModel]
ending_slash: bool
list_response_schema: Type[BaseModel]
model: Type[TypeModel]
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

operations: Iterable[Operation]
pagination_default_limit: int | None
pagination_default_number: int | None
pagination_default_offset: int | None
pagination_default_size: int | None
path: str | list[str]
schema_in_patch: Type[BaseModel] | None
schema_in_patch_data: Type[BaseModel]
schema_in_post: Type[BaseModel] | None
schema_in_post_data: Type[BaseModel]
source_schema: Type[TypeSchema]
tags: list[str]
view: Type[ViewBase]

fastapi_jsonapi.querystring module

Helper to deal with querystring parameters according to jsonapi specification.

class fastapi_jsonapi.querystring.HeadersQueryStringManager(*, host: str | None = None, connection: str | None = None, accept: str | None = None, user_agent: str | None = None, referer: str | None = None, accept_encoding: str | None = None, accept_language: str | None = None)

Bases: BaseModel

Header query string manager.

Contains info about request headers.

accept: str | None
accept_encoding: str | None
accept_language: str | None
connection: str | None
host: str | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

referer: str | None
user_agent: str | None
class fastapi_jsonapi.querystring.PaginationQueryStringManager(*, offset: int | None = None, size: int | None = 25, number: int = 1, limit: int | None = None)

Bases: BaseModel

Pagination query string manager.

Contains info about offsets, sizes, number and limits of query with pagination.

limit: int | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

number: int
offset: int | None
size: int | None
class fastapi_jsonapi.querystring.QueryStringManager(request: Request)

Bases: object

Querystring parser according to jsonapi reference.

classmethod extract_item_key(key: str) str
property fields: dict[str, set]

Return fields wanted by client.

Returns:

a dict of sparse fieldsets information

Return value will be a dict containing all fields by resource, for example:

{
    "user": ['name', 'email'],
}
Raises:

InvalidField – if result field not in schema.

property filters: list[dict]

Return filters from query string.

Returns:

filter information

Raises:

InvalidFilters – if filter loading from json has failed.

property include: list[str]

Return fields to include.

Returns:

a list of include information.

Raises:

InvalidInclude – if nesting is more than MAX_INCLUDE_DEPTH.

managed_keys = ('filter', 'page', 'fields', 'sort', 'include', 'q')
property pagination: PaginationQueryStringManager

Return all page parameters as a dict.

Returns:

a dict of pagination information.

To allow multiples strategies, all parameters starting with page will be included. e.g:

{
    "number": '25',
    "size": '150',
}

Example with number strategy:

query_string = {‘page[number]’: ‘25’, ‘page[size]’: ‘10’} parsed_query.pagination {‘number’: ‘25’, ‘size’: ‘10’}

Raises:

BadRequest – if the client is not allowed to disable pagination.

property querystring: dict[str, str]

Return original querystring but containing only managed keys.

Returns:

dict of managed querystring parameter

property sorts: list[dict]

fastapi_jsonapi.schema module

Base JSON:API schemas.

Pydantic (for FastAPI).

class fastapi_jsonapi.schema.BaseJSONAPIDataInSchema(**data: Any)

Bases: BaseModel

data: BaseJSONAPIItemInSchema
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class fastapi_jsonapi.schema.BaseJSONAPIItemInSchema(*, type: str, attributes: dict)

Bases: BaseJSONAPIItemSchema

Schema for post/patch method

TODO POST: optionally accept custom id for object https://jsonapi.org/format/#crud-creating-client-ids TODO PATCH: accept object id (maybe create a new separate schema)

attributes: TypeSchema
id: str | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

relationships: TypeSchema | None
class fastapi_jsonapi.schema.BaseJSONAPIItemSchema(*, type: str, attributes: dict)

Bases: BaseModel

Base JSON:API item schema.

attributes: dict
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

type: str
class fastapi_jsonapi.schema.BaseJSONAPIObjectSchema(*, type: str, attributes: dict, id: str)

Bases: BaseJSONAPIItemSchema

Base JSON:API object schema.

id: str
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class fastapi_jsonapi.schema.BaseJSONAPIRelationshipDataToManySchema(*, data: list[BaseJSONAPIRelationshipSchema])

Bases: BaseModel

data: list[BaseJSONAPIRelationshipSchema]
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class fastapi_jsonapi.schema.BaseJSONAPIRelationshipDataToOneSchema(*, data: BaseJSONAPIRelationshipSchema)

Bases: BaseModel

data: BaseJSONAPIRelationshipSchema
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class fastapi_jsonapi.schema.BaseJSONAPIRelationshipSchema(*, id: str, type: str)

Bases: BaseModel

id: str
model_config: ClassVar[ConfigDict] = {'extra': 'forbid'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

type: str
class fastapi_jsonapi.schema.BaseJSONAPIResultSchema(*, meta: JSONAPIResultListMetaSchema | None = None, jsonapi: JSONAPIDocumentObjectSchema = JSONAPIDocumentObjectSchema(version='1.0'))

Bases: BaseModel

JSON:API Required fields schema

jsonapi: JSONAPIDocumentObjectSchema
meta: JSONAPIResultListMetaSchema | None
model_config: ClassVar[ConfigDict] = {'from_attributes': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class fastapi_jsonapi.schema.BuiltSchemasDTO(schema_in_post: 'Type[BaseJSONAPIDataInSchema]', schema_in_post_data: 'Type[BaseJSONAPIItemInSchema]', schema_in_patch: 'Type[BaseJSONAPIDataInSchema]', schema_in_patch_data: 'Type[BaseJSONAPIItemInSchema]', detail_response_schema: 'Type[JSONAPIResultDetailSchema]', list_response_schema: 'Type[JSONAPIResultListSchema]')

Bases: object

detail_response_schema: Type[JSONAPIResultDetailSchema]
list_response_schema: Type[JSONAPIResultListSchema]
schema_in_patch: Type[BaseJSONAPIDataInSchema]
schema_in_patch_data: Type[BaseJSONAPIItemInSchema]
schema_in_post: Type[BaseJSONAPIDataInSchema]
schema_in_post_data: Type[BaseJSONAPIItemInSchema]
class fastapi_jsonapi.schema.JSONAPIDocumentObjectSchema(*, version: str = '1.0')

Bases: BaseModel

JSON:API Document Object Schema.

https://jsonapi.org/format/#document-jsonapi-object

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

version: str
class fastapi_jsonapi.schema.JSONAPIObjectSchema(*, type: str, attributes: dict, id: str)

Bases: BaseJSONAPIObjectSchema

JSON:API base object schema.

model_config: ClassVar[ConfigDict] = {'from_attributes': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class fastapi_jsonapi.schema.JSONAPIObjectSchemas(attributes_schema: 'Type[BaseModel]', relationships_schema: 'Type[BaseModel]', object_jsonapi_schema: 'Type[JSONAPIObjectSchema]', can_be_included_schemas: 'dict[str, Type[JSONAPIObjectSchema]]')

Bases: object

attributes_schema: Type[BaseModel]
can_be_included_schemas: dict[str, Type[JSONAPIObjectSchema]]
property included_schemas_list: list[Type[JSONAPIObjectSchema]]
object_jsonapi_schema: Type[JSONAPIObjectSchema]
relationships_schema: Type[BaseModel]
class fastapi_jsonapi.schema.JSONAPIResultDetailSchema(*, meta: JSONAPIResultListMetaSchema | None = None, jsonapi: JSONAPIDocumentObjectSchema = JSONAPIDocumentObjectSchema(version='1.0'), data: JSONAPIObjectSchema)

Bases: BaseJSONAPIResultSchema

JSON:API base detail schema.

data: JSONAPIObjectSchema
model_config: ClassVar[ConfigDict] = {'from_attributes': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class fastapi_jsonapi.schema.JSONAPIResultListMetaSchema(*, count: int | None, totalPages: int | None)

Bases: BaseModel

JSON:API list meta schema.

count: int | None
model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'populate_by_name': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

total_pages: int | None
class fastapi_jsonapi.schema.JSONAPIResultListSchema(*, meta: JSONAPIResultListMetaSchema | None = None, jsonapi: JSONAPIDocumentObjectSchema = JSONAPIDocumentObjectSchema(version='1.0'), data: Sequence[JSONAPIObjectSchema])

Bases: BaseJSONAPIResultSchema

JSON:API list base result schema.

data: Sequence[JSONAPIObjectSchema]
model_config: ClassVar[ConfigDict] = {'from_attributes': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

exception fastapi_jsonapi.schema.JSONAPISchemaIntrospectionError

Bases: Exception

class fastapi_jsonapi.schema.SchemasInfoDTO(resource_id_field: 'tuple[Type, FieldInfo, Callable, FieldValidators]', attributes_schema: 'Type[BaseModel]', relationships_schema: 'Type[BaseModel]', has_required_relationship: 'bool', included_schemas: 'list[tuple[str, BaseModel, str]]', relationships_info: 'dict[str, tuple[RelationshipInfo, Any]]', field_schemas: 'dict[str, Type[BaseModel]]', model_validators: 'dict')

Bases: object

attributes_schema: Type[BaseModel]
field_schemas: dict[str, Type[BaseModel]]
has_required_relationship: bool
included_schemas: list[tuple[str, BaseModel, str]]
model_validators: dict
relationships_info: dict[str, tuple[RelationshipInfo, Any]]
relationships_schema: Type[BaseModel]
resource_id_field: tuple[Type, FieldInfo, Callable, dict[str, Callable]]
fastapi_jsonapi.schema.get_model_field(schema: Type['TypeSchema'], field: str) str

Get the model field of a schema field.

# todo: use alias (custom names)?

For example:

class Computer(sqla_base):

user = relationship(User)

class ComputerSchema(pydantic_base):

owner = Field(alias=”user”, relationship=…)

Parameters:
  • schema – a pydantic schema

  • field – the name of the schema field

Returns:

the name of the field in the model

Raises:

Exception – if the schema from parameter has no attribute for parameter.

Retrieve the related schema of a relationship field.

Params schema:

the schema to retrieve le relationship field from

Params field:

the relationship field

Returns:

the related schema

fastapi_jsonapi.schema.get_relationship_fields_names(schema: Type['TypeSchema']) set[str]

Return relationship fields of a schema.

Parameters:

schema – a schemas schema

fastapi_jsonapi.schema.get_schema_from_field_annotation(field: FieldInfo) Type[TypeSchema] | None
fastapi_jsonapi.schema.get_schema_from_type(resource_type: str, app: FastAPI) Type[BaseModel]

Retrieve a schema from the registry by his type.

Parameters:
  • resource_type – the type of the resource.

  • app – FastAPI app instance.

Return Schema:

the schema class.

Raises:

Exception – if the schema not found for this resource type.

fastapi_jsonapi.schema_base module

class fastapi_jsonapi.schema_base.BaseModel

Bases: RegistryMeta

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

fastapi_jsonapi.schema_base.Field(default: Any = PydanticUndefined, *, default_factory: Callable[[], Any] | Callable[[dict[str, Any]], Any] | None = PydanticUndefined, alias: str | None = PydanticUndefined, alias_priority: int | None = PydanticUndefined, validation_alias: str | AliasPath | AliasChoices | None = PydanticUndefined, serialization_alias: str | None = PydanticUndefined, title: str | None = PydanticUndefined, field_title_generator: Callable[[str, FieldInfo], str] | None = PydanticUndefined, description: str | None = PydanticUndefined, examples: list[Any] | None = PydanticUndefined, exclude: bool | None = PydanticUndefined, discriminator: str | types.Discriminator | None = PydanticUndefined, deprecated: Deprecated | str | bool | None = PydanticUndefined, json_schema_extra: JsonDict | Callable[[JsonDict], None] | None = PydanticUndefined, frozen: bool | None = PydanticUndefined, validate_default: bool | None = PydanticUndefined, repr: bool = PydanticUndefined, init: bool | None = PydanticUndefined, init_var: bool | None = PydanticUndefined, kw_only: bool | None = PydanticUndefined, pattern: str | Pattern[str] | None = PydanticUndefined, strict: bool | None = PydanticUndefined, coerce_numbers_to_str: bool | None = PydanticUndefined, gt: annotated_types.SupportsGt | None = PydanticUndefined, ge: annotated_types.SupportsGe | None = PydanticUndefined, lt: annotated_types.SupportsLt | None = PydanticUndefined, le: annotated_types.SupportsLe | None = PydanticUndefined, multiple_of: float | None = PydanticUndefined, allow_inf_nan: bool | None = PydanticUndefined, max_digits: int | None = PydanticUndefined, decimal_places: int | None = PydanticUndefined, min_length: int | None = PydanticUndefined, max_length: int | None = PydanticUndefined, union_mode: Literal['smart', 'left_to_right'] = PydanticUndefined, fail_fast: bool | None = PydanticUndefined, **extra: Unpack[_EmptyKwargs]) Any

Usage docs: https://docs.pydantic.dev/2.10/concepts/fields

Create a field for objects that can be configured.

Used to provide extra information about a field, either for the model schema or complex validation. Some arguments apply only to number fields (int, float, Decimal) and some apply only to str.

Note:
  • Any _Unset objects will be replaced by the corresponding value defined in the _DefaultValues dictionary. If a key for the _Unset object is not found in the _DefaultValues dictionary, it will default to None

Args:

default: Default value if the field is not set. default_factory: A callable to generate the default value. The callable can either take 0 arguments

(in which case it is called as is) or a single argument containing the already validated data.

alias: The name to use for the attribute when validating or serializing by alias.

This is often used for things like converting between snake and camel case.

alias_priority: Priority of the alias. This affects whether an alias generator is used. validation_alias: Like alias, but only affects validation, not serialization. serialization_alias: Like alias, but only affects serialization, not validation. title: Human-readable title. field_title_generator: A callable that takes a field name and returns title for it. description: Human-readable description. examples: Example values for this field. exclude: Whether to exclude the field from the model serialization. discriminator: Field name or Discriminator for discriminating the type in a tagged union. deprecated: A deprecation message, an instance of warnings.deprecated or the typing_extensions.deprecated backport,

or a boolean. If True, a default deprecation message will be emitted when accessing the field.

json_schema_extra: A dict or callable to provide extra JSON schema properties. frozen: Whether the field is frozen. If true, attempts to change the value on an instance will raise an error. validate_default: If True, apply validation to the default value every time you create an instance.

Otherwise, for performance reasons, the default value of the field is trusted and not validated.

repr: A boolean indicating whether to include the field in the __repr__ output. init: Whether the field should be included in the constructor of the dataclass.

(Only applies to dataclasses.)

init_var: Whether the field should _only_ be included in the constructor of the dataclass.

(Only applies to dataclasses.)

kw_only: Whether the field should be a keyword-only argument in the constructor of the dataclass.

(Only applies to dataclasses.)

coerce_numbers_to_str: Whether to enable coercion of any Number type to str (not applicable in strict mode). strict: If True, strict validation is applied to the field.

See [Strict Mode](../concepts/strict_mode.md) for details.

gt: Greater than. If set, value must be greater than this. Only applicable to numbers. ge: Greater than or equal. If set, value must be greater than or equal to this. Only applicable to numbers. lt: Less than. If set, value must be less than this. Only applicable to numbers. le: Less than or equal. If set, value must be less than or equal to this. Only applicable to numbers. multiple_of: Value must be a multiple of this. Only applicable to numbers. min_length: Minimum length for iterables. max_length: Maximum length for iterables. pattern: Pattern for strings (a regular expression). allow_inf_nan: Allow inf, -inf, nan. Only applicable to numbers. max_digits: Maximum number of allow digits for strings. decimal_places: Maximum number of decimal places allowed for numbers. union_mode: The strategy to apply when validating a union. Can be smart (the default), or left_to_right.

See [Union Mode](../concepts/unions.md#union-modes) for details.

fail_fast: If True, validation will stop on the first error. If False, all validation errors will be collected.

This option can be applied only to iterable types (list, tuple, set, and frozenset).

extra: (Deprecated) Extra fields that will be included in the JSON schema.

!!! warning Deprecated

The extra kwargs is deprecated. Use json_schema_extra instead.

Returns:
A new [FieldInfo][pydantic.fields.FieldInfo]. The return annotation is Any so Field can be used on

type-annotated fields without causing a type error.

fastapi_jsonapi.schema_builder module

JSON API schemas builder class.

class fastapi_jsonapi.schema_builder.SchemaBuilder(resource_type: str)

Bases: object

build_detail_schemas(schema: Type[BaseModel]) tuple[Type[JSONAPIObjectSchema], Type[JSONAPIResultDetailSchema]]
build_jsonapi_object(base_name: str, resource_type: str, dto: ~fastapi_jsonapi.schema.SchemasInfoDTO, model_base: ~typing.Type[~fastapi_jsonapi.schema_builder.JSONAPIObjectSchemaType] = <class 'fastapi_jsonapi.schema.JSONAPIObjectSchema'>, with_relationships: bool = True, id_field_required: bool = False) Type[JSONAPIObjectSchemaType]
build_list_schemas(schema: Type[BaseModel]) tuple[Type[JSONAPIObjectSchema], Type[JSONAPIResultListSchema]]
build_schema_for_detail_result(name: str, object_jsonapi_schema: Type[JSONAPIObjectSchema], includes_schemas: list[Type[JSONAPIObjectSchema]]) Type[JSONAPIResultDetailSchema]
build_schema_for_list_result(name: str, object_jsonapi_schema: Type[JSONAPIObjectSchema], includes_schemas: list[Type[JSONAPIObjectSchema]]) Type[JSONAPIResultListSchema]
classmethod build_schema_for_result(name: str, base: Type[BaseJSONAPIResultSchema], data_type: Type[JSONAPIObjectSchema] | Type[list[JSONAPIObjectSchema]], includes_schemas: list[Type[JSONAPIObjectSchema]]) Type[JSONAPIResultListSchema] | Type[JSONAPIResultDetailSchema]
build_schema_in(schema_in: Type[BaseModel], schema, operation_type: Literal['create', 'update', 'get'], schema_name_suffix: str = '', non_optional_relationships: bool = False, id_field_required: bool = False) tuple[Type[BaseJSONAPIDataInSchema], Type[BaseJSONAPIItemInSchema]]
create_jsonapi_object_schemas(schema: Type[BaseModel], resource_type: str | None = None, base_name: str = '', compute_included_schemas: bool = False) JSONAPIObjectSchemas
create_relationship_data_schema(field_name: str, base_name: str, operation_type: Literal['create', 'update', 'get'], field: FieldInfo, relationship_info: RelationshipInfo) Type[BaseJSONAPIRelationshipDataToOneSchema] | Type[BaseJSONAPIRelationshipDataToManySchema]
classmethod create_relationship_schema(name: str, relationship_info: RelationshipInfo) Type[BaseJSONAPIRelationshipSchema]
create_schemas(schema: Type[BaseModel], schema_in_post: Type[BaseModel] | None = None, schema_in_patch: Type[BaseModel] | None = None) BuiltSchemasDTO
find_all_included_schemas(included_schemas: list[tuple[str, BaseModel, str]]) dict[str, Type[JSONAPIObjectSchema]]
get_info_from_schema_for_building(base_name: str, schema: Type[BaseModel], operation_type: Literal['create', 'update', 'get'], non_optional_relationships: bool = False) SchemasInfoDTO

fastapi_jsonapi.signature module