jackson-dataformats-text
jackson-dataformats-text copied to clipboard
Nested Yaml question
Hi. I was using version 2.10.5 and my code was able to process nested yamls correctly. With version 2.12.2 I am not able to make it work.
Code:
YAMLMapper yamlMapper = new YAMLMapper(new YAMLFactory().enable(YAMLGenerator.Feature.MINIMIZE_QUOTES));
yamlMapper.writerWithDefaultPrettyPrinter().writeValue(new File("output.yaml"), resource.getResource());
Output with version 2.10.5
---
apiVersion: v1
kind: MyKind
metadata:
name: example
data:
config.yaml: |
lowercaseOutputName: true
rules:
- pattern: "metric<name=(.+)><>(\\w+)"
name: "metric_$1_$2"
type: "GAUGE"
Output with version 2.12.2
---
apiVersion: v1
kind: MyKind
metadata:
name: example
data:
config.yaml: "lowercaseOutputName:\
\ true\nrules:\n- pattern: \"metric<name=(.+)><>(\\\\w+)\"\n name:\
\ \"metric_$1_$2\"\n type: \"GAUGE\"\n"
Am I missing some serialization option?
I assume value of config.yaml
is String
(including definition of Java value being serialized would be useful here).
If so, contents being serialized are identical but definitely look ugly.
Unfortunately I don't know what might have changed here. It might make sense to see what 2.11.4 does to maybe pinpoint change(s) to logic. I suspect this is related to logic wrt escaping of unsafe values.
One setting to maybe try would be YAMLGenerator.Feature.LITERAL_BLOCK_STYLE
I tried YAMLGenerator.Feature.LITERAL_BLOCK_STYLE
with no luck.
I also tried various versions. Thi behaviour begins since 2.11.0 (included)
I did some digging.
The difference happens there: https://github.com/FasterXML/jackson-dataformats-text/blob/2.10/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java#L982 https://github.com/FasterXML/jackson-dataformats-text/blob/2.11/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLGenerator.java#L987
The affected condition (this._valueNeedsQuoting(text)
) returns false on 2.10.5 but true in 2.11.x
The text
is whatever string containing quotes.
Yes. This is difficult because leaving out quoting is dangerous. But I know that use case of "just leave out minimally processed chunk to use in k8s manifests (Helm Charts, whatever)" is pretty common and this is problematic for user-editability.
This is probably related to #246.
Also forgot to mention that a unit test would be needed to show details (explanation alone is not sufficient to reproduce). It would be good to have a small reproduction, but I'll proceed with #246 since I think that will resolve this issue too.