Skip to content

Commit

Permalink
Merge pull request #900 from drew-gross/schemas-500
Browse files Browse the repository at this point in the history
Handle legacy _client_permissions key in _SCHEMA. Fixes #888.
  • Loading branch information
nlutsenko committed Mar 8, 2016
2 parents 477e978 + 963811d commit 7909f0e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 25 deletions.
29 changes: 29 additions & 0 deletions spec/Schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,4 +703,33 @@ describe('Schema', () => {
});
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();
});
});
30 changes: 5 additions & 25 deletions src/Routers/SchemasRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var express = require('express'),
Parse = require('parse/node').Parse,
Schema = require('../Schema');

import PromiseRouter from '../PromiseRouter';
import PromiseRouter from '../PromiseRouter';
import * as middleware from "../middlewares";

function classNameMismatchResponse(bodyClass, pathClass) {
Expand All @@ -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) {
return req.config.database.adaptiveCollection('_SCHEMA')
.then(collection => collection.find({}))
.then(schemas => schemas.map(mongoSchemaToSchemaAPIResponse))
.then(schemas => schemas.map(Schema.mongoSchemaToSchemaAPIResponse))
.then(schemas => ({ response: { results: schemas }}));
}

Expand All @@ -51,7 +31,7 @@ function getOneSchema(req) {
}
return results[0];
})
.then(schema => ({ response: mongoSchemaToSchemaAPIResponse(schema) }));
.then(schema => ({ response: Schema.mongoSchemaToSchemaAPIResponse(schema) }));
}

function createSchema(req) {
Expand All @@ -68,7 +48,7 @@ function createSchema(req) {

return req.config.database.loadSchema()
.then(schema => schema.addClassIfNotExists(className, req.body.fields))
.then(result => ({ response: mongoSchemaToSchemaAPIResponse(result) }));
.then(result => ({ response: Schema.mongoSchemaToSchemaAPIResponse(result) }));
}

function modifySchema(req) {
Expand Down Expand Up @@ -118,7 +98,7 @@ function modifySchema(req) {
if (err) {
reject(err);
}
resolve({ response: mongoSchemaToSchemaAPIResponse(mongoObject.result)});
resolve({ response: Schema.mongoSchemaToSchemaAPIResponse(mongoObject.result)});
})
}));
});
Expand Down
22 changes: 22 additions & 0 deletions src/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,27 @@ function getObjectType(obj) {
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 = {
load: load,
classNameIsValid: classNameIsValid,
Expand All @@ -768,4 +789,5 @@ module.exports = {
schemaAPITypeToMongoFieldType: schemaAPITypeToMongoFieldType,
buildMergedSchemaObject: buildMergedSchemaObject,
mongoFieldTypeToSchemaAPIType: mongoFieldTypeToSchemaAPIType,
mongoSchemaToSchemaAPIResponse,
};

0 comments on commit 7909f0e

Please sign in to comment.