|
6 | 6 |
|
7 | 7 | from __future__ import annotations |
8 | 8 |
|
| 9 | +from dataclasses import dataclass |
9 | 10 | from inspect import isclass |
10 | 11 | from types import GenericAlias |
11 | | -from typing import TYPE_CHECKING, Optional, Sequence, Type, Union, get_args |
| 12 | +from typing import TYPE_CHECKING, Any, Callable, Optional, Sequence, Type, Union, get_args |
12 | 13 |
|
13 | 14 | from fastapi import FastAPI |
14 | 15 | from pydantic import BaseModel, ConfigDict, Field |
|
20 | 21 | from pydantic.fields import FieldInfo |
21 | 22 |
|
22 | 23 | from fastapi_jsonapi.common import search_relationship_info |
| 24 | +from fastapi_jsonapi.types_metadata import RelationshipInfo |
23 | 25 |
|
24 | 26 | if TYPE_CHECKING: |
25 | 27 | from fastapi_jsonapi.data_typing import TypeSchema |
@@ -137,6 +139,50 @@ class JSONAPISchemaIntrospectionError(Exception): |
137 | 139 | pass |
138 | 140 |
|
139 | 141 |
|
| 142 | +# todo: when 3.9 support is dropped, return back `slots=True to JSONAPIObjectSchemas dataclass` |
| 143 | + |
| 144 | + |
| 145 | +@dataclass(frozen=True) |
| 146 | +class JSONAPIObjectSchemas: |
| 147 | + attributes_schema: Type[BaseModel] |
| 148 | + relationships_schema: Type[BaseModel] |
| 149 | + object_jsonapi_schema: Type[JSONAPIObjectSchema] |
| 150 | + can_be_included_schemas: dict[str, Type[JSONAPIObjectSchema]] |
| 151 | + |
| 152 | + @property |
| 153 | + def included_schemas_list(self) -> list[Type[JSONAPIObjectSchema]]: |
| 154 | + return list(self.can_be_included_schemas.values()) |
| 155 | + |
| 156 | + |
| 157 | +@dataclass(frozen=True) |
| 158 | +class BuiltSchemasDTO: |
| 159 | + schema_in_post: Type[BaseJSONAPIDataInSchema] |
| 160 | + schema_in_post_data: Type[BaseJSONAPIItemInSchema] |
| 161 | + schema_in_patch: Type[BaseJSONAPIDataInSchema] |
| 162 | + schema_in_patch_data: Type[BaseJSONAPIItemInSchema] |
| 163 | + detail_response_schema: Type[JSONAPIResultDetailSchema] |
| 164 | + list_response_schema: Type[JSONAPIResultListSchema] |
| 165 | + |
| 166 | + |
| 167 | +FieldValidators = dict[str, Callable] |
| 168 | + |
| 169 | + |
| 170 | +@dataclass(frozen=True) |
| 171 | +class SchemasInfoDTO: |
| 172 | + # id field |
| 173 | + resource_id_field: tuple[Type, FieldInfo, Callable, FieldValidators] |
| 174 | + # pre-built attributes |
| 175 | + attributes_schema: Type[BaseModel] |
| 176 | + # relationships |
| 177 | + relationships_schema: Type[BaseModel] |
| 178 | + # has any required relationship |
| 179 | + has_required_relationship: bool |
| 180 | + # anything that can be included |
| 181 | + included_schemas: list[tuple[str, BaseModel, str]] |
| 182 | + |
| 183 | + relationships_info: dict[str, tuple[RelationshipInfo, Any]] |
| 184 | + |
| 185 | + |
140 | 186 | def get_model_field(schema: Type["TypeSchema"], field: str) -> str: |
141 | 187 | """ |
142 | 188 | Get the model field of a schema field. |
|
0 commit comments