【问题标题】:Included relationships with same id and type in Json:api specification [closed]Json:api规范中包含具有相同ID和类型的关系[关闭]
【发布时间】:2019-09-14 23:31:00
【问题描述】:

我发送这样的响应(执行此响应的代码是正确的,我不需要代码):

"data": [
        {
            "type": "participant",
            "id": "c15409",
            "attributes": {
                "licenseNumber": "",
                "givenName": "Valeria Alejandra",
                "familyName": "Snchez Arredondo",
                "passportGivenName": "VALERIA ALEJANDRA",
            },
            "relationships": {
                "organization": {
                    "data": {
                        "type": "organization",
                        "id": "1205"
                    }
                }
            }
        },
        {
            "type": "participant",
            "id": "c16733",
            "attributes": {
                "licenseNumber": "",
                "givenName": "Amir",
                "familyName": "Mota Castaeda",
                "passportGivenName": "AMIR",
            },
            "relationships": {
                "organization": {
                    "data": {
                        "type": "organization",
                        "id": "1205"
                    }
                }
            }
        },

"included": [
    {
        "type": "organization",
        "id": "1205",
        "attributes": {
             "name": "Team",
             "country": "USA"
         }
    },
{
        "type": "organization",
        "id": "1205",
        "attributes": {
             "name": "Team",
             "country": "USA"
         }
    },
]

Json:api 不允许有相同的 id 和 types 所以这个响应是不正确的,但我需要几个参与者可以属于同一个组织。我想显示组织属性,因为请求资源的人需要这些数据。不知道下一个结构是否正确:

"data": [
        {
            "type": "participant",
            "id": "c15409",
            "attributes": {
                "licenseNumber": "",
                "givenName": "Valeria Alejandra",
                "familyName": "Snchez Arredondo",
                "passportGivenName": "VALERIA ALEJANDRA",
            },
            "relationships": {
                "organization": {
                    "data": {
                        "type": "organization",
                        "id": "1205"
                    }
                }
            }
        },
        {
            "type": "participant",
            "id": "c16733",
            "attributes": {
                "licenseNumber": "",
                "givenName": "Amir",
                "familyName": "Mota Castaeda",
                "passportGivenName": "AMIR",
            },
            "relationships": {
                "organization": {
                    "data": {
                        "type": "organization",
                        "id": "1205"
                    }
                }
            }
        },

"included": [
    {
        "type": "organization",
        "id": "1205",
        "attributes": {
             "name": "Team",
             "country": "USA"
         }
    }

我的母语不是英语,但我认为很明显我不是在问为什么这段代码不起作用。我只是想知道我所做的那个结构是否正确。是或否是一个有效的答案。无论如何,感谢回答我的 Ryan。

【问题讨论】:

  • 为什么 JSON API 会关心不同对象中的重复值?您能否请edit您的问题并将您的代码提供为minimal reproducible example?您可能还想先阅读How to Ask
  • 重复的 id 和类型 根据规范不正确 jsonapi.org/format 抱歉,我不知道还能提供什么,我发布了一个我所做的示例,但它不符合规范我需要将此响应“转换”为有效响应

标签: php json-api


【解决方案1】:

可能你的json结构不对,检查json结构是否符合你的需要。

{
    "data":[
        {
            "type":"participant",
            "id":"c15409",
            "attributes":{
                "licenseNumber":"",
                "givenName":"Valeria Alejandra",
                "familyName":"Snchez Arredondo",
                "passportGivenName":"VALERIA ALEJANDRA"
            },
            "relationships":{
                "organization":{
                    "data":{
                        "type":"organization",
                        "id":"1205"
                    }
                }
            }
        },
        {
            "type":"participant",
            "id":"c16733",
            "attributes":{
                "licenseNumber":"",
                "givenName":"Amir",
                "familyName":"Mota Castaeda",
                "passportGivenName":"AMIR"
            },
            "relationships":{
                "organization":{
                    "data":{
                        "type":"organization",
                        "id":"1205"
                    }
                }
            }
        }
    ],
    "included":[
        {
            "type":"organization",
            "id":"1205",
            "attributes":{
                "name":"Team",
                "country":"USA"
            }
        }
    ]
}

见官方文档:https://jsonapi.org/format/#document-compound-documents

【讨论】:

  • 但是这个结构正确吗?我的意思是,如果有两种类型:参与者,不应该有两种类型:组织吗?
  • 不,参与者的组织是同一个组织,所以一个就可以了。就像另一个表中的数据库(参与者,组织),N : 1 关系。
  • 非常感谢!我将应用该结构
猜你喜欢
  • 2021-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-19
  • 2011-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多