CRU fails for schema using Mongoose Map type
davad opened this issue Β· comments
Describe the bug
When a schema has a Map field, create/read/update operations throw the following server-side error:
[09:41:56.302] 77020 LOG api/test/Find β params({"_id":"5c7941a3ba4d188ef4d295f3"}), query({}), payload(null)
[09:41:56.309] 77020 ERROR node_modules/rest-hapi/utilities/handler-helper.js:1693:9 api/test/Find β MongoError: Positional projection 'value.$*' does not match the query document.
[09:41:56.309] 77020 ERROR node_modules/rest-hapi/utilities/handler-helper.js:1693:9 api/test/Find β at queryCallback (/Users/david.m.landry/Sync/Avanade/MetLife/gssp-composer-backend/node_modules/mongodb-core/lib/cursor.js:248:25)
[09:41:56.309] 77020 ERROR node_modules/rest-hapi/utilities/handler-helper.js:1693:9 api/test/Find β at /Users/david.m.landry/Sync/Avanade/MetLife/gssp-composer-backend/node_modules/mongodb-core/lib/connection/pool.js:532:18
[09:41:56.309] 77020 ERROR node_modules/rest-hapi/utilities/handler-helper.js:1693:9 api/test/Find β at process._tickCallback (internal/process/next_tick.js:61:11)
Debug: internal, implementation, error
Error: There was an error processing the request.
at handleError (/Users/david.m.landry/Sync/Avanade/MetLife/gssp-composer-backend/node_modules/rest-hapi/utilities/handler-helper.js:1694:11)
at Object._findHandler [as findHandler] (/Users/david.m.landry/Sync/Avanade/MetLife/gssp-composer-backend/node_modules/rest-hapi/utilities/handler-helper.js:321:5)
To Reproduce
Example schema:
module.exports = mongoose => {
const modelName = "test";
const Types = mongoose.Schema.Types;
const Schema = new mongoose.Schema({
value: {
type: Map,
of: Types.String,
},
});
Schema.statics = {
collectionName: modelName,
routeOptions: {},
};
return Schema;
};
Expected behavior
Map types should be converted to/from plain objects. Joi would validate the shape of the contents of the Map, but not the Map keys themselves.
E.g. Sample JSON using the schema above; the first key/value, "key1", would pass Joi validation. The rest would fail.
{
"value": {
"key1": "a string",
"key2": true,
"key3": 5,
"key4": ["a string"],
"key5": {}
}
}
This would pass as well:
{
"value": {}
}
Desktop (please complete the following information):
- OS: macOS
- Browser cURL
Additional info
The DELETE operation still works. I created content directly in the DB, and was able to delete it through the REST endpoint.
I tracked down what's causing the error. I don't know if this is the only issue, but it allows me to create/read/update documents using a Map field.
Mongoose adds an extra entry to schema.paths
for the Map type. It's called 'mapField.$*'. When that's included in the RestHapi queries, it throws the error mentioned above. When I filter out that path from the query, everything seems to work normally.
I'll submit a PR for that change.
closed with #167