Skip to content

Commit c90025b

Browse files
authored
Disallow string downcast for groups and repeats (getodk#765)
1 parent 60c8feb commit c90025b

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/model/query/forms.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,11 +666,21 @@ const rejectIfWarnings = () => ({ context }) => {
666666
}
667667
};
668668

669+
// This function replaces a database trigger called check_field_collisions that
670+
// prevents certain kinds of field type changes and downcasts.
671+
// - Most types can be downcast to a string, but they cannot change between non-string types.
672+
// e.g. A date can become a string, but a string can't become a date, int, etc. because if
673+
// the data in that string is not a valid date/int, it will cause problems when exporting.
674+
// - A group or repeat cannot be downcast to a string.
669675
const checkFieldDowncast = (allFields, fields) => () => {
670676
const lookup = {};
671677
for (const f of allFields) lookup[f.path] = f;
672-
for (const f of fields.filter(ff => (ff.type !== 'string'))) {
673-
if (lookup[f.path] && f.type !== lookup[f.path].type)
678+
for (const f of fields) {
679+
// If new field type is not a string, types must stay the same
680+
if (f.type !== 'string' && lookup[f.path] && f.type !== lookup[f.path].type)
681+
return reject(Problem.user.fieldTypeConflict({ path: f.path, type: lookup[f.path].type }));
682+
// Downcasting to string is only allowed if not group/structure or repeat.
683+
if (f.type === 'string' && lookup[f.path] && (lookup[f.path].type === 'structure' || lookup[f.path].type === 'repeat'))
674684
return reject(Problem.user.fieldTypeConflict({ path: f.path, type: lookup[f.path].type }));
675685
}
676686
return resolve();

test/integration/api/forms/draft.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ describe('api: /projects/:id/forms (drafts)', () => {
370370
body.details.should.eql({ path: '/age', type: 'string' });
371371
})))));
372372

373-
it.skip('should complain on downcast from group to string', testService((service) =>
373+
it('should complain on downcast from group to string', testService((service) =>
374374
service.login('alice', (asAlice) =>
375375
asAlice.post('/v1/projects/1/forms/simple/draft')
376376
.send(testData.forms.simple.replace('nodeset="/data/meta/instanceID"', 'nodeset="/data/meta"'))
@@ -381,11 +381,11 @@ describe('api: /projects/:id/forms (drafts)', () => {
381381
body.details.should.eql({ path: '/meta', type: 'structure' });
382382
}))));
383383

384-
it.skip('should complain on downcast from repeat to string', testService((service) =>
384+
it('should complain on downcast from repeat to string', testService((service) =>
385385
service.login('alice', (asAlice) =>
386386
asAlice.post('/v1/projects/1/forms/withrepeat/draft')
387387
.send(testData.forms.withrepeat
388-
.replace('</model>', '<bind nodeset="/data/children/child" type="int"/></model>')
388+
.replace('</model>', '<bind nodeset="/data/children/child" type="string"/></model>')
389389
.replace('<repeat', '<rpt')
390390
.replace('</repeat', '</rpt'))
391391
.set('Content-Type', 'application/xml')

0 commit comments

Comments
 (0)