microsoft / Application-Insights-Workbooks

Templates for Azure Monitor Workbooks
MIT License
562 stars 462 forks source link

Update JSON validation test to handle UTF-8 Byte-Order Mark (BOM) error #89

Closed andrewki-msft closed 4 years ago

andrewki-msft commented 5 years ago

The JSON validation test fails if Byte-Order Mark (BOM) is present in the file and throws an ambiguous error. The validation test should be updated to warn the user to take corrective action to remove BOM. It is recommended that BOM should not be used in UTF-8.

https://tools.ietf.org/html/rfc3629#page-6

Steps to reproduce:

  1. Use notepad++ or some text editor that prepends a UTF-8 BOM
  2. Save a new workbook as UTF-8
  3. Execute jsonValidationTest.js
  4. Test will fail, throwing an error saying unexpected "blank" token was found
    SyntaxError: Unexpected token  in JSON at position 0
      at TryParseJson (test/jsonValidationTest.js:149:16)
      at validateJsonStringAndGetObject (test/jsonValidationTest.js:115:15)
      at results.filter.forEach.file (test/jsonValidationTest.js:55:21)
      at Array.forEach (<anonymous>)
      at browseDirectory (test/jsonValidationTest.js:54:18)
      at next (test/jsonValidationTest.js:96:31)
      at /home/travis/build/Microsoft/Application-Insights-Workbooks/test/jsonValidationTest.js:102:25
      at next (test/jsonValidationTest.js:96:31)
      at /home/travis/build/Microsoft/Application-Insights-Workbooks/test/jsonValidationTest.js:106:21
      at FSReqCallback.oncomplete (fs.js:161:5)

Fix diff for the workbook to remove the BOM:

@@ -1,4 +1,4 @@                                                                                                                                                       
-<EF><BB><BF>{                                                                                                                                                        
+{                                                                                                                                                                    
   "version": "Notebook/1.0",
gardnerjr commented 5 years ago

I'm tryng to add @ericc1103 to who it is assigned to and somehow it autocompleted to 04diiguyi? wth github?

gardnerjr commented 5 years ago

from an ancient archive of nodejs 0.x:

https://github.com/nodejs/node-v0.x-archive/issues/1918

it looks like the fix is to just strip it out after reading from the file: body = body.replace(/^\uFEFF/, '');