Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions spec/MongoSchemaCollectionAdapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const MongoSchemaCollection = require('../lib/Adapters/Storage/Mongo/MongoSchemaCollection')
.default;
const Parse = require('parse/node');

describe('MongoSchemaCollection', () => {
it('can transform legacy _client_permissions keys to parse format', done => {
Expand Down Expand Up @@ -96,4 +97,28 @@ describe('MongoSchemaCollection', () => {
});
done();
});

it('throws a clear error for a null field type instead of crashing (#9847)', () => {
expect(() =>
MongoSchemaCollection._TESTmongoSchemaToParseSchema({
_id: 'SomeClass',
goodField: 'string',
badField: null,
})
).toThrowMatching(
e =>
e.code === Parse.Error.INCORRECT_TYPE &&
e.message.includes('SomeClass') &&
e.message.includes('badField')
);
});

it('throws a clear error for a non-string object field type (#9847)', () => {
expect(() =>
MongoSchemaCollection._TESTmongoSchemaToParseSchema({
_id: 'SomeClass',
badField: { foo: 1 },
})
).toThrowMatching(e => e.code === Parse.Error.INCORRECT_TYPE);
});
});
11 changes: 9 additions & 2 deletions src/Adapters/Storage/Mongo/MongoSchemaCollection.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import MongoCollection from './MongoCollection';
import Parse from 'parse/node';

function mongoFieldToParseSchemaField(type) {
function mongoFieldToParseSchemaField(type, fieldName, className) {
if (typeof type !== 'string') {
throw new Parse.Error(
Parse.Error.INCORRECT_TYPE,
`Invalid schema for class '${className}': field '${fieldName}' has an invalid type ` +
`(${type === null ? 'null' : typeof type}). Expected a string type descriptor.`
);
}
if (type[0] === '*') {
return {
type: 'Pointer',
Expand Down Expand Up @@ -43,7 +50,7 @@ const nonFieldSchemaKeys = ['_id', '_metadata', '_client_permissions'];
function mongoSchemaFieldsToParseSchemaFields(schema) {
var fieldNames = Object.keys(schema).filter(key => nonFieldSchemaKeys.indexOf(key) === -1);
var response = fieldNames.reduce((obj, fieldName) => {
obj[fieldName] = mongoFieldToParseSchemaField(schema[fieldName]);
obj[fieldName] = mongoFieldToParseSchemaField(schema[fieldName], fieldName, schema._id);
if (
schema._metadata &&
schema._metadata.fields_options &&
Expand Down
Loading