mobxjs / serializr

Serialize and deserialize complex object graphs to and from JSON and Javascript classes
MIT License
763 stars 51 forks source link

Polymorphism #98

Open andreaslarssen opened 5 years ago

andreaslarssen commented 5 years ago

With:

export class ClientErrorResponse  {
  @serializable(alias('error', raw())) protected _error: {messages: [{message: string}]};
}
export class UnauthorizedResponse extends ClientErrorResponse {}

...and then deserializing UnauthorizedResponse:

deserialize(UnauthorizedResponse, data);

...i get an instance of ClientErrorResponse, not the UnauthorizedResponse I hoped for, but when defining schemas this way:

export const UnauthorizedResponseSchema = {
  factory: context => new UnauthorizedResponse(),
  extends: ClientErrorResponseSchema,
  props: {}
};
export const ClientErrorResponseSchema = {
  factory: context => new ClientErrorResponse(),
  extends: BaseResponseSchema,
  props: {
    error: raw()
  }
};

...and then:

deserialize(UnauthorizedResponseSchema, error);

...it gives me an instance of UnauthorizedResponse.

Is this cool? Is it something I'm missing?

waliarubal commented 5 years ago

I am facing exact same kind of issue, i am serializing child class and on de-serialization I am receiving object of base class which doesn't has additional properties defined in child class. Seems to me an issue, kindly provide a fix or a workaround.

andreaslarssen commented 5 years ago

Thought I'd look into this. Are there any contrib docs I could look at?