You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, I'm migrating a project from active_model_serializer to improve speed of json generation and noticed that attribute sorting doesn't work correctly when my new oj serializer has more than 10 attributes.
I know it is not important in most situations but in this case, it is crucial to preserve the existing order of attributes in the output not to confuse customers.
Which leads to the incorrect order of attributes in the output.
What if we modify that lambda like this:
sort_by=->(name,options,index){options[:identifier] ? index * -1 : index}
So the IDs will come first after sorting but we do use the index as a number which keeps the order of other attributes correct.
Although, it has one disadvantage. If there is more than one it will set the identifiers in the inverse order of their positions instead of sorting them by name. But I guess it should be a rare case 🤔
So if that is critical maybe there should be a better solution.
Ideally, from sort_by == :definition I'd expect to not make any changes in the order at all and to have a separate sort_by kind for putting IDs to the first place when needed, e.g. sort_by == :identifiers_first.
But to keep the backward compatibility, might just make the suggested (or somehow improved) change.
What do you think?
For me, it is important to fix this as soon as possible, so I can try to prepare a PR with this fix if that's okay.
The text was updated successfully, but these errors were encountered:
All the identifiers will be positioned in the negative range in the correct order without affecting the position of the attribute placed at 0 index (if the :id is at the end of the list initially), so the result will remain stable:
Hello, I'm migrating a project from
active_model_serializer
to improve speed of json generation and noticed that attribute sorting doesn't work correctly when my new oj serializer has more than 10 attributes.I know it is not important in most situations but in this case, it is crucial to preserve the existing order of attributes in the output not to confuse customers.
I have this list of attributes
so the correct attributes list in the order of definition is:
however, after calling this method it becomes incorrect:
oj_serializers/lib/oj_serializers/serializer.rb
Lines 709 to 720 in b327a62
and looks like this (
address
andcreator11
were moved to the wrong places):I debugged it and found that this lambda:
internally builds such array of conditions for sorting:
however, if we call
sort
on it it makes an unobvious modification because of the way ruby handles strings sorting:Which leads to the incorrect order of attributes in the output.
What if we modify that lambda like this:
So the IDs will come first after sorting but we do use the index as a number which keeps the order of other attributes correct.
Although, it has one disadvantage. If there is more than one it will set the
identifiers
in the inverse order of their positions instead of sorting them by name. But I guess it should be a rare case 🤔So if that is critical maybe there should be a better solution.
Ideally, from
sort_by == :definition
I'd expect to not make any changes in the order at all and to have a separatesort_by
kind for putting IDs to the first place when needed, e.g.sort_by == :identifiers_first
.But to keep the backward compatibility, might just make the suggested (or somehow improved) change.
What do you think?
For me, it is important to fix this as soon as possible, so I can try to prepare a PR with this fix if that's okay.
The text was updated successfully, but these errors were encountered: