Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YamlFile#load & #loadWithComments behave different on double quote value #79

Open
Ancash opened this issue Nov 22, 2023 · 6 comments
Open

Comments

@Ancash
Copy link

Ancash commented Nov 22, 2023

Version: 1.8.4
i have a yaml file:

test:
- "&8%plugin_so-sum_{cat}_{sub-cat}%&8 in &8%plugin_so-count_{cat}_{sub-cat}%&8 offers"

when i load it with YamlFile#load everything is fine.
when i load it with YamlFile#loadWithComments the file now looks like this:

test: offers'    offers'

this is not the behaviour i expected because there are no "#" denoting a comment

@Ancash
Copy link
Author

Ancash commented Nov 23, 2023

Using the following produces not the expected output (replaces the input with the output mentioned in the post before)

YamlFile yaml = new YamlFile();
yaml.options().useComments(true);
yaml.set("test", Arrays.asList("&8%plugin_so-sum_{cat}_{sub-cat}%&8 in &8%plugin_so-count_{cat}_{sub-cat}%&8 offers"));
System.out.println(yaml.saveToString());

but the following snippets do

YamlFile yaml = new YamlFile();
yaml.set("test", Arrays.asList("&8%plugin_so-sum_{cat}_{sub-cat}%&8 in &8%plugin_so-count_{cat}_{sub-cat}%&8 offers"));
System.out.println(yaml.saveToString());
YamlFile yaml = new YamlFile();
yaml.set("test", "&8%plugin_so-sum_{cat}_{sub-cat}%&8 in &8%plugin_so-count_{cat}_{sub-cat}%&8 offers");
System.out.println(yaml.saveToString());
YamlFile yaml = new YamlFile();
yaml.options().useComments(true);
yaml.set("test", "&8%plugin_so-sum_{cat}_{sub-cat}%&8 in &8%plugin_so-count_{cat}_{sub-cat}%&8 offers");
System.out.println(yaml.saveToString());

@Ancash
Copy link
Author

Ancash commented Nov 23, 2023

somehow leaving out the &8 makes it work

YamlFile yaml = new YamlFile();
yaml.options().useComments(true);
yaml.set("test", Arrays.asList("%plugin_so-sum_{cat}_{sub-cat}% in %plugin_so-count_{cat}_{sub-cat}% offers"));
System.out.println(yaml.saveToString());

@Carleslc
Copy link
Owner

According to your examples seems to be an issue with list elements having some special characters while using the comment parser. With a simple string value does not happen 🤔

& is used for key references in yaml (anchors and aliases):
https://yaml.org/spec/1.2.2/#692-node-anchors
Maybe is something related to that.

Try these other snippets independently and see if the same happens:

  • Add something before the & starting character.
  • Add QuoteStyle.DOUBLE as the third parameter in the set method to force double quotes.
  • Use &8 but remove { and } characters.

@Ancash
Copy link
Author

Ancash commented Nov 23, 2023

YamlFile yaml = new YamlFile();
yaml.options().useComments(true);
yaml.set("test",Arrays.asList("&8%plugin_so-sum_{cat}_{sub-cat}%&8 in &8%plugin_so-count_{cat}_{sub-cat}%&8 offers"), QuoteStyle.DOUBLE);
System.out.println(yaml.saveToString());

outputs

test:

    \ offers"    \ offers"
YamlFile yaml = new YamlFile();
yaml.options().useComments(true);
yaml.set("test",Arrays.asList("&8%plugi_so-sum_{cat}_{sub-cat}%&8 in &8%plugin_so-count_{cat}_{sub-cat}%&8 offers"));
System.out.println(yaml.saveToString());

outputs

test:
  - '&8%plugi_so-sum_{cat}_{sub-cat}%&8 in &8%plugin_so-count_{cat}_{sub-cat}%&8 offers'
YamlFile yaml = new YamlFile();
yaml.options().useComments(true);
yaml.set("test",Arrays.asList("&8%plugin_so-sum%&8 in &8%plugin_so-count%&8 offers"));
System.out.println(yaml.saveToString());

outputs

test:
  - '&8%plugin_so-sum%&8 in &8%plugin_so-count%&8 offers'
YamlFile yaml = new YamlFile();
yaml.options().useComments(true);
yaml.set("test",Arrays.asList("abcd&8%plugin_so-sum_{cat}_{sub-cat}%&8 in &8%plugin_so-count_{cat}_{sub-cat}%&8 offers"));
System.out.println(yaml.saveToString());

outputs

test:
  - abcd&8%plugin_so-sum_{cat}_{sub-cat}%&8 in &8%plugin_so-count_{cat}_{sub-cat}%&8
    offers
YamlFile yaml = new YamlFile();
yaml.options().useComments(true);
yaml.set("test",Arrays.asList("&8%plugin_so-sum_%cat%_%sub-cat%%&8 in &8%plugin_so-count_%cat%_%sub-cat%%&8 offers"), QuoteStyle.DOUBLE);
System.out.println(yaml.saveToString());

outputs

test:

    \ offers"    \ offers"
YamlFile yaml = new YamlFile();
yaml.options().useComments(true);
yaml.set("test",Arrays.asList("&8%plugin_so-sum_cat%_%sub-cat%%&8 in &8%plugin_so-count_%cat%_%sub-cat%%&8 offers"));
System.out.println(yaml.saveToString());

outputs

test:
  - '&8%plugin_so-sum_cat%_%sub-cat%%&8 in &8%plugin_so-count_%cat%_%sub-cat%%&8 offers'

@Ancash
Copy link
Author

Ancash commented Nov 29, 2023

using the following works in 1.7.3 but not in 1.8.1 1.8.2 1.8.3

YamlFile yaml = new YamlFile();
yaml.set("test",Arrays.asList("&8%plugin_so-sum_%cat%_%sub-cat%%&8 in &8%plugin_so-count_%cat%_%sub-cat%%&8 offers"));
yaml = YamlFile.loadConfiguration(new StringReader(yaml.saveToString()), true);
System.out.println(yaml.saveToString());

@Ancash
Copy link
Author

Ancash commented Dec 11, 2023

Using SnakeYamlImplementation works.
After some more testing it seems like SimpleYamlImplementation is having trouble with dumping multi line list elements.

DumperOptions dop = new DumperOptions();
dop.setWidth(10);
YamlFile yaml = new YamlFile();
yaml.set("test",Arrays.asList("&8%plugin_so-sum_%cat%_%sub-cat%%&8 in &8%plugin_so-count_%cat%_%sub-cat%%&8 offers"),	QuoteStyle.DOUBLE);
String savedToString = yaml.saveToString();
System.out.println(savedToString);
yaml = new YamlFile(new SimpleYamlImplementation(new LoaderOptions(), dop));
yaml.options().useComments(true);
yaml.loadFromString(savedToString);
System.out.println(yaml.saveToString());

outputs

test:
  - "&8%plugin_so-sum_%cat%_%sub-cat%%&8 in &8%plugin_so-count_%cat%_%sub-cat%%&8\
    \ offers"

test:

    in &8%plugin_so-count_%cat%_%sub-cat%%&8
    offers'    offers'

where as

DumperOptions dop = new DumperOptions();
dop.setWidth(100);
YamlFile yaml = new YamlFile();
yaml.set("test",Arrays.asList("&8%plugin_so-sum_%cat%_%sub-cat%%&8 in &8%plugin_so-count_%cat%_%sub-cat%%&8 offers"),	QuoteStyle.DOUBLE);
String savedToString = yaml.saveToString();
System.out.println(savedToString);
yaml = new YamlFile(new SimpleYamlImplementation(new LoaderOptions(), dop));
yaml.options().useComments(true);
yaml.loadFromString(savedToString);
System.out.println(yaml.saveToString());

outputs

test:
  - "&8%plugin_so-sum_%cat%_%sub-cat%%&8 in &8%plugin_so-count_%cat%_%sub-cat%%&8\
    \ offers"

test:
  - '&8%plugin_so-sum_%cat%_%sub-cat%%&8 in &8%plugin_so-count_%cat%_%sub-cat%%&8 offers'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants