infinum / Japx

Lightweight parser for the complex JSON:API (http://jsonapi.org/) structure.
MIT License
153 stars 35 forks source link

Unable to deserialise included with different types #8

Closed alexandru-calinoiu closed 6 years ago

alexandru-calinoiu commented 6 years ago

Expected Behavior

I am trying to deserialize a response like this:

{
    "data": {
        "id": "1",
        "type": "schedule",
        "attributes": {},
        "relationships": {
            "requests": {
                "data": [{
                    "type": "pickup_request",
                    "id": "5"
                }, {
                    "type": "swap_request",
                    "id": "25"
                }]
            }
        },
        "links": {
            "self": "example.com/member/1/schedule"
        }
    },
    "included": [{
        "type": "pickup_request",
        "id": "5",
        "attributes": {
            "attribute1": "",
            "attribute2": "",
            "status": "requested"
        }
    }, {
        "type": "swap_request",
        "id": "25",
        "attributes": {
            "attribute3": "",
            "status": "requested",
        }
    }]
}

As you notice the included items have different types.

Actual Behavior

An error is thrown

Steps to Reproduce the Problem

  1. Try to deserialize the above response

Specifications

Truba commented 6 years ago

Hi @alexandru-calinoiu,

Ty for contacting us with abut this issue.

Unfortunately I was unable to get the error while decoding your input.

This is the Japx output for your input:

{
   "data":{
      "id":"1",
      "links":{
         "self":"example.com\/member\/1\/schedule"
      },
      "type":"schedule",
      "requests":[
         {
            "status":"requested",
            "id":"5",
            "attribute1":"",
            "type":"pickup_request",
            "attribute2":""
         },
         {
            "status":"requested",
            "id":"25",
            "attribute3":"",
            "type":"swap_request"
         }
      ]
   }
}

I've also created a sample macOS app that decodes your json in app delegate: JapxTest.zip

I hope I was able to help. If you have any additional questions feel free to ask :)

alexandru-calinoiu commented 6 years ago

Thank for the quick respones @Truba

I got this far, but the problem I have is when I try to decode this with codable:

I have my base Request struct

struc Request : JapxCodable {
 let id: UInt
}

and a couple of specialized structs

struct PickupRequest: Request {
 let attribute1: String
}

struct SwapRequest: Request {
 let attribute3: String
}

I am not able to figure out a way to create this objects from the above json.

Truba commented 6 years ago

Hi @alexandru-calinoiu, ty for more details about your problem. From what you have said it seems that you are having issues with Swift's Codable and class inheritance, not with this library. For this reason I'm closing this issue. ;) Feel free to reopen it if you feel that I got something wrong. :)

Please note that JapxCodable is just Codable with a friendly reminder for a coder to write type: String and id: String in his/hers implementation. :)

With that said, a few words that might help you with this issue:

I said class inheritance, because I'm assuming your Request, PickupRequest and SwapRequest are class, since there is no struct inheritance in Swift :)

Here you can find a nice article on how to manage class inheritance with Swift's Codable: https://medium.com/tsengineering/swift-4-0-codable-decoding-subclasses-inherited-classes-heterogeneous-arrays-ee3e180eb556

alexandru-calinoiu commented 6 years ago

Thanks a lot, indeed I meant classes :+1: