Increase simple key length to 1024 stream characters
Version
SnakeYAML 1.23, Java 1.8.0_181
Problem
Accordingly to https://yaml.org/spec/1.1/#id934537, the simple key must not span more than 1024 stream characters while now it is limited to just 128 characters, which causes the output to be broken on valid input data.
Reproducing the problem
Building YAML out of the Spring MVC controllers using springfox, jackson, and snakeyaml. One of the REST methods has 13 query parameters, and the displayed name has the length of 160+ symbols.
Expected output
paths:
/examples/{exampleId}/example/example{?exmplType,exm,exmplsOnly,examplea,exmplFrom,exmplTo,exmplLimit,exmplLocal,exmplRequest,exmpls,exampleb,exmplTypes,examplec}:
get:
tags:
Actual output (broken)
paths:
? /examples/{exampleId}/example/example{?exmplType,exm,exmplsOnly,examplea,exmplFrom,exmplTo,exmplLimit,exmplLocal,exmplRequest,exmpls,exampleb,exmplTypes,examplec}
: get:
tags:
Proposed solution
Increase the limit to 1024 characters here, preferably making a constant out of the hardcoded value:
Comments (14)
-
-
Please be aware that the output is not "broken" as indicated above. It is a valid YAML but the key is explicit.
-
How can I reproduce the problem ?
-
reporter Well, springfox highlights the line with such key as an error while trying to parse the file for displaying it in the UI (see https://springfox.github.io/springfox/docs/current/#springfox-swagger-ui)
I'll try to make a sample project reproducing this behaviour. This may take a day or two.
-
If you build SnakeYAML with the expected limit does it solve the issue ?
-
reporter Yes, rebuilding SnakeYAML with the limit of 1024 solves the issue.
-
- changed status to resolved
Fix issue 431: Customize simple key length when dumping
→ <<cset c95b00780448>>
-
It will be provided in version 1.24 - https://bitbucket.org/asomov/snakeyaml/wiki/Changes
-
reporter Thank you Andrey! I've created a sample project, however it seems to be useless now.
-
It is not useless !! Can you please send it me ? I can try to add it to the tests
-
reporter - attached snakeyaml-simple-key-behaviour.zip
Sample app showing the parser behaviour on keys longer than 128 symbols when generating Swagger YAML out of the Spring MVC controllers using SpringFox, Jackson, and SnakeYAML
-
reporter I've attached the demo app. For now I have no idea how to proxy the newly introduced DumperOption from the Spring config when the YamlFactory is created.
-
As far as I know they use Jackson, which means that you need to ask Jackson community to accept another setting:
public YAMLGenerator(IOContext ctxt, int jsonFeatures, int yamlFeatures, ObjectCodec codec, Writer out, Version version) throws IOException { super(jsonFeatures, codec); this._ioContext = ctxt; this._formatFeatures = yamlFeatures; this._writer = out; this._outputOptions = this.buildDumperOptions(jsonFeatures, yamlFeatures, version); this._emitter = new Emitter(this._writer, this._outputOptions); this._emitter.emit(new StreamStartEvent((Mark)null, (Mark)null)); Map<String, String> noTags = Collections.emptyMap(); boolean startMarker = YAMLGenerator.Feature.WRITE_DOC_START_MARKER.enabledIn(yamlFeatures); this._emitter.emit(new DocumentStartEvent((Mark)null, (Mark)null, startMarker, version, noTags)); }
-
reporter Looks like this method in YAMLGenerator is the subject to change:
protected DumperOptions buildDumperOptions(int jsonFeatures, int yamlFeatures, org.yaml.snakeyaml.DumperOptions.Version version) { DumperOptions opt = new DumperOptions(); // would we want canonical? if (Feature.CANONICAL_OUTPUT.enabledIn(_formatFeatures)) { opt.setCanonical(true); } else { opt.setCanonical(false); // if not, MUST specify flow styles opt.setDefaultFlowStyle(FlowStyle.BLOCK); } // [dataformat#35]: split-lines for text blocks? opt.setSplitLines(Feature.SPLIT_LINES.enabledIn(_formatFeatures)); return opt; }
- Log in to comment
Sorry, my fault. I was too quick with my conclusion. This is not a question of specification but a flavour of SnakeYAML to decide when to use simple key.
It will require to change the DumperOptions to introduce a setting to customize the length.