Skip to main content

Node.js Circular Dependency Fix

· 3 min read

We have changed the structure of the SDK to break cyclic dependencies. Due to this, the following changes have been made:

  • Model deserialization logic has moved out from model classes.
  • We have added new files ObjectMapper.js and ModelFactory.js to the sdk.
  • Exception handling in BaseController has changed to use ObjectMapper.
  • BaseModel is now being used to assign values to model properties (when model class is directly instantiated)
  • BaseController is not being exposed by the API Client now.

Please generate your SDK again to get the improved code.

Detailed Changes

  • Previously, models were being imported in other model classes because model deserialization was done inside the model class. This caused cyclic dependencies in some cases. To break this cyclic dependency, we have changed the structure of model classes, exception classes and added ObjectMapper and ModelFactory classes alongwith changes to Controllers, BaseController and BaseModel.
  • ObjectMapper class is now being used for mapping the model fields with their appropriate values. Object Mapper expects a json object (containing the input values to be assigned) and the name of the model to be instantiated. It then maps the input json object with the fields of the model and assigns them the correct value.
  • Model instantiation has now moved to ModelFactory.js class. Previously, controllers directly instantiated models. However, now controllers get a mapped object for the model from the ObjectMapper.js instead. The ObjectMapper class uses Model Factory to get an instance of the model/exception and then returns the mapped object (with correct values assigned to properties).
  • Previously, properties of a model object were deserialized (or mapped with their corresponding values) inside the model class itself. This behavior has changed now. Model classes do not deserialize the object themselves. There are 2 cases: We still allow users to create instances of models and pass in an object. In this case, the correct value for a model field is gotten from the object (if it is defined). However, if a model is instantiated using the model factory (as is done in the controllers now), then model deserialization will be handled by the ObjectMapper class.
  • Datetime deserialization is also being handled by the ObjectMapper class now.
  • We are now maintaining a mapping object in model and exception files which contains information about the properties of the models/exceptions.
  • The toJSON function in BaseModel.js file has now changed. It uses mappingInfo of a model class to check if it has additional properties. Previously, a variable called _variableDict was being used for storing details of additional model properties.
  • Global error handling logic in BaseController has changed. BaseController does not directly create instances of Exception classes. Instead, ModelFactory and ObjectMapper are being used for instantiating and deserializing Exception classes.