Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
29 changes: 29 additions & 0 deletions spec/schemas.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,35 @@ describe('schemas', () => {
});
});

it('responds with all fields when getting incomplete schema', done => {
config.database.schemaCollection().then((schema) => {
return schema.addSchema('_User');
}).then(() => {
request.get({
url: 'http://localhost:8378/1/schemas/_User',
headers: masterKeyHeaders,
json: true
}, (error, response, body) => {
expect(body).toEqual({
className: '_User',
fields: {
objectId: {type: 'String'},
updatedAt: {type: 'Date'},
createdAt: {type: 'Date'},
username: {type: 'String'},
password: {type: 'String'},
authData: {type: 'Object'},
email: {type: 'String'},
emailVerified: {type: 'Boolean'},
ACL: {type: 'ACL'}
},
classLevelPermissions: defaultClassLevelPermissions
});
done();
});
})
});

it('lets you specify class name in both places', done => {
request.post({
url: 'http://localhost:8378/1/schemas/NewClass',
Expand Down
16 changes: 16 additions & 0 deletions src/Routers/SchemasRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,32 @@ function classNameMismatchResponse(bodyClass, pathClass) {
);
}

function injectDefaultSchema(schema) {
let defaultSchema = Schema.defaultColumns[schema.className];
if (defaultSchema) {
Object.keys(defaultSchema).forEach((key) => {
schema.fields[key] = defaultSchema[key];
});
}
return schema;
}

function getAllSchemas(req) {
return req.config.database.schemaCollection()
.then(collection => collection.getAllSchemas())
.then(schemas => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to be more concise, you could do

then(schemas => schemas.map(injectDefaultSchema))

Up to you though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's really valid...

return schemas.map((schema) => {
return injectDefaultSchema(schema);
})
})
.then(schemas => ({ response: { results: schemas } }));
}

function getOneSchema(req) {
const className = req.params.className;
return req.config.database.schemaCollection()
.then(collection => collection.findSchema(className))
.then(schema => injectDefaultSchema(schema))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be more concise here too if you feel like it:

.then(injectDefaultSchema)

.then(schema => ({ response: schema }))
.catch(error => {
if (error === undefined) {
Expand Down
1 change: 1 addition & 0 deletions src/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -848,4 +848,5 @@ export {
schemaAPITypeToMongoFieldType,
buildMergedSchemaObject,
systemClasses,
defaultColumns,
};