Closed Mirosta closed 10 months ago
Well this is not really a bug, but the way bash works. So the outer quotes are needed, so that bash does not delimit the string using its internal $LFS, which is usally newlines, tabs and spaces. The escaping () is needed, so that bash does take the quote as a literal and not a special character.
In Java your string value (!) then is "some json string"
so when you export this to Json, which seem to need escaping of special chars like the quote, that is something you have to take care of, not jComamnder.
I think you've missed the issue, it seems that the way jCommander is written, it is assuming that the quotes will be passed in by the shell.
If I do echo "hello world" Then the output is hello world But jCommander thinks it would be "hello world"
Go have a look at the trim function, if you're not sure what I mean; it checks if the start and end of the string are a quote character, and removes then if they are.
So in my example above I would expect that:
java -jar test.jar --value "\"some json string\""
If inside that command you just do
System.out.println(jsonValue);
The output should be:
"some json string"
However it is actually:
some json string
This is as expected:
java -jar test.jar --value "some json string"
some json string
Yes you are right, I did not get the problem completely. I did some tests and the only Test that seem to fail if you quote the removal of \" in trim() is QuotedMainTest which is wrong concerning your argumentation, which I agree to!
Can this issue be closed and/or is the behavior like the testCase below expected?
I upgraded from JCommander 1.72 to 1.78 and encountered a change which looks like it is related to this issue. I have made the following testCase which succeeds with version 1.78 but fails with 1.72:
@Parameters(separators = "=", commandDescription = "Just for testing quote handling.")
public class TestParameterQuoteHandling {
@Parameter(names = { "--aParameter" }, description = "A String.")
public String aParameter = null;
@Parameter(names = { "--aParameterList"}, description = "A String list.")
public List<String> aParameterList = null;
@Test
public void testParameterHavingQuotes() {
JCommander jc = new JCommander(this);
jc.parse("--aParameter=\"X\"");
Assert.assertNotNull(aParameter);
// as of JCommander 1.74/75
Assert.assertEquals("Expect \"X\" for JCommander 1.74/75 and more recent", "\"X\"", aParameter);
}
@Test
public void testParameterListHavingQuotes() {
JCommander jc = new JCommander(this);
jc.parse("--aParameterList=\"X,Y\"");
Assert.assertNotNull(aParameterList);
Assert.assertEquals(2, aParameterList.size());
Assert.assertEquals("\"X", aParameterList.get(0));
Assert.assertEquals("Y\"", aParameterList.get(1));
}
}
Is this the desired behavior? If so is it an idea to document it on
Can this issue be closed and/or is the behavior like the testCase below expected? Is this the desired behavior? If so is it an idea to document it on
Thank you, Jan! I have added your unit test to the master branch. Indeed, it proofs that no quotes are removed. Hence I will close this issue as it seems to be fixed already. If anybody thinks the problem still exists, feel free to reopen this issue and post a unit test proving your claim. :-)
153 Is not fixed, for example:
@Parameter(names = {"-v", "--value"}, description = "JSON value to set the attribute to", required = true) String jsonValue;
From bash:
java -jar test.jar --value "\"some json string\""
Will result in jsonValue being set to "some json string", which will then fail to parse as it's no longer a valid json string
I'm not sure what the logic is behind that trim() function removing the outer quotes, as that is the shell's job not the program's.
In order to workaround this you have to do (in bash):
java -jar test.jar --value "\"\"some json string\"\""