From 9c3ff8789283a80225ff227287957f0c655de08e Mon Sep 17 00:00:00 2001 From: Andrea Falzetti Date: Thu, 3 Dec 2026 10:08:24 +0100 Subject: [PATCH] fix: Defined schema migration detects unchanged fields with an array, object or Date default value or Relation options as changed The schema migration compared the options of an existing field with `===` on every key, so a field with an array or object default value (`[]`, `{}`, Pointer, GeoPoint, File, Polygon), a Date default value declared as a string, or a Relation with options was detected as changed at every start and submitted again with the class update. Compare the stored field with the field as it would be submitted, after the same normalization done by `handleFields`, by value. A field whose options really differ is still updated. Co-Authored-By: Claude Opus 5.5 --- spec/DefinedSchemas.spec.js | 151 +++++++++++++++++++++++++ src/SchemaMigrations/DefinedSchemas.js | 19 +++- 2 files changed, 169 insertions(+), 1 deletion(-) diff --git a/spec/DefinedSchemas.spec.js b/spec/DefinedSchemas.spec.js index a2bbb2167c..8a2dccf582 100644 --- a/spec/DefinedSchemas.spec.js +++ b/spec/DefinedSchemas.spec.js @@ -324,6 +324,157 @@ describe('DefinedSchemas', () => { await object.fetch({ useMasterKey: true }); expect(object.get('aField')).toEqual('Hello'); }); + + describe('unchanged field options', () => { + // Records the fields submitted for the class and the number of updates of the class + const spyOnSubmittedFields = className => { + const submittedFields = []; + submittedFields.updates = 0; + const updateSchemaToDB = DefinedSchemas.prototype.updateSchemaToDB; + spyOn(DefinedSchemas.prototype, 'updateSchemaToDB').and.callFake(function (schema) { + if (schema.className === className) { + submittedFields.updates++; + submittedFields.push(...Object.keys(schema._fields)); + } + return updateSchemaToDB.call(this, schema); + }); + return submittedFields; + }; + + it('should not submit unchanged fields with object or array default values', async () => { + const server = await reconfigureServer(); + const fields = { + anArray: { type: 'Array', defaultValue: [] }, + anArrayWithValues: { type: 'Array', defaultValue: [1, 'a', { b: 2 }] }, + anObject: { type: 'Object', defaultValue: {} }, + aNestedObject: { type: 'Object', required: true, defaultValue: { a: { b: [1] } } }, + aDate: { type: 'Date', defaultValue: '2020-01-01T00:00:00.000Z' }, + aPointer: { + type: 'Pointer', + targetClass: '_User', + defaultValue: { __type: 'Pointer', className: '_User', objectId: 'abc' }, + }, + aGeoPoint: { + type: 'GeoPoint', + defaultValue: { __type: 'GeoPoint', latitude: 10, longitude: 20 }, + }, + aFile: { + type: 'File', + defaultValue: { __type: 'File', name: 'file.txt', url: 'http://localhost/file.txt' }, + }, + aPolygon: { + type: 'Polygon', + defaultValue: { + __type: 'Polygon', + coordinates: [ + [0, 0], + [0, 1], + [1, 1], + [0, 0], + ], + }, + }, + aString: { type: 'String', required: false, defaultValue: 'Test' }, + }; + const schemas = { definitions: [{ className: 'Test', fields }] }; + + await new DefinedSchemas(schemas, server.config).execute(); + await server.config.schemaCache.clear(); + const expectedFields = (await new Parse.Schema('Test').get()).fields; + + const submittedFields = spyOnSubmittedFields('Test'); + await new DefinedSchemas(schemas, server.config).execute(); + + expect(submittedFields.updates).toBe(1); + expect(submittedFields.length).toBe(0); + await server.config.schemaCache.clear(); + const schema = await new Parse.Schema('Test').get(); + expect(schema.fields).toEqual(expectedFields); + }); + + it('should not submit unchanged relation fields with options', async () => { + const server = await reconfigureServer(); + const schemas = { + definitions: [ + { + className: 'Test', + fields: { + aRelation: { type: 'Relation', targetClass: '_User', required: true }, + anotherRelation: { type: 'Relation', targetClass: '_User', required: false }, + }, + }, + ], + }; + + await new DefinedSchemas(schemas, server.config).execute(); + await server.config.schemaCache.clear(); + + const submittedFields = spyOnSubmittedFields('Test'); + await new DefinedSchemas(schemas, server.config).execute(); + + expect(submittedFields.updates).toBe(1); + expect(submittedFields.length).toBe(0); + await server.config.schemaCache.clear(); + const schema = await new Parse.Schema('Test').get(); + expect(schema.fields.aRelation).toEqual({ type: 'Relation', targetClass: '_User' }); + expect(schema.fields.anotherRelation).toEqual({ type: 'Relation', targetClass: '_User' }); + }); + + it('should update fields with changed object or array default values and options', async () => { + const server = await reconfigureServer(); + const definitions = fields => ({ definitions: [{ className: 'Test', fields }] }); + + await new DefinedSchemas( + definitions({ + anArray: { type: 'Array', defaultValue: [] }, + anObject: { type: 'Object', defaultValue: { a: { b: 1 } } }, + aDate: { type: 'Date', defaultValue: '2020-01-01T00:00:00.000Z' }, + aRequiredArray: { type: 'Array', required: true, defaultValue: [1] }, + aNotRequiredObject: { type: 'Object', required: false }, + anUnchangedArray: { type: 'Array', defaultValue: [1] }, + }), + server.config + ).execute(); + await server.config.schemaCache.clear(); + + const submittedFields = spyOnSubmittedFields('Test'); + await new DefinedSchemas( + definitions({ + anArray: { type: 'Array', defaultValue: [1] }, + anObject: { type: 'Object', defaultValue: { a: { b: 2 } } }, + aDate: { type: 'Date', defaultValue: '2021-01-01T00:00:00.000Z' }, + aRequiredArray: { type: 'Array', required: false, defaultValue: [1] }, + aNotRequiredObject: { type: 'Object' }, + anUnchangedArray: { type: 'Array', defaultValue: [1] }, + }), + server.config + ).execute(); + + expect(submittedFields.updates).toBe(1); + expect([...submittedFields].sort()).toEqual([ + 'aDate', + 'aNotRequiredObject', + 'aRequiredArray', + 'anArray', + 'anObject', + ]); + await server.config.schemaCache.clear(); + const schema = await new Parse.Schema('Test').get(); + expect(schema.fields.anArray).toEqual({ type: 'Array', defaultValue: [1] }); + expect(schema.fields.anObject).toEqual({ type: 'Object', defaultValue: { a: { b: 2 } } }); + expect(schema.fields.aDate).toEqual({ + type: 'Date', + defaultValue: { __type: 'Date', iso: '2021-01-01T00:00:00.000Z' }, + }); + expect(schema.fields.aRequiredArray).toEqual({ + type: 'Array', + required: false, + defaultValue: [1], + }); + expect(schema.fields.aNotRequiredObject).toEqual({ type: 'Object' }); + expect(schema.fields.anUnchangedArray).toEqual({ type: 'Array', defaultValue: [1] }); + }); + }); }); describe('Indexes', () => { diff --git a/src/SchemaMigrations/DefinedSchemas.js b/src/SchemaMigrations/DefinedSchemas.js index e67f5a003e..8a00c29efd 100644 --- a/src/SchemaMigrations/DefinedSchemas.js +++ b/src/SchemaMigrations/DefinedSchemas.js @@ -1,6 +1,7 @@ // @flow // @flow-disable-next Cannot resolve module `parse/node`. const Parse = require('parse/node'); +import { isDeepStrictEqual } from 'util'; import { logger } from '../logger'; import Config from '../Config'; import { internalCreateSchema, internalUpdateSchema } from '../Routers/SchemasRouter'; @@ -274,7 +275,7 @@ export class DefinedSchemas { } // Check if something changed other than the type (like required, defaultValue) - if (!this.paramsAreEquals(field, localField)) { + if (!this.fieldParamsAreEquals(localSchema.className, fieldName, field, localField)) { fieldsWithChangedParams.push(fieldName); } }); @@ -441,6 +442,22 @@ export class DefinedSchemas { return keysA.every(k => objA[k] === objB[k]); } + // Compare the field stored in the database with the field as it would be submitted by + // handleFields, which normalizes the options like the SDK does: a Date default value is sent + // as a Date object and the options of a Relation, which Parse does not accept, are dropped. + // Values are compared by value since default values can be arrays or objects. + fieldParamsAreEquals( + className: string, + fieldName: string, + cloudField: Migrations.FieldType, + localField: Migrations.FieldType + ) { + const submittedSchema = new Parse.Schema(className); + this.handleFields(submittedSchema, fieldName, localField); + const toJSON = value => JSON.parse(JSON.stringify(value)); + return isDeepStrictEqual(toJSON(cloudField), toJSON(submittedSchema._fields[fieldName])); + } + handleFields(newLocalSchema: Parse.Schema, fieldName: string, field: Migrations.FieldType) { if (field.type === 'Relation') { newLocalSchema.addRelation(fieldName, field.targetClass);