Skip to content

Commit 7909f0e

Browse files
committed
Merge pull request #900 from drew-gross/schemas-500
Handle legacy _client_permissions key in _SCHEMA. Fixes #888.
2 parents 477e978 + 963811d commit 7909f0e

File tree

3 files changed

+56
-25
lines changed

3 files changed

+56
-25
lines changed

spec/Schema.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,4 +703,33 @@ describe('Schema', () => {
703703
});
704704
done();
705705
});
706+
707+
it('handles legacy _client_permissions keys without crashing', done => {
708+
Schema.mongoSchemaToSchemaAPIResponse({
709+
"_id":"_Installation",
710+
"_client_permissions":{
711+
"get":true,
712+
"find":true,
713+
"update":true,
714+
"create":true,
715+
"delete":true,
716+
},
717+
"_metadata":{
718+
"class_permissions":{
719+
"get":{"*":true},
720+
"find":{"*":true},
721+
"update":{"*":true},
722+
"create":{"*":true},
723+
"delete":{"*":true},
724+
"addField":{"*":true},
725+
}
726+
},
727+
"installationId":"string",
728+
"deviceToken":"string",
729+
"deviceType":"string",
730+
"channels":"array",
731+
"user":"*_User",
732+
});
733+
done();
734+
});
706735
});

src/Routers/SchemasRouter.js

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var express = require('express'),
44
Parse = require('parse/node').Parse,
55
Schema = require('../Schema');
66

7-
import PromiseRouter from '../PromiseRouter';
7+
import PromiseRouter from '../PromiseRouter';
88
import * as middleware from "../middlewares";
99

1010
function classNameMismatchResponse(bodyClass, pathClass) {
@@ -14,30 +14,10 @@ function classNameMismatchResponse(bodyClass, pathClass) {
1414
);
1515
}
1616

17-
function mongoSchemaAPIResponseFields(schema) {
18-
var fieldNames = Object.keys(schema).filter(key => key !== '_id' && key !== '_metadata');
19-
var response = fieldNames.reduce((obj, fieldName) => {
20-
obj[fieldName] = Schema.mongoFieldTypeToSchemaAPIType(schema[fieldName])
21-
return obj;
22-
}, {});
23-
response.ACL = {type: 'ACL'};
24-
response.createdAt = {type: 'Date'};
25-
response.updatedAt = {type: 'Date'};
26-
response.objectId = {type: 'String'};
27-
return response;
28-
}
29-
30-
function mongoSchemaToSchemaAPIResponse(schema) {
31-
return {
32-
className: schema._id,
33-
fields: mongoSchemaAPIResponseFields(schema),
34-
};
35-
}
36-
3717
function getAllSchemas(req) {
3818
return req.config.database.adaptiveCollection('_SCHEMA')
3919
.then(collection => collection.find({}))
40-
.then(schemas => schemas.map(mongoSchemaToSchemaAPIResponse))
20+
.then(schemas => schemas.map(Schema.mongoSchemaToSchemaAPIResponse))
4121
.then(schemas => ({ response: { results: schemas }}));
4222
}
4323

@@ -51,7 +31,7 @@ function getOneSchema(req) {
5131
}
5232
return results[0];
5333
})
54-
.then(schema => ({ response: mongoSchemaToSchemaAPIResponse(schema) }));
34+
.then(schema => ({ response: Schema.mongoSchemaToSchemaAPIResponse(schema) }));
5535
}
5636

5737
function createSchema(req) {
@@ -68,7 +48,7 @@ function createSchema(req) {
6848

6949
return req.config.database.loadSchema()
7050
.then(schema => schema.addClassIfNotExists(className, req.body.fields))
71-
.then(result => ({ response: mongoSchemaToSchemaAPIResponse(result) }));
51+
.then(result => ({ response: Schema.mongoSchemaToSchemaAPIResponse(result) }));
7252
}
7353

7454
function modifySchema(req) {
@@ -118,7 +98,7 @@ function modifySchema(req) {
11898
if (err) {
11999
reject(err);
120100
}
121-
resolve({ response: mongoSchemaToSchemaAPIResponse(mongoObject.result)});
101+
resolve({ response: Schema.mongoSchemaToSchemaAPIResponse(mongoObject.result)});
122102
})
123103
}));
124104
});

src/Schema.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,27 @@ function getObjectType(obj) {
760760
return 'object';
761761
}
762762

763+
const nonFieldSchemaKeys = ['_id', '_metadata', '_client_permissions'];
764+
function mongoSchemaAPIResponseFields(schema) {
765+
var fieldNames = Object.keys(schema).filter(key => nonFieldSchemaKeys.indexOf(key) === -1);
766+
var response = fieldNames.reduce((obj, fieldName) => {
767+
obj[fieldName] = mongoFieldTypeToSchemaAPIType(schema[fieldName])
768+
return obj;
769+
}, {});
770+
response.ACL = {type: 'ACL'};
771+
response.createdAt = {type: 'Date'};
772+
response.updatedAt = {type: 'Date'};
773+
response.objectId = {type: 'String'};
774+
return response;
775+
}
776+
777+
function mongoSchemaToSchemaAPIResponse(schema) {
778+
return {
779+
className: schema._id,
780+
fields: mongoSchemaAPIResponseFields(schema),
781+
};
782+
}
783+
763784
module.exports = {
764785
load: load,
765786
classNameIsValid: classNameIsValid,
@@ -768,4 +789,5 @@ module.exports = {
768789
schemaAPITypeToMongoFieldType: schemaAPITypeToMongoFieldType,
769790
buildMergedSchemaObject: buildMergedSchemaObject,
770791
mongoFieldTypeToSchemaAPIType: mongoFieldTypeToSchemaAPIType,
792+
mongoSchemaToSchemaAPIResponse,
771793
};

0 commit comments

Comments
 (0)