我的 SQLAlchemy 表中有一个嵌套对象,由Marshmallow 的嵌套模式特性生成。例如,一个articles
对象 GET 响应将包括一个author
(User
类型)对象。
我知道JSONAPI 规范已经允许更新关系。但是,通常我想在一次调用中更新一篇文章以及它的嵌套对象(包含新作者的文章的 POST 请求将自动创建作者)。是否可以发出包含尚不存在的关系对象资源的 PATCH 请求?
所以不仅仅是这个:
PATCH /articles/1 HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": {
"type": "articles",
"id": "1",
"relationships": {
"author": {
"data": { "type": "people", "id": "1" }
}
}
}
}
如果不存在作者,最好将其传递给创建新作者(这不是我的实际用例,但我有类似的现实生活需要):
PATCH /articles/1 HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": {
"type": "articles",
"id": "1",
"relationships": {
"author": {
"data": { "type": "people", "id": "1", "attributes": {"name": "new author":, "articles_written": 1} }
}
}
}
}
这完全有可能吗,或者对哪些 REST 框架可以支持这一点有建议,还是完全违反 JSON API 规范?