Skip to content

Commit

Permalink
enhace getodk#793: query param to sanitize property names
Browse files Browse the repository at this point in the history
  • Loading branch information
sadiqkhoja committed May 17, 2023
1 parent 5e5e8c6 commit b3beecf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/model/query/datasets.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const { isEmpty, isNil, either, reduceBy, groupBy, uniqWith, equals: rEquals } =

const Problem = require('../../util/problem');
const { construct } = require('../../util/util');
const { sanitizeOdataIdentifier } = require('../../util/util');

////////////////////////////////////////////////////////////////////////////
// DATASET CREATE AND UPDATE
Expand Down Expand Up @@ -247,7 +248,7 @@ const getById = (id, extended = false) => ({ maybeOne }) => {

// Gets the dataset information, properties (including which forms each property comes from),
// and which forms consume the dataset via CSV attachment.
const getMetadata = (dataset) => async ({ all, Datasets }) => {
const getMetadata = (dataset, sanitizeFieldsForOdata) => async ({ all, Datasets }) => {

const _getLinkedForms = (datasetName, projectId) => sql`
SELECT DISTINCT f."xmlFormId", coalesce(fd.name, f."xmlFormId") "name" FROM form_attachments fa
Expand All @@ -268,6 +269,7 @@ const getMetadata = (dataset) => async ({ all, Datasets }) => {
if (!propertiesMap.has(property.name)) {
propertiesMap.set(property.name, {
...property,
name: sanitizeFieldsForOdata ? sanitizeOdataIdentifier(property.name) : property.name,
forms: []
});
}
Expand Down
6 changes: 3 additions & 3 deletions lib/resources/datasets.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
const sanitize = require('sanitize-filename');
const { getOrNotFound } = require('../util/promise');
const { streamEntityCsv } = require('../data/entity');
const { contentDisposition, withEtag } = require('../util/http');
const { contentDisposition, withEtag, isTrue } = require('../util/http');
const { md5sum } = require('../util/crypto');
const { Dataset } = require('../model/frames');

Expand All @@ -21,11 +21,11 @@ module.exports = (service, endpoint) => {
.then((project) => auth.canOrReject('dataset.list', project))
.then(() => Datasets.getList(params.id, queryOptions))));

service.get('/projects/:projectId/datasets/:name', endpoint(({ Datasets }, { params, auth }) =>
service.get('/projects/:projectId/datasets/:name', endpoint(({ Datasets }, { params, auth, query }) =>
Datasets.get(params.projectId, params.name)
.then(getOrNotFound)
.then((dataset) => auth.canOrReject('dataset.read', dataset)
.then(() => Datasets.getMetadata(dataset)))));
.then(() => Datasets.getMetadata(dataset, isTrue(query.odata))))));

service.patch('/projects/:projectId/datasets/:name', endpoint(async ({ Datasets }, { params, body, auth }) => {
const dataset = await Datasets.get(params.projectId, params.name).then(getOrNotFound);
Expand Down
19 changes: 19 additions & 0 deletions test/integration/api/datasets.js
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,25 @@ describe('datasets and entities', () => {
});
}));


it('should sanitize property names for odata', testService(async (service) => {
const asAlice = await service.login('alice');

await asAlice.post('/v1/projects/1/forms?publish=true')
.send(testData.forms.simpleEntity.replace(/age/g, 'the.age'))
.set('Content-Type', 'application/xml')
.expect(200);

await asAlice.get('/v1/projects/1/datasets/people?odata=true')
.expect(200)
.then(({ body }) => {

body.properties.map(p => p.name).should.be.eql(['first_name', 'the_age']);

});

}));

});
});

Expand Down

0 comments on commit b3beecf

Please sign in to comment.