From 0c20ccfde0bfb00b5b447bf1f200647960d3d147 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Thu, 21 Oct 2021 09:08:50 -0400 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20quick-serialize=20records?= =?UTF-8?q?=20before=20applying=20boolean=20filter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/api/mirage/config.js | 3 ++- addons/api/mirage/helpers/bexpr-filter.js | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/addons/api/mirage/config.js b/addons/api/mirage/config.js index 5d87801d7..b1481adf9 100644 --- a/addons/api/mirage/config.js +++ b/addons/api/mirage/config.js @@ -438,8 +438,9 @@ export default function () { sessionModel?.scope?.scope?.id === scope_id ); }); + } else { + resultSet = sessions.where((session) => session.scopeId === scope_id); } - resultSet = sessions.where((session) => session.scopeId === scope_id); return resultSet.filter(makeBooleanFilter(filter)); } ); diff --git a/addons/api/mirage/helpers/bexpr-filter.js b/addons/api/mirage/helpers/bexpr-filter.js index b8e41e63a..e2cffef02 100644 --- a/addons/api/mirage/helpers/bexpr-filter.js +++ b/addons/api/mirage/helpers/bexpr-filter.js @@ -1,4 +1,19 @@ import { makeEvaluator } from 'js-bexpr'; +import { underscore } from '@ember/string'; + +/** + * Because Mirage, a record cannot be serialized outside of a route handler + * context. Here is a quick and dirty attempt to serialize attribute names + * to underscore format. This is not entirely realistic, but may suffice. + */ +const quickSerialize = (item) => { + const json = item.toJSON(); + Object.keys(json).forEach((key) => { + const newKey = underscore(key); + json[newKey] = json[key]; + }); + return json; +}; /** * Creates a filter function that operates on a single object, which itself @@ -8,7 +23,10 @@ export default (expression) => { if (expression) { const evaluator = makeEvaluator(expression); // Since API query selectors begin with `/item`, the instance is wrapped. - return (item) => evaluator({ item }); + return (item) => { + const json = quickSerialize(item); + return evaluator({ item: json }); + }; } else { // If no expression was passed, always return true, // thus filtering out none.