FasterXML / jackson-dataformats-text

Uber-project for (some) standard Jackson textual format backends: csv, properties, yaml (xml to be added in future)
Apache License 2.0
404 stars 148 forks source link

Add support for non-blocking ("async") YAML parsing #165

Open guillermocalvo opened 4 years ago

guillermocalvo commented 4 years ago

Jackson-core supports non-blocking parsers since version 2.9, but YAML doesn't seem to have a non-blocking parser yet.

This is preventing us from parsing YAML request bodies directly in reactive web apps (Spring WebFlux).

These are the steps to reproduce this problem:

  1. Create a YAML decoder:
    public class YamlDecoder extends AbstractJackson2Decoder {
    public YamlDecoder() {
        super(new YAMLMapper(), MediaType.valueOf("application/x-yaml"));
    }
    }
  2. Add it as a custom decoder:
    @EnableWebFlux
    public class AppConfiguration implements WebFluxConfigurer {
    ...
    @Override
    public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
        configurer.customCodecs().decoder(new YamlDecoder());
    }
    }
  3. Add a REST controller with a mapping that consumes YAML:
    @RestController
    public class FoobarController {
    @PostMapping(value = "/foo", consumes = "application/x-yaml")
    public void foo(@RequestBody MyCustomClass body) {
        ...
    }
    }
  4. Send a YAML request. Then an UnsupportedOperationException will be thrown from JsonFactory with the message:
    Non-blocking source not (yet?) supported for this format (YAML)
cowtowncoder commented 4 years ago

True, non-blocking handling has so far only been implemented for JSON and Smile backends. Of other backends, CBOR should be quite easy, and XML plausible with aalto-xml.

But the challenge with YAML is that this would require SnakeYAML to support it, and then format module could use it.

So: if anyone has time and interest to work on this, it'd be interesting challenge. :)