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

Polymorphism fixes #182

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/core/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,19 @@ function deserializeStarProps(
}
}

function identifyActualSchema(json: any, baseSchema: ModelSchema<any>) {
function identifyActualSchema(json: any, baseSchema: ModelSchema<any>): ModelSchema<any> {
if (baseSchema.subSchemas?.length) {
for (const subSchema of baseSchema.subSchemas) {
if (subSchema.discriminator) {
if (subSchema.discriminator.isActualType(json)) {
return subSchema;
}

const subtypeSchema = identifyActualSchema(json, subSchema)
// If we got subSchema back -- ignore it, because we've checked its discriminator already.
if (subtypeSchema !== subSchema) {
return subtypeSchema
}
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/types/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ export default function object(
typeof modelSchema === "object" || typeof modelSchema === "function",
"No modelschema provided. If you are importing it from another file be aware of circular dependencies."
);
let result: PropSchema = {
const result: PropSchema = {
serializer: function (item) {
modelSchema = getDefaultModelSchema(modelSchema)!;
invariant(isModelSchema(modelSchema), `expected modelSchema, got ${modelSchema}`);
const actualSchema = getDefaultModelSchema(item) || getDefaultModelSchema(modelSchema)!;
invariant(isModelSchema(actualSchema), `expected modelSchema, got ${actualSchema}`);
if (item === null || item === undefined) return item;
return serialize(modelSchema, item);
return serialize(actualSchema, item);
},
deserializer: function (childJson, done, context) {
modelSchema = getDefaultModelSchema(modelSchema)!;
Expand All @@ -60,6 +60,5 @@ export default function object(
);
},
};
result = processAdditionalPropArgs(result, additionalArgs);
return result;
return processAdditionalPropArgs(result, additionalArgs);
}
4 changes: 4 additions & 0 deletions test/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"printWidth": 100,
"semi": false
}
69 changes: 69 additions & 0 deletions test/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,72 @@ test("identifier can register themselves", (t) => {
})
t.end()
})

test("it should handle polymorphism", (t) => {
class Store {
constructor(id = 0, shapes = []) {
this.id = id
this.shapes = shapes
}
}

class Shape {
constructor(id) {
this.id = id
}
}

class Sphere extends Shape {
constructor(id, radius = -1) {
super(id)
this.radius = radius
}
}

class Box extends Shape {
constructor(id, side = -10) {
super(id)
this.side = side
}
}

_.createModelSchema(Shape, {
id: _.identifier(),
})

_.createModelSchema(Sphere, { radius: true })
_.getDefaultModelSchema(Sphere).extends = _.getDefaultModelSchema(Shape)

_.createModelSchema(Box, { side: true })
_.getDefaultModelSchema(Box).extends = _.getDefaultModelSchema(Shape)

_.subSchema("sphere", Shape)(Sphere)
_.subSchema("box", Shape)(Box)

_.createModelSchema(Store, {
shapes: _.list(_.object(Shape)),
})

test("it should accept subtypes", (t) => {
var store = new Store(100, [
new Sphere(1, 10),
new Box(2, 20),
new Sphere(10, 100),
new Box(3, 40),
])

const json = _.serialize(store)
const s = _.deserialize(Store, json)

t.ok(s.shapes[0] instanceof Sphere)
t.equal(s.shapes[0].radius, 10)
t.ok(s.shapes[1] instanceof Box)
t.equal(s.shapes[1].side, 20)
t.ok(s.shapes[2] instanceof Sphere)
t.equal(s.shapes[2].radius, 100)
t.ok(s.shapes[3] instanceof Box)
t.equal(s.shapes[3].side, 40)
t.end()
})
t.end()
})