Skip to content

Commit 45b4e9b

Browse files
committed
atomic operations: response schemas
1 parent 9fc805e commit 45b4e9b

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

fastapi_jsonapi/atomic/schemas.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,19 @@ def validate_operation(cls, values: dict):
138138

139139
class AtomicOperationRequest(BaseModel):
140140
operations: List[AtomicOperation] = Field(alias="atomic:operations")
141+
142+
143+
class AtomicResult(BaseModel):
144+
data: Optional[dict] = Field(default=None, description="the “primary data” resulting from the operation.")
145+
meta: Optional[dict] = Field(
146+
default=None,
147+
description="a meta object that contains non-standard meta-information about the result.",
148+
)
149+
150+
151+
class AtomicResultResponse(BaseModel):
152+
"""
153+
https://jsonapi.org/ext/atomic/#auto-id-responses-4
154+
"""
155+
156+
results: List[AtomicResult] = Field(alias="atomic:results")

tests/test_atomic/test_response.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import pytest
2+
3+
from fastapi_jsonapi.atomic.schemas import AtomicResultResponse
4+
5+
6+
class TestAtomicResultResponse:
7+
@pytest.mark.parametrize(
8+
"operation_response",
9+
[
10+
{
11+
"atomic:results": [
12+
{
13+
"data": {
14+
"links": {
15+
"self": "https://example.com/blogPosts/13",
16+
},
17+
"type": "articles",
18+
"id": "13",
19+
"attributes": {
20+
"title": "JSON API paints my bikeshed!",
21+
},
22+
},
23+
},
24+
],
25+
},
26+
{
27+
"atomic:results": [
28+
{
29+
"data": {
30+
"links": {
31+
"self": "https://example.com/authors/acb2ebd6-ed30-4877-80ce-52a14d77d470",
32+
},
33+
"type": "authors",
34+
"id": "acb2ebd6-ed30-4877-80ce-52a14d77d470",
35+
"attributes": {"name": "dgeb"},
36+
},
37+
},
38+
{
39+
"data": {
40+
"links": {
41+
"self": "https://example.com/articles/bb3ad581-806f-4237-b748-f2ea0261845c",
42+
},
43+
"type": "articles",
44+
"id": "bb3ad581-806f-4237-b748-f2ea0261845c",
45+
"attributes": {
46+
"title": "JSON API paints my bikeshed!",
47+
},
48+
"relationships": {
49+
"author": {
50+
"links": {
51+
"self": "https://example.com/articles/bb3ad581-806f-4237-b748-f2ea0261845c/relationships/author",
52+
"related": "https://example.com/articles/bb3ad581-806f-4237-b748-f2ea0261845c/author",
53+
},
54+
},
55+
},
56+
},
57+
},
58+
],
59+
},
60+
],
61+
)
62+
def test_response_data(self, operation_response: dict):
63+
validated = AtomicResultResponse.parse_obj(operation_response)
64+
assert validated.dict(exclude_unset=True, by_alias=True) == operation_response

0 commit comments

Comments
 (0)