From ec1872b031829ba53fa092d0e65ef19280bd41b5 Mon Sep 17 00:00:00 2001 From: Kamal Bennani Date: Fri, 8 Jun 2018 02:37:24 +0200 Subject: [PATCH 1/6] Do not format value of type array and object because it will turn to zero --- packages/strapi-utils/lib/models.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/strapi-utils/lib/models.js b/packages/strapi-utils/lib/models.js index c9fe242ead..7c8ffd4a0d 100755 --- a/packages/strapi-utils/lib/models.js +++ b/packages/strapi-utils/lib/models.js @@ -454,7 +454,11 @@ module.exports = { let formattedValue; try { - formattedValue = !_.isNaN(_.toNumber(value)) ? _.toNumber(value) : value; + formattedValue = _.isObject(value) // Do not format object or array values + ? value + : !_.isNaN(_.toNumber(value)) + ? _.toNumber(value) + : value; } catch(err) { formattedValue = value; } From a8da79c6f8425bb0280b05b2614791a2d78bbbde Mon Sep 17 00:00:00 2001 From: Kamal Bennani Date: Fri, 8 Jun 2018 02:37:56 +0200 Subject: [PATCH 2/6] Place the break in the correct place --- packages/strapi-plugin-graphql/services/GraphQL.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/strapi-plugin-graphql/services/GraphQL.js b/packages/strapi-plugin-graphql/services/GraphQL.js index e4f2bdbd80..4c2fdd6df4 100644 --- a/packages/strapi-plugin-graphql/services/GraphQL.js +++ b/packages/strapi-plugin-graphql/services/GraphQL.js @@ -620,8 +620,8 @@ module.exports = { [ref.primaryKey]: arrayOfIds, ...where.where }).where; + break; } - break; // falls through } default: From b85baf568e497e661152a9f496e79762e72382fa Mon Sep 17 00:00:00 2001 From: Kamal Bennani Date: Fri, 8 Jun 2018 02:38:32 +0200 Subject: [PATCH 3/6] use default format for fields of type string and text --- packages/strapi-plugin-graphql/services/GraphQL.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/strapi-plugin-graphql/services/GraphQL.js b/packages/strapi-plugin-graphql/services/GraphQL.js index 4c2fdd6df4..b1046a40ac 100644 --- a/packages/strapi-plugin-graphql/services/GraphQL.js +++ b/packages/strapi-plugin-graphql/services/GraphQL.js @@ -154,10 +154,6 @@ module.exports = { let type = 'String'; switch (definition.type) { - case 'string': - case 'text': - type = 'String'; - break; case 'boolean': type = 'Boolean'; break; From e9fc37d03a197686b5bee7678b75b26308c0cfff Mon Sep 17 00:00:00 2001 From: Kamal Bennani Date: Fri, 8 Jun 2018 02:39:52 +0200 Subject: [PATCH 4/6] Add comment to handle fields of type array --- packages/strapi-plugin-graphql/services/GraphQL.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/strapi-plugin-graphql/services/GraphQL.js b/packages/strapi-plugin-graphql/services/GraphQL.js index b1046a40ac..7fde6f9a9e 100644 --- a/packages/strapi-plugin-graphql/services/GraphQL.js +++ b/packages/strapi-plugin-graphql/services/GraphQL.js @@ -154,6 +154,7 @@ module.exports = { let type = 'String'; switch (definition.type) { + // TODO: Handle fields of type Array, Perhaps default to [Int] or [String] ... case 'boolean': type = 'Boolean'; break; From 4920c1fa76a7beb542f3db8ef14e3937d97cfed4 Mon Sep 17 00:00:00 2001 From: Kamal Bennani Date: Fri, 8 Jun 2018 20:27:45 +0200 Subject: [PATCH 5/6] Set the entry type only when the value is defined --- packages/strapi-plugin-graphql/services/GraphQL.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/strapi-plugin-graphql/services/GraphQL.js b/packages/strapi-plugin-graphql/services/GraphQL.js index 7fde6f9a9e..a85430cd70 100644 --- a/packages/strapi-plugin-graphql/services/GraphQL.js +++ b/packages/strapi-plugin-graphql/services/GraphQL.js @@ -533,7 +533,10 @@ module.exports = { const entry = withRelated && withRelated.toJSON ? withRelated.toJSON() : withRelated; - entry[association.alias]._type = _.upperFirst(association.model); + // Set the _type only when the value is defined + if (entry[association.alias]) { + entry[association.alias]._type = _.upperFirst(association.model); + } return entry[association.alias]; } From cd53ef5ddf93ce543721fb688aeb929211d20486 Mon Sep 17 00:00:00 2001 From: Kamal Bennani Date: Sun, 17 Jun 2018 23:34:25 +0200 Subject: [PATCH 6/6] correctly determines if a value is valid candidate to become a number --- packages/strapi-utils/lib/models.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/strapi-utils/lib/models.js b/packages/strapi-utils/lib/models.js index 7c8ffd4a0d..f7a434528b 100755 --- a/packages/strapi-utils/lib/models.js +++ b/packages/strapi-utils/lib/models.js @@ -10,6 +10,11 @@ const path = require('path'); // Public node modules. const _ = require('lodash'); +// Following this discussion https://stackoverflow.com/questions/18082/validate-decimal-numbers-in-javascript-isnumeric this function is the best implem to determine if a value is a valid number candidate +const isNumeric = (value) => { + return !isNaN(parseFloat(value)) && isFinite(value); +} + /* eslint-disable prefer-template */ /* * Set of utils for models @@ -453,15 +458,10 @@ module.exports = { let result; let formattedValue; - try { - formattedValue = _.isObject(value) // Do not format object or array values - ? value - : !_.isNaN(_.toNumber(value)) - ? _.toNumber(value) - : value; - } catch(err) { - formattedValue = value; - } + // Check if the value if a valid candidate to be converted to a number value + formattedValue = isNumeric(value) + ? _.toNumber(value) + : value; if (_.includes(['_start', '_limit'], key)) { result = convertor(formattedValue, key);