CartoDB / cartodb

Location Intelligence & Data Visualization tool
http://carto.com
BSD 3-Clause "New" or "Revised" License
2.75k stars 652 forks source link

Scaping backslash on templates when creating named maps #4582

Closed jsanz closed 7 years ago

jsanz commented 9 years ago

Getting the template of a named map scapes the back slashes with something like this:

            "sql": "SELECT * FROM (SELECT * \\\nFROM nycpluto_all \\\nWHERE ST_Intersects(\\\n  ST_Buffer(\\\n    ST_SetSRID(\\\n      ST_GeomFromText('POINT(-73.988371 40.736)'),\\\n      4326\\\n    )::geography,\\\n    1000)::geometry,\\\n  the_geom) ) AS wrapped_query WHERE <%= layer0 %>=1",
            "layer_name": "nycpluto_all",
            "cartocss": "#nycpluto_all{\\\n  polygon-fill: #FFFFB2;\\\n  polygon-opacity: 0.8;\\\n  line-color: #FFF;\\\n  line-width: 0.5;\\\n  line-opacity: 1;\\\n}\\\n#nycpluto_all [ comarea <= 2124441] {\\\n   polygon-fill: #B10026;\\\n}\\\n#nycpluto_all [ comarea <= 65857] {\\\n   polygon-fill: #E31A1C;\\\n}\\\n#nycpluto_all [ comarea <= 22274] {\\\n   polygon-fill: #FC4E2A;\\\n}\\\n#nycpluto_all [ comarea <= 10750] {\\\n   polygon-fill: #FD8D3C;\\\n}\\\n#nycpluto_all [ comarea <= 5642] {\\\n   polygon-fill: #FEB24C;\\\n}\\\n#nycpluto_all [ comarea <= 3128] {\\\n   polygon-fill: #FED976;\\\n}\\\n#nycpluto_all [ comarea <= 1675] {\\\n   polygon-fill: #FFFFB2;\\\n}",

But if you create a new named map using this scaped slashes as they came, the named map is created but when I instantiate it I get an error on parsing those strings

{
  "errors": [
    "Error: style0:1:9 Invalid code: \\\\\\\n\\\nstyle0:2:24 Invalid code: \\\\\\\n\\\nstyle0:3:23 Invalid code: \\\\\\\n\\\nstyle0:4:19 Invalid code: \\\\\\\n\\\nstyle0:5:18 Invalid code: \\\\\\\n\\\nstyle0:6:18 Invalid code: \\\\\\\n\\\nstyle0:7:1 Invalid code: \\\\\\\n\\\nstyle0:8:31 Invalid code: \\\\\\\n\\\nstyle0:9:25 Invalid code: \\\\\\\n\\\nstyle0:10:1 Invalid code: \\\\\\\n\\\nstyle0:11:29 Invalid code: \\\\\\\n\\\nstyle0:12:25 Invalid code: \\\\\\\n\\\nstyle0:13:1 Invalid code: \\\\\\\n\\\nstyle0:14:29 Invalid code: \\\\\\\n\\\nstyle0:15:25 Invalid code: \\\\\\\n\\\nstyle0:16:1 Invalid code: \\\\\\\n\\\nstyle0:17:29 Invalid code: \\\\\\\n\\\nstyle0:18:25 Invalid code: \\\\\\\n\\\nstyle0:19:1 Invalid code: \\\\\\\n\\\nstyle0:20:28 Invalid code: \\\\\\\n\\\nstyle0:21:25 Invalid code: \\\\\\\n\\\nstyle0:22:1 Invalid code: \\\\\\\n\\\nstyle0:23:28 Invalid code: \\\\\\\n\\\nstyle0:24:25 Invalid code: \\\\\\\n\\\nstyle0:25:1 Invalid code: \\\\\\\n\\\nstyle0:26:28 Invalid code: \\\\\\\n\\\nstyle0:27:25 Invalid code: \\\\\\\n"
  ]
}

And the error cames evident if you ask for the template defined

            "sql": "SELECT * FROM (SELECT * \\\\\\\nFROM nycpluto_all \\\\\\\nWHERE ST_Intersects(\\\\\\\n  ST_Buffer(\\\\\\\n    ST_SetSRID(\\\\\\\n      ST_GeomFromText('POINT(-73.988371 40.736)'),\\\\\\\n      4326\\\\\\\n    )::geography,\\\\\\\n    1000)::geometry,\\\\\\\n  the_geom) ) AS wrapped_query WHERE <%= layer0 %>=1",
            "layer_name": "nycpluto_all",
            "cartocss": "#nycpluto_all{\\\\\\\n  polygon-fill: #FFFFB2;\\\\\\\n  polygon-opacity: 0.8;\\\\\\\n  line-color: #FFF;\\\\\\\n  line-width: 0.5;\\\\\\\n  line-opacity: 1;\\\\\\\n}\\\\\\\n#nycpluto_all [ comarea <= 2124441] {\\\\\\\n   polygon-fill: #B10026;\\\\\\\n}\\\\\\\n#nycpluto_all [ comarea <= 65857] {\\\\\\\n   polygon-fill: #E31A1C;\\\\\\\n}\\\\\\\n#nycpluto_all [ comarea <= 22274] {\\\\\\\n   polygon-fill: #FC4E2A;\\\\\\\n}\\\\\\\n#nycpluto_all [ comarea <= 10750] {\\\\\\\n   polygon-fill: #FD8D3C;\\\\\\\n}\\\\\\\n#nycpluto_all [ comarea <= 5642] {\\\\\\\n   polygon-fill: #FEB24C;\\\\\\\n}\\\\\\\n#nycpluto_all [ comarea <= 3128] {\\\\\\\n   polygon-fill: #FED976;\\\\\\\n}\\\\\\\n#nycpluto_all [ comarea <= 1675] {\\\\\\\n   polygon-fill: #FFFFB2;\\\\\\\n}"

Not sure if this is an error but I'd expect to send the same strings I receive from it. Not a big deal and it's pretty evident in any case.

javisantana commented 9 years ago

cc @rochoa

rochoa commented 9 years ago

There are two different issues here:

  1. Invalid syntax in CartoCSS
  2. Double escaped backslashes

About the first one there is already an issue open about checking named map has valid layers. Check: https://github.com/CartoDB/Windshaft-cartodb/issues/134. In this specific case is because of \\\n is not valid, read it as \\ + \n, where \\ is the invalid part.

About the second one: Could you provide me an example? I'm not able to reproduce it.

I've used the following template:

{
  "version": "0.0.1",
  "name": "escaped",
  "auth": {
    "method": "open"
  },
  "placeholders": {},
  "layergroup": {
    "version": "1.0.1",
    "layers": [
      {
        "type": "cartodb",
        "options": {
          "cartocss_version": "2.3.0",
          "cartocss": "#nycpluto_all{\\\n  polygon-fill: #FFFFB2;\\\n  polygon-opacity: 0.8;\\\n  line-color: #FFF;\\\n  line-width: 0.5;\\\n  line-opacity: 1;\\\n}\\\n#nycpluto_all [ comarea <= 2124441] {\\\n   polygon-fill: #B10026;\\\n}\\\n#nycpluto_all [ comarea <= 65857] {\\\n   polygon-fill: #E31A1C;\\\n}\\\n#nycpluto_all [ comarea <= 22274] {\\\n   polygon-fill: #FC4E2A;\\\n}\\\n#nycpluto_all [ comarea <= 10750] {\\\n   polygon-fill: #FD8D3C;\\\n}\\\n#nycpluto_all [ comarea <= 5642] {\\\n   polygon-fill: #FEB24C;\\\n}\\\n#nycpluto_all [ comarea <= 3128] {\\\n   polygon-fill: #FED976;\\\n}\\\n#nycpluto_all [ comarea <= 1675] {\\\n   polygon-fill: #FFFFB2;\\\n}",
          "sql": "select * from world_borders"
        }
      }
    ]
  }
}

And I got:

$ curl -X POST -H 'Content-Type: application/json' -d @./escaped.json 'http://development.localhost.lan:8181/api/v1/map/named?api_key=:API_KEY' && echo ''
{"template_id":"escaped"}
15:16:46 rochoa@lightness:~/projects/namedmaps/templates$ curl -s 'http://development.localhost.lan:8181/api/v1/map/named/escaped?api_key=:API_KEY' | python -m json.tool
{
    "template": {
        "auth": {
            "method": "open"
        },
        "layergroup": {
            "layers": [
                {
                    "options": {
                        "cartocss": "#nycpluto_all{\\\n  polygon-fill: #FFFFB2;\\\n  polygon-opacity: 0.8;\\\n  line-color: #FFF;\\\n  line-width: 0.5;\\\n  line-opacity: 1;\\\n}\\\n#nycpluto_all [ comarea <= 2124441] {\\\n   polygon-fill: #B10026;\\\n}\\\n#nycpluto_all [ comarea <= 65857] {\\\n   polygon-fill: #E31A1C;\\\n}\\\n#nycpluto_all [ comarea <= 22274] {\\\n   polygon-fill: #FC4E2A;\\\n}\\\n#nycpluto_all [ comarea <= 10750] {\\\n   polygon-fill: #FD8D3C;\\\n}\\\n#nycpluto_all [ comarea <= 5642] {\\\n   polygon-fill: #FEB24C;\\\n}\\\n#nycpluto_all [ comarea <= 3128] {\\\n   polygon-fill: #FED976;\\\n}\\\n#nycpluto_all [ comarea <= 1675] {\\\n   polygon-fill: #FFFFB2;\\\n}",
                        "cartocss_version": "2.3.0",
                        "sql": "select * from world_borders"
                    },
                    "type": "cartodb"
                }
            ],
            "version": "1.0.1"
        },
        "name": "escaped",
        "placeholders": {},
        "version": "0.0.1"
    }
}

After that I'm unable to instantiate it because of cited problem 1:

$ curl -s 'http://development.localhost.lan:8181/api/v1/map/named/escaped/jsonp?callback=cb'
cb({"errors":["style0:1:9 Invalid code: \\\n\nstyle0:2:24 Invalid code: \\\n\nstyle0:3:23 Invalid code: \\\n\nstyle0:4:19 Invalid code: \\\n\nstyle0:5:18 Invalid code: \\\n\nstyle0:6:18 Invalid code: \\\n\nstyle0:7:1 Invalid code: \\\n\nstyle0:8:31 Invalid code: \\\n\nstyle0:9:25 Invalid code: \\\n\nstyle0:10:1 Invalid code: \\\n\nstyle0:11:29 Invalid code: \\\n\nstyle0:12:25 Invalid code: \\\n\nstyle0:13:1 Invalid code: \\\n\nstyle0:14:29 Invalid code: \\\n\nstyle0:15:25 Invalid code: \\\n\nstyle0:16:1 Invalid code: \\\n\nstyle0:17:29 Invalid code: \\\n\nstyle0:18:25 Invalid code: \\\n\nstyle0:19:1 Invalid code: \\\n\nstyle0:20:28 Invalid code: \\\n\nstyle0:21:25 Invalid code: \\\n\nstyle0:22:1 Invalid code: \\\n\nstyle0:23:28 Invalid code: \\\n\nstyle0:24:25 Invalid code: \\\n\nstyle0:25:1 Invalid code: \\\n\nstyle0:26:28 Invalid code: \\\n\nstyle0:27:25 Invalid code: \\\n"]});
alonsogarciapablo commented 7 years ago

There has been no activity on this issue for more several months. We are closing it. If you think this still needs to be addressed please open a new issue.