Skip to content

Missing null check in sdkifyField #55

@khoait

Description

@khoait

The sdkifyField is not checking for null value before parsing to correct attribute type.

switch (attributeType) {
case AttributeTypes.EntityReference:
({ attributeValue, attributeLogicalName } = getEntityReference(entityRecord, field));
break;
case AttributeTypes.Money:
attributeValue = entityRecord[attributeLogicalName] as number;
break;
case AttributeTypes.MultiSelectOptionSetValue:
attributeValue = getMultiSelectOptionSet(entityRecord, attributeLogicalName);
break;
case AttributeTypes.OptionSetValue:
attributeValue = entityRecord[attributeLogicalName];
break;
case AttributeTypes.EntityCollection:
({ attributeValue, isActivityPartiesField } = await getEntityCollection(field, entityMetadata, entityRecord));
break;
case AttributeTypes.DateTime:
attributeValue = new Date(Date.parse(entityRecord[attributeLogicalName] as string));
break;
default:
// Default - set primitive type value
attributeValue = entityRecord[attributeLogicalName];
break;
}

For example, an attribute is of type datetime and entityRecord[attributeLogicalName] is null (line 228), parsing null value to a Date object will returns an invalid Date object.

Image

I think we can check for undefined or null before parsing like this:

if (entityRecord[attributeLogicalName] === undefined || entityRecord[attributeLogicalName] === null){
  attributeValue = entityRecord[attributeLogicalName]
} else {
 switch (attributeType) {
    case AttributeTypes.EntityReference:
      ({ attributeValue, attributeLogicalName } = getEntityReference(entityRecord, field));
      break;
    case AttributeTypes.Money:
      attributeValue = entityRecord[attributeLogicalName] as number;
      break;
    case AttributeTypes.MultiSelectOptionSetValue:
      attributeValue = getMultiSelectOptionSet(entityRecord, attributeLogicalName);
      break;
    case AttributeTypes.OptionSetValue:
      attributeValue = entityRecord[attributeLogicalName];
      break;
    case AttributeTypes.EntityCollection:
      ({ attributeValue, isActivityPartiesField } = await getEntityCollection(field, entityMetadata, entityRecord));
      break;
    case AttributeTypes.DateTime:
      attributeValue = new Date(Date.parse(entityRecord[attributeLogicalName] as string));
      break;
    default:
      // Default - set primitive type value
      attributeValue = entityRecord[attributeLogicalName];
      break;
  }
}

entityRecord[attributeLogicalName] = attributeValue;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions