@colyseus/schema 3.0.71 → 3.0.73

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3522,7 +3522,15 @@ function schema(fieldsAndMethods, name, inherits = Schema) {
3522
3522
  // Collection: Set → new SetSchema()
3523
3523
  defaultValues[fieldName] = new SetSchema();
3524
3524
  }
3525
- else if (value['type'] !== undefined && Schema.is(value['type'])) ;
3525
+ else if (value['type'] !== undefined && Schema.is(value['type'])) {
3526
+ // Direct Schema type: Type → new Type()
3527
+ if (!value['type'].prototype.initialize || value['type'].prototype.initialize.length === 0) {
3528
+ // only auto-initialize Schema instances if:
3529
+ // - they don't have an initialize method
3530
+ // - or initialize method doesn't accept any parameters
3531
+ defaultValues[fieldName] = new value['type']();
3532
+ }
3533
+ }
3526
3534
  }
3527
3535
  else {
3528
3536
  defaultValues[fieldName] = value['default'];
@@ -3531,8 +3539,12 @@ function schema(fieldsAndMethods, name, inherits = Schema) {
3531
3539
  else if (typeof (value) === "function") {
3532
3540
  if (Schema.is(value)) {
3533
3541
  // Direct Schema type: Type → new Type()
3534
- // TODO: should we auto-initialize Schema instances, in case their initialize() method is not defined?
3535
- // defaultValues[fieldName] = new value();
3542
+ if (!value.prototype.initialize || value.prototype.initialize.length === 0) {
3543
+ // only auto-initialize Schema instances if:
3544
+ // - they don't have an initialize method
3545
+ // - or initialize method doesn't accept any parameters
3546
+ defaultValues[fieldName] = new value();
3547
+ }
3536
3548
  fields[fieldName] = getNormalizedType(value);
3537
3549
  }
3538
3550
  else {
@@ -3559,14 +3571,27 @@ function schema(fieldsAndMethods, name, inherits = Schema) {
3559
3571
  }
3560
3572
  return defaults;
3561
3573
  };
3574
+ const getParentProps = (props) => {
3575
+ const fieldNames = Object.keys(fields);
3576
+ const parentProps = {};
3577
+ for (const key in props) {
3578
+ if (!fieldNames.includes(key)) {
3579
+ parentProps[key] = props[key];
3580
+ }
3581
+ }
3582
+ return parentProps;
3583
+ };
3562
3584
  /** @codegen-ignore */
3563
3585
  const klass = Metadata.setFields(class extends inherits {
3564
3586
  constructor(...args) {
3565
- super(Object.assign({}, getDefaultValues(), args[0] || {}));
3566
3587
  // call initialize method
3567
3588
  if (methods.initialize && typeof methods.initialize === 'function') {
3589
+ super(Object.assign({}, getDefaultValues(), getParentProps(args[0] || {})));
3568
3590
  methods.initialize.apply(this, args);
3569
3591
  }
3592
+ else {
3593
+ super(Object.assign({}, getDefaultValues(), args[0] || {}));
3594
+ }
3570
3595
  }
3571
3596
  }, fields);
3572
3597
  // Store the getDefaultValues function on the class for inheritance