Skip to content
This repository was archived by the owner on Sep 4, 2025. It is now read-only.
This repository was archived by the owner on Sep 4, 2025. It is now read-only.

Nested structs not being Unmarshaled #49

Closed

Description

@iderdik

Given a struct that contains another struct:

type Address struct {
    City string `jsonapi:"attr,city" json:"city"`
    State string `jsonapi:"attr,state" json:"state"`
}

type AdditionalInfo struct {
    HomeAddress     *Address `jsonapi:"attr,homeaddress" json:"homeaddress"`
        Phone                  *string     `jsonapi:"attr,phone" json:"phone"`
}

When calling UnmarshalPayload with this json:

{
    "homeaddress": {
        "city": "brooklyn",
        "state": "NY"
    },
    "phone": "555-555-5555"
}

I get an error data is not a jsonapi representation of AdditionalInfo. Are nested structs not supported?

Activity

  1. aren55555 commented on Aug 31, 2016

    @aren55555
    Contributor

    Your payload is not valid JSON API; even if it was (see below) I don't believe the nested struct is supported.

    {
        "data": {
            "type": "info",
            "attributes": {
                "phone": "555-555-5555",
                "homeaddress": {
                    "city": "brooklyn",
                    "state": "NY"
                }
            }
        }
    }

    If you want, please submit a PR adding support for this feature. Please ensure the implementation follow the JSON API spec's Attributes guidelines:

    Attributes may contain any valid JSON value.

    Complex data structures involving JSON objects and arrays are allowed as attribute values. However, any object that constitutes or is contained in an attribute MUST NOT contain a relationships or links member, as those members are reserved by this specification for future use.

  2. iderdik commented on Sep 1, 2016

    @iderdik
    Author

    Sorry about that - my actual json was valid jsonAPI, I had just extracted the relevant part of the attributes key.

    In any case, I seem to have gotten the functionality I need by declaring the type of HomeAddress to be a map[string]interface{} and handling any conversions between map and struct elsewhere. It's obviously not ideal but it's all I have right now.

    I'll leave the feature request open because I still think it would be valuable to have nested struct handling. Perhaps someone else reading this might be able to pick it up and work on it.

  3. bensullivan commented on Sep 17, 2016

    @bensullivan

    +1 for this feature please

  4. shwoodard commented on Sep 22, 2016

    @shwoodard
    Contributor

    Dup of #21

  5. iderdik commented on Sep 22, 2016

    @iderdik
    Author

    #21 is more about embedded anonymous structs. This issue is about structs as a field in a struct.

  6. msabramo commented on Jan 18, 2018

    @msabramo
    Contributor

    See #99 and mrowdy#3, which fixes pointers to struct fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions