Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to encode object that contains meta #27

Closed
jcavar opened this issue Sep 12, 2019 · 7 comments
Closed

How to encode object that contains meta #27

jcavar opened this issue Sep 12, 2019 · 7 comments
Assignees

Comments

@jcavar
Copy link

jcavar commented Sep 12, 2019

It might be due to my limited knowledge of this library, but I can't seem to find a way to encode object with meta field.
If I add meta property to object, it is encoded as attribute (i would say expectedly).
I can workaround this by manually inserting meta dictionary to encoded version of the object. But this is not very elegant.

I am using Codable extension.

@fgulan
Copy link
Member

fgulan commented Sep 16, 2019

Hi @jcavar, thanks for the report! This seems similar to #10

What we can do here is to check if object has data as nested object and in that case treat meta and included objects as JSON API objects as well.

We didn't want to automatically encode meta and included as JSON API objects since those can represent normal JSON objects in most of the cases.

Handling meta and included only when data is used as nested object seems like a least intrusive way of handling this case. Or we can use some config which could specify wanted behaviour? What do you think @Truba ?

@jcavar
Copy link
Author

jcavar commented Sep 17, 2019

We didn't want to automatically encode meta and included as JSON API objects since those can represent normal JSON objects in most of the cases.

Could you give an example of this? I thought, when present, meta has to be JSON API meta object.

@fgulan
Copy link
Member

fgulan commented Sep 18, 2019

Not necessarily, I mean encoding JSON objects to JSON:API objects is not defined by any specification nor standard. So handling meta, included is left to implementation details of library.

For example:

{
    "type": "articles",
    "id": "1",
    "title": "JSON API paints my bikeshed!",
    "body": "The shortest article. Ever.",
    "created": "2015-05-22T14:56:29.000Z",
    "updated": "2015-05-22T14:56:28.000Z",
    "author": {
        "type": "people",
        "id": "42",
        "name": "John",
        "age": 80,
        "gender": "male"
    },
    "meta": {
        "type": "meta",
        "id": "2",
        "name": "Some name"
    }
}

it is unclear should we treat meta as included relationship or JSON:API meta information.

That's why we are considering using flags to let user choose the behavior

@jcavar
Copy link
Author

jcavar commented Sep 19, 2019

Hmm, I see. Yes, that makes sense.

Truba added a commit that referenced this issue Nov 14, 2019
@Truba
Copy link
Member

Truba commented Nov 14, 2019

Hi @jcavar ,
In encoder there was already a property additionalParams to add additional params to the root near the main data part. You can end up with something like:
{ "data": ..., additionlParam1: ..., additionalParam2: ... }

We've also added JapxEncodingOptions with includeMetaToCommonNamespce property that would allow you to extract meta at the same level as the attributes and relationships, instead of it ending up in the attributes. It also allows you to extract relationship meta into resource identifiers.

@jcavar
Copy link
Author

jcavar commented Nov 14, 2019

Very nice, thank you!

@fgulan
Copy link
Member

fgulan commented Nov 15, 2019

This is released with 2.2.0, closing, thanks for the input @jcavar !

@fgulan fgulan closed this as completed Nov 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants