Skip to content

fix: exposeDefaultValues: false doesn't work in some cases #1429

Open
@acrosett

Description

@acrosett

Description

When using transform without a source specified, on an object that isn't a map, it will call the targetType constructor if present. Thus, newValue will contain all the default values of the targetType class. The option exposeDefaultValues doesn't change that behavior.

TransformOperationExecutor.ts line 151

      let newValue: any = source ? source : {};
      if (
        !source &&
        (this.transformationType === TransformationType.PLAIN_TO_CLASS ||
          this.transformationType === TransformationType.CLASS_TO_CLASS)
      ) {
        if (isMap) {
          newValue = new Map();
        } else if (targetType) {
          newValue = new (targetType as any)();
        } else {
          newValue = {};
        }
      }

TransformOperationExecutor.ts line L329

            if (subValue === undefined && this.options.exposeDefaultValues) {
              // Set default value if nothing provided
              finalValue = newValue[newValueKey];
            } else {

TransformOperationExecutor.ts line L360

          if (finalValue !== undefined || this.options.exposeUnsetFields) {
            if (newValue instanceof Map) {
              newValue.set(newValueKey, finalValue);
            } else {
              newValue[newValueKey] = finalValue;
            }
          }

TransformOperationExecutor.ts line 374

     return newValue;

Since newValue is returned L329 has no impact on the presence of default values.

Suggested fix

Check exposeDefaultValues when initializing newValue.

      let newValue: any = source ? source : {};
      if (
        !source &&
        (this.transformationType === TransformationType.PLAIN_TO_CLASS ||
          this.transformationType === TransformationType.CLASS_TO_CLASS)
      ) {
        if (isMap) {
          newValue = new Map();
        } else if (targetType && this.options.exposeDefaultValues) {
          newValue = new (targetType as any)();
        } else {
          newValue = {};
        }
      }

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: needs triageIssues which needs to be reproduced to be verified report.type: fixIssues describing a broken feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions