Skip to content
This repository has been archived by the owner on Dec 12, 2018. It is now read-only.

Commit

Permalink
Fixes for recent expansions fix
Browse files Browse the repository at this point in the history
This commit fixes these problems:

* If a defined resource exists (it exists as a file in lib/resource/) but doesn’t export a constructor function, then `instantiate()` will fail.
* If a resource cannot be found in lib/resource, we get exceptions.

The tests in this library are not catching these exceptions.  I found these while running the express-stormpath tests, while linked to the master head of this library
  • Loading branch information
Robert committed Nov 2, 2016
1 parent a48bb11 commit a3398e0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
12 changes: 12 additions & 0 deletions lib/resource/AccountCreationPolicy.js
@@ -1,3 +1,6 @@
var InstanceResource = require('./InstanceResource');
var utils = require('../utils');

/**
* @class AccountCreationPolicy
*
Expand Down Expand Up @@ -27,3 +30,12 @@
* The function to call when the save operation is complete. Will be called
* with the parameters (err, updatedResource).
*/


function AccountCreationPolicy() {
AccountCreationPolicy.super_.apply(this, arguments);
}

utils.inherits(AccountCreationPolicy, InstanceResource);

module.exports = AccountCreationPolicy;
8 changes: 5 additions & 3 deletions lib/resource/ResourceFactory.js
Expand Up @@ -6,6 +6,9 @@ var CustomData = require('./CustomData');
var InstanceResource = require('./InstanceResource');
var utils = require('../utils');

var fs = require('fs');
var path = require('path');

/**
* @private
*
Expand All @@ -21,7 +24,7 @@ function getPathForResourceName(resourceName) {
var normalizedFieldName = resourceName.charAt(0).toUpperCase() +
resourceName.substr(1);

return './' + normalizedFieldName;
return path.join(__dirname , normalizedFieldName + '.js');
}

/**
Expand Down Expand Up @@ -51,8 +54,7 @@ function expandResource(expandedFields, resource, query, dataStore) {

expandedFields.forEach(function(fieldName) {
var path = getPathForResourceName(fieldName);

if (typeof resource[fieldName] !== 'undefined') {
if (typeof resource[fieldName] !== 'undefined' && fs.existsSync(path) ) {
resource[fieldName] = instantiate(require(path), resource[fieldName], query, dataStore);
}
});
Expand Down

0 comments on commit a3398e0

Please sign in to comment.