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
When I specify the model schema for 2 classes where one extends the other, the order of fields during serialization of the subclass sometimes do not follow the order specified in in createModelSchema. Look at the example I provided
I have 2 classes Person and Worker extends Person. When using createModelSchema, if I specify the model schema for Person before I specify the model schema for Worker, when I try to serialize an instance of Worker, the props in the model schema of Worker which overlap with the props in the model schema of Person will always be arranged first, and the fields which specialized to Worker will be arranged last, in the example, I'm trying to serialize using workerModelSchema, which specifies company field to go before name, but in the output, I don't see this.
Now, if I use the (1), where I specify the model schema of Worker before I do Person, I would get what I expected, or if I do (3) where I avoid using createModelSchema, I would get what I expected as well.
I really think this is a bug due to the inconsistency we see here, I suppose this has to do with some intricate handling we do in createModelSchema
But it's kind of hacky, I wonder if there's a way we can just default to have createModelSchema not doing magic, or maybe have a flag on createModelSchema to turn off this auto-detect extends mechanism, or maybe a flag on serialize to disable this, or a global configuration for serializr to disable this behavior. Let me know what you guys think! Thanks
The text was updated successfully, but these errors were encountered:
akphi
changed the title
Inconsistent field ordering in serialized object when using createModelSchema with inheritance
bug: inconsistent field ordering in serialized object when using createModelSchema with inheritance
Apr 25, 2023
Based on this example I think the right way to serialize subclasses is to remove name from Worker and add getDefaultModelSchema(Worker).extends = getDefaultModelSchema(Person).
This way name will always go first as the base class state should be (de)serialized before subclasses.
In general, having two serializers for name is a dangerous route because it's error prone: implementation details leak into sub class, have to remember to update multiple code locations if name logic changes, it's not clear what a parent should do when two subclasses serialize name in different ways, etc.
Currently all 3 snippets seem to declare inheritance in JS classes but instead of relying on the serializr inheritance features they use various workarounds to get a (sort of) correct output.
Worth mentioning that while JS guarantees key order, JSON dictionaries are still unsorted dictionaries and it's better to avoid relying on it.
Description
When I specify the model schema for 2 classes where one extends the other, the order of fields during serialization of the subclass sometimes do not follow the order specified in in
createModelSchema
. Look at the example I providedI have 2 classes
Person
andWorker extends Person
. When usingcreateModelSchema
, if I specify the model schema forPerson
before I specify the model schema forWorker
, when I try to serialize an instance ofWorker
, the props in the model schema ofWorker
which overlap with the props in the model schema ofPerson
will always be arranged first, and the fields which specialized toWorker
will be arranged last, in the example, I'm trying to serialize usingworkerModelSchema
, which specifiescompany
field to go beforename
, but in the output, I don't see this.Now, if I use the (1), where I specify the model schema of
Worker
before I doPerson
, I would get what I expected, or if I do (3) where I avoid usingcreateModelSchema
, I would get what I expected as well.I really think this is a bug due to the inconsistency we see here, I suppose this has to do with some intricate handling we do in
createModelSchema
Reproduce
akphi/issue-repo#13
Thoughts & Ideas
I think it comes down to this logic here
serializr/src/core/serialize.ts
Line 52 in f8b9196
My workaround right now would just be to do something like
But it's kind of hacky, I wonder if there's a way we can just default to have
createModelSchema
not doing magic, or maybe have a flag oncreateModelSchema
to turn off this auto-detect extends mechanism, or maybe a flag on serialize to disable this, or a global configuration forserializr
to disable this behavior. Let me know what you guys think! ThanksThe text was updated successfully, but these errors were encountered: