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

Circular references not resolved if JSON has not been processed yet #103

Open
omatrot opened this issue Jul 12, 2019 · 2 comments
Open

Circular references not resolved if JSON has not been processed yet #103

omatrot opened this issue Jul 12, 2019 · 2 comments

Comments

@omatrot
Copy link

omatrot commented Jul 12, 2019

I have the following Circular reference:
Vehicle->Device->Vehicle

Vehicle has a Device nested object and vice-versa.

    createModelSchema(Vehicule, {
      vehiculeId: identifier(),
      companyId: primitive(),
      phoneNumber: primitive(),
      hardwareId: primitive(),
      vehiculeUserId: primitive(),
      gpsBoxType: primitive(),
      category: primitive(),
      name: primitive(),
      description: primitive(),
      iconId: primitive(),
      gpsBoxTrackingDelay: primitive(),
      address: primitive(),
      addressProtocol: primitive(),
      boardId: primitive(),
      rowVersion: primitive(),
      rowEnabled: primitive(),
      dbInsertTime: date(),
      customId: primitive(),
      hasGeoWorker: primitive(),
      device: reference(Device, nullHandler),
    });
    createModelSchema(Device, {
      imei: identifier(),
      brand: primitive(),
      model: primitive(),
      name: primitive(),
      osVersion: primitive(),
      appVersion: primitive(),
      rowVersion: primitive(),
      vehicule: reference(Vehicule, nullHandler)
    });

When I deserialize Vehicule, because Device has not been seen yet, the corresponding property is not set.

I would say that unresolved references are left unprocessed.

Am I wrong about that?
Is there a simple way to solve this problem?

@akphi
Copy link
Contributor

akphi commented Jul 23, 2019

@omatrot I think you should take a look at #9
I managed to do it with something like this:

class A {
 b: B;
}

class B {
 a: A
}

class C {
  // declare your model schema here, it has to be declared after A and B,
  // for my case I have class C using class A so this is natural, but maybe you can just declare
  // everything in one file
}

(also, I use object instead of reference)

@qb20nh
Copy link

qb20nh commented Jan 5, 2023

if A -> object() -> B,
B -> object() -> A creates a circular serialization structure which will run forever.
create id field on either side and reference them with reference()

I used this util function to generate unique id for distinct classes

const idMap: { [className: string]: number } = {}

export const id = <T extends Function>(clazz: T) => {
  const next = (idMap[clazz.name] ?? 0) + 1
  idMap[clazz.name] = next
  return next
}

and use like this

class Model {
  readonly id: number = id(Model)
  ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants