-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
extractMeta hook not firing in JSONAPISerializer #4161
Comments
Thanks for opening this issue @JakeDluhy! I think the original reasoning behind omitting the call to Would you be able to explain your use-case to help understanding the situation a bit more? |
Sure! Essentially I started looking into using extractMeta because (I thought) it was already there, and I wanted to use it to extract data from the links object in the payload. See adopted-ember-addons/ember-infinity#97 for a full explanation of what I did, but ultimately it would look like
so essentially I want to be able to set properties on the route corresponding to the query params at the end of my links values, so that I can then use them for pagination. I suppose that to answer your question, I don't really need extractMeta (I would rather have extractLinks) |
I actually have another use case, that specifically pertains to the meta data. We have an group object, and we want to provide information about that object that pertains to the current user (e.g. is the user following that group). JSON API spec would seem to suggest that that information should be in the meta, as it is not attributes on the model. However, we still need to access that information and set it as attributes, and I presume the best place to do that would be in extractMeta. |
Seems related to #2905, where there is ongoing discussion of the same issue. I just ran into this issue myself and would love to help (possibly with updating guides once the code has been written?) |
Having the same issue with not being able to customize meta data. Any updates on this? |
I'm pulling back a collection of records (findAll) that includes meta data. How can I read the meta data from this JSON API payload? extractMeta is never called. Can you only pull meta data when you perform a query? Its not clear to me as to when meta data will be processed. I can't really find any documentation related to this so I end up having to step debug my way through for answers. Can someone provide a bit of insight? UPDATE: For now I override a normalize... hook in the serializer to pull out the meta data. |
@visualjeff I'm doing the same (overriding normalize) |
@visualjeff @JakeDluhy would either of you mind posting an example of how you worked around this? running into the same frustrating issue. |
Hey @bstro for my first pass I overrode normalizeResponse like so... // serializers/activity.js
// In normalizeResponse, grab the payload and set the params values to the index route
// Would probably prefer to do this in extractMeta, but that hook doesn't work for JSONAPISerializer atm
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
this._extractLinks(payload.links);
return this._super(store, primaryModelClass, payload, id, requestType);
},
_extractLinks(linksObject) {
let parser = document.createElement('a');
let indexRoute = getOwner(this).lookup('route:index');
['prev', 'self', 'next', 'last'].forEach((key) => {
if(linksObject[key]) {
parser.href = linksObject[key];
indexRoute.set(`${key}Params`, parser.search)
} else {
indexRoute.set(`${key}Params`, null);
}
});
} Accessing the index route isn't ideal, but it worked. This was my first pass, and I think I later refactored it to move what was in links to meta (I no longer have access to the code base I was working in or I'd just grab the snippet). I think it looked more like normalizeResponse(store, primaryModelClass, payload, id, requestType) {
this._extractLinks(payload);
return this._super(store, primaryModelClass, payload, id, requestType);
},
_extractLinks(payload) {
['prev', 'self', 'next', 'last'].forEach((key) => {
if(payload.links[key]) {
payload.meta[key] = payload.links[key]
} else {
payload.meta[key] = null;
}
});
} |
Ran into this issue today as well. Was going to take a shot at implementing extractMeta as part of
@wecc Would you mind clarifying that statement? My use case for why I ran into this today was to use metadata to add some state to a |
@alvincrespo Originally we didn't implement Not defending the decision, just how I recalled the reasoning at that time. I think calling |
@wecc Thanks for the summary - I totally appreciate it. Gives me alot of background information. Ill try and tackle this since I think using extractMeta would be awesome to keep consistency across the serializers. |
Hi, I think the extractMeta can be useful. Here is what I was trying to use it for (camelize all meta keys). It makes no sense to use camel-case for everything except the meta. // In: {"data":[],"meta":{"page":{"number":1,"size":20,"total-entries":0,"total-pages":0}}}
// Out: {"data":[],"meta":{"page":{"number":1,"size":20,"totalEntries":0,"totalPages":0}}}
export default DS.JSONAPISerializer.extend({
extractMeta: function(store, typeClass, payload) {
if (payload && payload['meta'] !== undefined) {
let meta = camelizeObjectKeys(payload.meta);
delete payload.meta;
return meta;
}
}
});
function camelizeObjectKeys(value) {
if(Array.isArray(value)) {
return value.map(camelizeObjectKeys);
} else if (value !== null && typeof value === 'object') {
let obj = {};
Object.keys(value).forEach(
k => obj[Ember.String.camelize(k)] = camelizeObjectKeys(value[k])
);
return obj;
}
return value;
} |
Basically I am 👍 on having such a hook on the I tend to think that this might also go into the direction of a Should this maybe go through a small RFC before this is addressed? @wecc |
Me, @pangratz and @igorT talked about this and since So, instead of adding several new meta specifik hooks the proposed solution is to use the hooks that's already there, @JakeDluhy I wouldn't recommend accessing routes in the serializer but using @urbany I've added a PR that explains this a bit more and also gives an example to your exact use case over at #4603 |
Closing since #4603 has been merged. |
I'm extending the JSONAPISerializer, and I was trying to use the extractMeta hook. It never gets triggered though. Digging into the source, it seems that _normalizeResponse, where extractMeta is called from in the JSONSerializer, is overwritten and no longer calls it (see below).
Maybe
this.extractMeta
needs to be added to _normalizeDocumentHelper?The text was updated successfully, but these errors were encountered: