nijikokun / generate-schema

🧞 Convert JSON Objects to MySQL, JSON Schema, Mongoose, Google BigQuery, Swagger, and more.
MIT License
1.04k stars 136 forks source link

Null type in mysql throws error. Possible solution. #47

Open RobAley opened 5 years ago

RobAley commented 5 years ago

The following json (partial from the github api) :

{
  "message": "Out-of-srcdir builds: AM_CPPFLAGS=-I${srcdir}/src",
  "tree": {
    "sha": "5b9f0e1342264d1465fa7001faec3902c89ca944",
    "url": "https://api.github.com/repos/stedolan/jq/git/trees/5b9f0e1342264d1465fa7001faec3902c89ca944"
  },
  "url": "https://api.github.com/repos/stedolan/jq/git/commits/0bc77083ba711e1961db1cb2ebc9de7c71d1a746",
  "comment_count": 0,
  "verification": {
    "verified": false,
    "reason": "unsigned",
    "signature": null,
    "payload": null
  }
}

throws the following error when converting to mysql :

src/utils.js:12 Uncaught (in promise) TypeError: Cannot read property 'length' of null
    at Object.exports.isTimestamp (src/utils.js:12)
    at processObject (schemas/mysql.js:93)
    at processObject (schemas/mysql.js:103)
    at processObject (schemas/mysql.js:103)
    at processObject (schemas/mysql.js:103)
    at Object.Process [as mysql] (schemas/mysql.js:162)

as Utils.isTimestamp() doesn't like nulls instead of strings. Changing line 92 of mysql.js from :

       if (type !== 'undefined') {

to

if (type !== 'undefined' && type !== 'null') {

seems to fix this, and the type for null fields like this (signature and payload in the example json above) becomes string, which in my case is the correct handling.

I haven't submitted a pull request as I'm not sure that this is the correct handling in all cases / the generic case, and I don't know enough about the code to know if it will cause any other unintended side effects.

Thanks for a great library.

RobAley commented 5 years ago

Just to add that I noticed when playing around this json also throws a similar error in bigquery(), but I know nothing of / don't use bigquery so I can't speculate whether a fix is needed there :

generate-schema/src/schemas/bigquery.js:28 Uncaught (in promise) TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at processFields (generate-schema/src/schemas/bigquery.js:28)
    at generate-schema/src/schemas/bigquery.js:37
    at Array.map (<anonymous>)
    at processFields (generate-schema/src/schemas/bigquery.js:28)
    at generate-schema/src/schemas/bigquery.js:37
    at Array.map (<anonymous>)
    at processFields generate-schema/src/schemas/bigquery.js:28)
    at generate-schema/src/schemas/bigquery.js:37
    at Array.map (<anonymous>)