angular2-jsonapi icon indicating copy to clipboard operation
angular2-jsonapi copied to clipboard

Null values are not populated into model

Open frans-beech-it opened this issue 6 years ago • 0 comments

When you json data holds attributes with null values these are not translated into the properties of the model

  public transformSerializedNamesToPropertyNames<T extends JsonApiModel>(modelType: ModelType<T>, attributes: any) {
    const serializedNameToPropertyName = this.getModelPropertyNames(modelType.prototype);
    const properties: any = {};

    Object.keys(serializedNameToPropertyName).forEach((serializedName) => {
      if (attributes && attributes[serializedName] !== null && attributes[serializedName] !== undefined) {
        properties[serializedNameToPropertyName[serializedName]] = attributes[serializedName];
      }
    });

    return properties;
  }

This should be:

  public transformSerializedNamesToPropertyNames<T extends JsonApiModel>(modelType: ModelType<T>, attributes: any) {

   if (!attributes) {
       return {};
    }

    const serializedNameToPropertyName = this.getModelPropertyNames(modelType.prototype);
    const properties: any = {};

    Object.keys(serializedNameToPropertyName).forEach((serializedName) => {
      if (attributes[serializedName] !== undefined) {
        properties[serializedNameToPropertyName[serializedName]] = attributes[serializedName];
      }
    });

    return properties;
  }

But then you run into the issue that Date object are initiated with a wrong value instead of having the value null https://github.com/ghidoz/angular2-jsonapi/issues/242

not tested yet for other model types

frans-beech-it avatar Nov 20 '19 15:11 frans-beech-it