2.1.2: JSON.stringify() drops quotes when serializing a scalar string
Description
In Karate 2.1.2, JSON.stringify('alpha') returns alpha instead of the JSON string literal "alpha". The comparison fails even inside a single JavaScript function, so this is reproducible without browser interaction or passing the serialized result through a feature variable.
The unchanged reproducer passes on Karate 1.5.2. This breaks code that safely embeds strings into JavaScript expressions: for example, concatenating a serialized CSS selector produces document.querySelector(#sample) instead of document.querySelector("#sample").
Steps to Reproduce
- Create an empty directory and download the Karate 2.1.2 standalone JAR.
- Save this as
repro.feature:
Feature: JSON stringify scalar string
Scenario: A scalar string must serialize as a quoted JSON string
* def isCorrect = function(){ return JSON.stringify('alpha') === '"alpha"' }
* match isCorrect() == true- Run:
java -jar karate-2.1.2.jar run -o report-2.1.2 repro.feature- For comparison, download the Karate 1.5.2 standalone JAR and run the identical file:
java -jar karate-1.5.2.jar -o report-1.5.2 repro.featureNo browser, server, credentials, configuration file, called features, or extra dependencies are required.
Expected Behavior
JSON.stringify('alpha') should return a string containing the enclosing double quotes: "alpha". The equality check should return true, and the scenario should pass.
Actual Behavior
On 2.1.2, the equality check returns false and the scenario fails:
* match isCorrect() == true
match failed: EQUALS
$ | not equal (BOOLEAN:BOOLEAN)
false
trueVerified using the same Java installation:
| Karate | Result | CLI exit code |
|---|---|---|
| 2.1.2 | 1 scenario failed | 1 |
| 1.5.2 | 1 scenario passed | 0 |
Additional isolated checks on 2.1.2 show that JSON.stringify(['alpha']) correctly returns ["alpha"], and JSON.stringify({name:'alpha'}) correctly returns {"name":"alpha"}. The failure above concerns a string at the root.
Karate Version
2.1.2 standalone JAR; comparison: 1.5.2.
Java Version
Oracle Java 24.0.2 (build 24.0.2+12-54).
Operating System
macOS
Source: karatelabs/karate