jackson-dataformats-text icon indicating copy to clipboard operation
jackson-dataformats-text copied to clipboard

Nested Yaml question

Open sknot-rh opened this issue 3 years ago • 5 comments

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?

sknot-rh avatar Mar 31 '21 13:03 sknot-rh

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

cowtowncoder avatar Mar 31 '21 16:03 cowtowncoder

I tried YAMLGenerator.Feature.LITERAL_BLOCK_STYLE with no luck. I also tried various versions. Thi behaviour begins since 2.11.0 (included)

sknot-rh avatar Apr 01 '21 09:04 sknot-rh

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.

sknot-rh avatar Apr 01 '21 11:04 sknot-rh

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.

cowtowncoder avatar Apr 02 '21 16:04 cowtowncoder

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.

cowtowncoder avatar Apr 09 '21 01:04 cowtowncoder