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

YAML: Allow handling of custom tags #214

Open
nictas opened this issue Aug 6, 2020 · 4 comments
Open

YAML: Allow handling of custom tags #214

nictas opened this issue Aug 6, 2020 · 4 comments
Labels
pr-needed Feature request for which PR likely needed (no active development but idea is workable) yaml Issue related to YAML format backend

Comments

@nictas
Copy link

nictas commented Aug 6, 2020

We're trying to migrate from SnakeYAML to jackson-dataformat-yaml, but there's one thing that's currently stopping us. We parse YAML documents and allow our users to mark fields as sensitive, so that they're handled in a safe way. For example:

modules:
  - name: foo
    parameters:
      username: someone
      password: !sensitive Abcd1234

Note that the value of the password field is marked with a custom YAML tag - !sensitive.

We handle this custom tag with the following code:

import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.nodes.Tag;

public class YamlTaggedObjectsConstructor extends SafeConstructor {

    private static final String SENSITIVE_TAG = "!sensitive";

    public YamlTaggedObjectsConstructor() {
        this.yamlConstructors.put(new Tag(SENSITIVE_TAG), new SecureConstruct());
    }
}

Where SecureConstruct is our custom implementation of org.yaml.snakeyaml.constructor.AbstractConstruct that knows how to parse values marked with !sensitive.

Is there any chance you plan to add support for deserializers for custom tags or are they something you consider out-of-scope?

@cowtowncoder
Copy link
Member

cowtowncoder commented Aug 19, 2020

I think that should be in-scope, ideally, similar to how anchors can be accessed via YAMLParser (although for those, general-purpose getObjectId() from JsonParser is used).

In theory there is already JsonParser.getTypeId(), and that is wired to expose some of tags, but it looks like it may do too much pre-processing to work.
So maybe addition getRawTag() (or whatever name makes sense) in YAMLParser would make sense? Being API addition, that needs to go in next minor version, 2.12.0, and I would be happy to get a PR if you or anyone else was interested in contribution?

I realize that this would be just part of the challenge, as custom deserializers would need to use it.
But at least it would make this possible.

There may be, come to think of it now, other challenges wrt buffering (if content needs to be read in different order, for Creator parameters for example).
So alternatively having a setting to expose ALL tags via getTypeId() might be better -- object and type ids ("native" ones) are retained by TokenBuffer.

@cowtowncoder cowtowncoder added the yaml Issue related to YAML format backend label Aug 19, 2020
@buremba
Copy link

buremba commented Apr 13, 2021

I believe that getRawTag() is a good workaround given though it doesn't cover the buffering use-cases. I would love to try out!

@awattez
Copy link

awattez commented Feb 28, 2023

Hey, any update about this use case? Will this feature be planned?
There are very interesting possibilities of using this functionality
https://stackoverflow.com/questions/68782030/build-custom-snakeyaml-constructor-to-deserialize-yaml-file-in-a-modular-way

@cowtowncoder
Copy link
Member

cowtowncoder commented Feb 28, 2023

@awattez This would require external contribution; my time is too limited to work on this unfortunately.

Scope of work sounds pretty extensive, fwtw; but perhaps one could first expose means to add custom tags on output (serialization), then some hooks on deserialization. But I don't really have a solid idea of how this could be done end-to-end; challenge is not so much getting/putting tags via snakeyaml but rather how to make them work for Databinding; what to expose as -- most existing constructors are for things JSON has either natively, or by some sort of configuration (Type and Object Ids).

@cowtowncoder cowtowncoder added the pr-needed Feature request for which PR likely needed (no active development but idea is workable) label Feb 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-needed Feature request for which PR likely needed (no active development but idea is workable) yaml Issue related to YAML format backend
Projects
None yet
Development

No branches or pull requests

4 participants