0

私の最後の質問ember.js JSONAPIAdapter with hasManyに加えて、同僚は、作業中の JSON:API 構造の「一種の」サイドロードされた関係を次のように埋め込むことができるかどうかを尋ねました。

{
    "data": [
      {
        "type": "altersgruppe",
        "id": "1",
        "attributes": {
          "name": "UNTER_21"
        },
        "relationships": {
          "tarifbeitraege": {
            "data": [
              {
                "type": "tarifbeitrag",
                "id": "3",
                "attributes": {
                  "name": "ZAHN70",
                  "beitrag": "3-29,70",
                  "proergaenzung": "7,00",
                  "gesamtbeitrag": "25.99"
                }
              },
              {
                "type": "tarifbeitrag",
                "id": "4",
                "attributes": {
                  "name": "ZAHN90",
                  "beitrag": "4-28,70",
                  "proergaenzung": "7,00",
                  "gesamtbeitrag": "30.99"
                }
              }
            ]
          }
        }
      },
      {
        "type": "altersgruppe",
        "id": "2",
        "attributes": {
          "name": "ALTER_21_24"
        },
        "relationships":{
          "tarifbeitraege": {
            "data": [
              {
                "type": "tarifbeitrag",
                "id": "1",
                "attributes": {
                  "name": "ZAHN70",
                  "beitrag": "1-25,70",
                  "proergaenzung": "7,00",
                  "gesamtbeitrag": "25.99"
                }
              },
              {
                "type": "tarifbeitrag",
                "id": "2",
                "attributes": {
                  "name": "ZAHN90",
                  "beitrag": "2-25,70",
                  "proergaenzung": "7,00",
                  "gesamtbeitrag": "25.99"
                }
              }]
          }
        }
      }
    ]
}

その背後にある考え方: Java バックエンド (サイドロードされた構造は実装が難しい) で問題の少ないリレーションシップを使用できます。

しかし、上記の JSON 構造は機能しません。ストアには、「altersgruppe」という最初のレベルのデータのみが含まれていますが、「tarifbeitrage」は空です。

4

1 に答える 1

1

このタイプのドキュメントは、JSON:API 仕様では複合ドキュメントと呼ばれます。

複合ドキュメントの「関係」セクションには、関係のみが含まれていると想定されています。個々のオブジェクトはリソース識別子オブジェクトであると想定されています。そこに属性を配置しても機能しません。それは本来あるべき場所ではないからです。

代わりに、完全なオブジェクトが最上位の「含まれる」セクションにサイドロードされます。したがって、応答はおそらく次のようになります。

{
    "data": [
      {
        "type": "altersgruppe",
        "id": "1",
        "attributes": {
          "name": "UNTER_21"
        },
        "relationships": {
          "tarifbeitraege": {
            "data": [
              { "type": "tarifbeitrag", "id": "3" },
              { "type": "tarifbeitrag", "id": "4" }
            ]
          }
        }
      },
      {
        "type": "altersgruppe",
        "id": "2",
        "attributes": {
          "name": "ALTER_21_24"
        },
        "relationships":{
          "tarifbeitraege": {
            "data": [
              { "type": "tarifbeitrag", "id": "1" },
              { "type": "tarifbeitrag", "id": "2" }
              ]
           }
        }
      }
    ],
    "included": [
      {
        "type": "tarifbeitrag",
        "id": "3",
        "attributes": {
          "name": "ZAHN70",
          "beitrag": "3-29,70",
          "proergaenzung": "7,00",
          "gesamtbeitrag": "25.99"
        }
      },
      {
        "type": "tarifbeitrag",
        "id": "4",
        "attributes": {
          "name": "ZAHN90",
          "beitrag": "4-28,70",
          "proergaenzung": "7,00",
          "gesamtbeitrag": "30.99"
        }
      },
      {
        "type": "tarifbeitrag",
        "id": "1",
        "attributes": {
          "name": "ZAHN70",
          "beitrag": "1-25,70",
          "proergaenzung": "7,00",
          "gesamtbeitrag": "25.99"
        }
      },
      {
        "type": "tarifbeitrag",
        "id": "2",
        "attributes": {
          "name": "ZAHN90",
          "beitrag": "2-25,70",
          "proergaenzung": "7,00",
          "gesamtbeitrag": "25.99"
        }
      }
    ]
}

http://jsonapi.orgのホームページには、サイド ロードを含む例と、複合ドキュメントを説明する仕様のセクションにある例があります。

于 2015-08-08T17:17:39.257 に答える