Selaa lähdekoodia

Update JSON parser and snippets (#77983)

Related to issue  #77823

This does the following:

- Updates several asciidoc files that contained code snippets with
  invalid JSON, most involving unnecessary trailing commas.

- Makes the switch from the Groovy JSON parser to the Jackson parser,
  pursuant to the general goal of eliminating Groovy dependence.

- Makes testing of JSON validity at build time more strict.

Note that this update still allows backslash escaping for any
character. Currently that matters because of the file
"docs/reference/ml/anomaly-detection/apis/get-datafeed-stats.asciidoc",
specifically this part:

    "attributes" : {
      "ml.machine_memory" :
        "$body.datafeeds.0.node.attributes.ml\.machine_memory",
      "ml.max_open_jobs" : "512"
    }

It's not clear to me what change, if any, is appropriate there. So,
I've left in the escaped period and configured the parser to ignore
it for the time being.
edh-oss 4 vuotta sitten
vanhempi
commit
62a471aefe

+ 17 - 11
build-tools-internal/src/main/groovy/org/elasticsearch/gradle/internal/doc/SnippetsTask.groovy

@@ -8,9 +8,10 @@
 
 package org.elasticsearch.gradle.internal.doc
 
-import groovy.json.JsonException
-import groovy.json.JsonParserType
-import groovy.json.JsonSlurper
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.core.JsonToken;
 
 import org.gradle.api.DefaultTask
 import org.gradle.api.InvalidUserDataException
@@ -118,15 +119,20 @@ class SnippetsTask extends DefaultTask {
                         .replaceAll(/([:,])\s*(\$[^ ,\n}]+)/, '$1 "$2"')
                         // quote fields starting with $
                         .replaceAll(/(\$[^ ,\n}]+)\s*:/, '"$1":')
-                    JsonSlurper slurper =
-                        new JsonSlurper(type: JsonParserType.INDEX_OVERLAY)
+
+                    JsonFactory jf = new JsonFactory();
+                    jf.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER,true);
+                    JsonParser jsonParser;
+
                     try {
-                        slurper.parseText(quoted)
-                    } catch (JsonException e) {
-                        throw new InvalidUserDataException("Invalid json "
-                            + "in $snippet. The error is:\n${e.message}.\n"
-                            + "After substitutions and munging, the json "
-                            + "looks like:\n$quoted", e)
+                        jsonParser = jf.createParser(quoted);
+                        while(jsonParser.isClosed() == false) {
+                            jsonParser.nextToken();
+                        }
+                    } catch (JsonParseException e) {
+                        throw new InvalidUserDataException("Invalid json in "
+                        + snippet.toString() + ". The error is:\n" + e.getMessage() + ".\n"
+                        + "After substitutions and munging, the json looks like:\n" + quoted, e);
                     }
                 }
                 perSnippet(snippet)

+ 2 - 2
docs/reference/aggregations/bucket/missing-aggregation.asciidoc

@@ -31,9 +31,9 @@ Response:
   ...
   "aggregations": {
     "products_without_a_price": {
-      "doc_count": 00
+      "doc_count": 0
     }
   }
 }
 --------------------------------------------------
-// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
+// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]

+ 1 - 1
docs/reference/aggregations/pipeline/bucket-selector-aggregation.asciidoc

@@ -105,7 +105,7 @@ And the following may be the response:
                "doc_count": 2,
                "total_sales": {
                    "value": 375.0
-               },
+               }
             }
          ]
       }

+ 2 - 2
docs/reference/aggregations/pipeline/bucket-sort-aggregation.asciidoc

@@ -111,7 +111,7 @@ And the following may be the response:
                "doc_count": 2,
                "total_sales": {
                    "value": 375.0
-               },
+               }
             },
             {
                "key_as_string": "2015/02/01 00:00:00",
@@ -119,7 +119,7 @@ And the following may be the response:
                "doc_count": 2,
                "total_sales": {
                    "value": 60.0
-               },
+               }
             }
          ]
       }

+ 1 - 1
docs/reference/indices/recovery.asciidoc

@@ -303,7 +303,7 @@ The API returns the following response:
         "percent" : "100.0%",
         "total_on_start" : 0,
         "total_time" : "0s",
-        "total_time_in_millis" : 0,
+        "total_time_in_millis" : 0
       },
       "verify_index" : {
         "check_index_time" : "0s",

+ 1 - 1
docs/reference/ingest/processors/geoip.asciidoc

@@ -140,7 +140,7 @@ returns this:
     "geo": {
       "continent_name": "North America",
       "country_name": "United States",
-      "country_iso_code": "US",
+      "country_iso_code": "US"
     }
   }
 }

+ 1 - 1
docs/reference/ingest/processors/pipeline.asciidoc

@@ -98,7 +98,7 @@ Response from the index request:
     "failed": 0
   },
   "_seq_no": 66,
-  "_primary_term": 1,
+  "_primary_term": 1
 }
 --------------------------------------------------
 // TESTRESPONSE[s/"_seq_no": \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 1/"_primary_term" : $body._primary_term/]

+ 1 - 1
docs/reference/ingest/processors/user-agent.asciidoc

@@ -71,7 +71,7 @@ Which returns
       },
       "device" : {
         "name" : "Mac"
-      },
+      }
     }
   }
 }

+ 2 - 2
docs/reference/rest-api/info.asciidoc

@@ -152,11 +152,11 @@ Example response:
       },
       "data_streams" : {
          "available" : true,
-         "enabled" : true,
+         "enabled" : true
       },
       "data_tiers" : {
          "available" : true,
-         "enabled" : true,
+         "enabled" : true
       }
    },
    "tagline" : "You know, for X"

+ 3 - 3
docs/reference/search/profile.asciidoc

@@ -77,9 +77,9 @@ The API returns the following result:
             "query": [
               {
                 "type": "BooleanQuery",
-                "description": "message:get
-                message:search", "time_in_nanos" : 11972972, "breakdown" :
-                {
+                "description": "message:get message:search",
+                "time_in_nanos" : 11972972,
+                "breakdown" : {
                   "set_min_competitive_score_count": 0,
                   "match_count": 5,
                   "shallow_advance_count": 0,

+ 4 - 4
docs/reference/search/search-your-data/retrieve-selected-fields.asciidoc

@@ -214,11 +214,11 @@ returning them as a flat list.
         "group" : ["fans"],
         "user": [{
             "first": ["John"],
-            "last": ["Smith"],
+            "last": ["Smith"]
           },
           {
             "first": ["Alice"],
-            "last": ["White"],
+            "last": ["White"]
           }
         ]
       }
@@ -270,10 +270,10 @@ structure of the nested `user` array:
       "_score": 1.0,
       "fields": {
         "user": [{
-            "first": ["John"],
+            "first": ["John"]
           },
           {
-            "first": ["Alice"],
+            "first": ["Alice"]
           }
         ]
       }

+ 2 - 2
docs/reference/snapshot-restore/apis/get-snapshot-api.asciidoc

@@ -432,7 +432,7 @@ The API returns the following response:
         "total": 0,
         "failed": 0,
         "successful": 0
-      },
+      }
     }
   ],
   "next": "c25hcHNob3RfMixteV9yZXBvc2l0b3J5LHNuYXBzaG90XzI=",
@@ -613,7 +613,7 @@ The API returns the following response:
         "total": 0,
         "failed": 0,
         "successful": 0
-      },
+      }
     }
   ],
   "total": 2,