Merge pull request #900 from drew-gross/schemas-500

Handle legacy _client_permissions key in _SCHEMA. Fixes #888.
This commit is contained in:
Nikita Lutsenko
2016-03-07 17:08:58 -08:00
3 changed files with 56 additions and 25 deletions

View File

@@ -703,4 +703,33 @@ describe('Schema', () => {
}); });
done(); done();
}); });
it('handles legacy _client_permissions keys without crashing', done => {
Schema.mongoSchemaToSchemaAPIResponse({
"_id":"_Installation",
"_client_permissions":{
"get":true,
"find":true,
"update":true,
"create":true,
"delete":true,
},
"_metadata":{
"class_permissions":{
"get":{"*":true},
"find":{"*":true},
"update":{"*":true},
"create":{"*":true},
"delete":{"*":true},
"addField":{"*":true},
}
},
"installationId":"string",
"deviceToken":"string",
"deviceType":"string",
"channels":"array",
"user":"*_User",
});
done();
});
}); });

View File

@@ -4,7 +4,7 @@ var express = require('express'),
Parse = require('parse/node').Parse, Parse = require('parse/node').Parse,
Schema = require('../Schema'); Schema = require('../Schema');
import PromiseRouter from '../PromiseRouter'; import PromiseRouter from '../PromiseRouter';
import * as middleware from "../middlewares"; import * as middleware from "../middlewares";
function classNameMismatchResponse(bodyClass, pathClass) { function classNameMismatchResponse(bodyClass, pathClass) {
@@ -14,30 +14,10 @@ function classNameMismatchResponse(bodyClass, pathClass) {
); );
} }
function mongoSchemaAPIResponseFields(schema) {
var fieldNames = Object.keys(schema).filter(key => key !== '_id' && key !== '_metadata');
var response = fieldNames.reduce((obj, fieldName) => {
obj[fieldName] = Schema.mongoFieldTypeToSchemaAPIType(schema[fieldName])
return obj;
}, {});
response.ACL = {type: 'ACL'};
response.createdAt = {type: 'Date'};
response.updatedAt = {type: 'Date'};
response.objectId = {type: 'String'};
return response;
}
function mongoSchemaToSchemaAPIResponse(schema) {
return {
className: schema._id,
fields: mongoSchemaAPIResponseFields(schema),
};
}
function getAllSchemas(req) { function getAllSchemas(req) {
return req.config.database.adaptiveCollection('_SCHEMA') return req.config.database.adaptiveCollection('_SCHEMA')
.then(collection => collection.find({})) .then(collection => collection.find({}))
.then(schemas => schemas.map(mongoSchemaToSchemaAPIResponse)) .then(schemas => schemas.map(Schema.mongoSchemaToSchemaAPIResponse))
.then(schemas => ({ response: { results: schemas }})); .then(schemas => ({ response: { results: schemas }}));
} }
@@ -51,7 +31,7 @@ function getOneSchema(req) {
} }
return results[0]; return results[0];
}) })
.then(schema => ({ response: mongoSchemaToSchemaAPIResponse(schema) })); .then(schema => ({ response: Schema.mongoSchemaToSchemaAPIResponse(schema) }));
} }
function createSchema(req) { function createSchema(req) {
@@ -68,7 +48,7 @@ function createSchema(req) {
return req.config.database.loadSchema() return req.config.database.loadSchema()
.then(schema => schema.addClassIfNotExists(className, req.body.fields)) .then(schema => schema.addClassIfNotExists(className, req.body.fields))
.then(result => ({ response: mongoSchemaToSchemaAPIResponse(result) })); .then(result => ({ response: Schema.mongoSchemaToSchemaAPIResponse(result) }));
} }
function modifySchema(req) { function modifySchema(req) {
@@ -118,7 +98,7 @@ function modifySchema(req) {
if (err) { if (err) {
reject(err); reject(err);
} }
resolve({ response: mongoSchemaToSchemaAPIResponse(mongoObject.result)}); resolve({ response: Schema.mongoSchemaToSchemaAPIResponse(mongoObject.result)});
}) })
})); }));
}); });

View File

@@ -760,6 +760,27 @@ function getObjectType(obj) {
return 'object'; return 'object';
} }
const nonFieldSchemaKeys = ['_id', '_metadata', '_client_permissions'];
function mongoSchemaAPIResponseFields(schema) {
var fieldNames = Object.keys(schema).filter(key => nonFieldSchemaKeys.indexOf(key) === -1);
var response = fieldNames.reduce((obj, fieldName) => {
obj[fieldName] = mongoFieldTypeToSchemaAPIType(schema[fieldName])
return obj;
}, {});
response.ACL = {type: 'ACL'};
response.createdAt = {type: 'Date'};
response.updatedAt = {type: 'Date'};
response.objectId = {type: 'String'};
return response;
}
function mongoSchemaToSchemaAPIResponse(schema) {
return {
className: schema._id,
fields: mongoSchemaAPIResponseFields(schema),
};
}
module.exports = { module.exports = {
load: load, load: load,
classNameIsValid: classNameIsValid, classNameIsValid: classNameIsValid,
@@ -768,4 +789,5 @@ module.exports = {
schemaAPITypeToMongoFieldType: schemaAPITypeToMongoFieldType, schemaAPITypeToMongoFieldType: schemaAPITypeToMongoFieldType,
buildMergedSchemaObject: buildMergedSchemaObject, buildMergedSchemaObject: buildMergedSchemaObject,
mongoFieldTypeToSchemaAPIType: mongoFieldTypeToSchemaAPIType, mongoFieldTypeToSchemaAPIType: mongoFieldTypeToSchemaAPIType,
mongoSchemaToSchemaAPIResponse,
}; };