feat: Add support for dot notation on array fields of Parse Object (#9115)

This commit is contained in:
Diamond Lewis
2024-07-08 16:29:58 -05:00
committed by GitHub
parent 892052cc44
commit cf4c8807b9
4 changed files with 90 additions and 4 deletions

View File

@@ -1851,6 +1851,14 @@ class DatabaseController {
// only valid ops that produce an actionable result
// the op may have happened on a keypath
this._expandResultOnKeyPath(response, key, result);
// Revert array to object conversion on dot notation for arrays (e.g. "field.0.key")
if (key.includes('.')) {
const [field, index] = key.split('.');
const isArrayIndex = Array.from(index).every(c => c >= '0' && c <= '9');
if (isArrayIndex && Array.isArray(result[field]) && !Array.isArray(response[field])) {
response[field] = result[field];
}
}
}
});
return Promise.resolve(response);

View File

@@ -1096,9 +1096,17 @@ export default class SchemaController {
maintenance?: boolean
) {
if (fieldName.indexOf('.') > 0) {
// subdocument key (x.y) => ok if x is of type 'object'
fieldName = fieldName.split('.')[0];
type = 'Object';
// "<array>.<index>" for Nested Arrays
// "<embedded document>.<field>" for Nested Objects
// JSON Arrays are treated as Nested Objects
const [x, y] = fieldName.split('.');
fieldName = x;
const isArrayIndex = Array.from(y).every(c => c >= '0' && c <= '9');
if (isArrayIndex && !['sentPerUTCOffset', 'failedPerUTCOffset'].includes(fieldName)) {
type = 'Array';
} else {
type = 'Object';
}
}
let fieldNameToValidate = `${fieldName}`;
if (maintenance && fieldNameToValidate.charAt(0) === '_') {