Search in sources :

Example 6 with LoaderOptions

use of org.yaml.snakeyaml.LoaderOptions in project spring-framework by spring-projects.

the class YamlProcessor method createYaml.

/**
 * Create the {@link Yaml} instance to use.
 * <p>The default implementation sets the "allowDuplicateKeys" flag to {@code false},
 * enabling built-in duplicate key handling in SnakeYAML 1.18+.
 * <p>As of Spring Framework 5.1.16, if custom {@linkplain #setSupportedTypes
 * supported types} have been configured, the default implementation creates
 * a {@code Yaml} instance that filters out unsupported types encountered in
 * YAML documents. If an unsupported type is encountered, an
 * {@link IllegalStateException} will be thrown when the node is processed.
 * @see LoaderOptions#setAllowDuplicateKeys(boolean)
 */
protected Yaml createYaml() {
    LoaderOptions loaderOptions = new LoaderOptions();
    loaderOptions.setAllowDuplicateKeys(false);
    return new Yaml(new FilteringConstructor(loaderOptions), new Representer(), new DumperOptions(), loaderOptions);
}
Also used : LoaderOptions(org.yaml.snakeyaml.LoaderOptions) Representer(org.yaml.snakeyaml.representer.Representer) DumperOptions(org.yaml.snakeyaml.DumperOptions) Yaml(org.yaml.snakeyaml.Yaml)

Example 7 with LoaderOptions

use of org.yaml.snakeyaml.LoaderOptions in project configuration-as-code-plugin by jenkinsci.

the class YamlUtils method read.

public static Node read(YamlSource source, Reader reader, ConfigurationContext context) throws IOException {
    LoaderOptions loaderOptions = new LoaderOptions();
    loaderOptions.setMaxAliasesForCollections(context.getYamlMaxAliasesForCollections());
    Composer composer = new Composer(new ParserImpl(new StreamReaderWithSource(source, reader)), new Resolver(), loaderOptions);
    try {
        return composer.getSingleNode();
    } catch (YAMLException e) {
        if (e.getMessage().startsWith("Number of aliases for non-scalar nodes exceeds the specified max")) {
            throw new ConfiguratorException(String.format("%s%nYou can increase the maximum by setting an environment variable or property%n  ENV: %s=\"100\"%n  PROPERTY: -D%s=\"100\"", e.getMessage(), ConfigurationContext.CASC_YAML_MAX_ALIASES_ENV, ConfigurationContext.CASC_YAML_MAX_ALIASES_PROPERTY));
        }
        throw e;
    }
}
Also used : Composer(org.yaml.snakeyaml.composer.Composer) LoaderOptions(org.yaml.snakeyaml.LoaderOptions) Resolver(org.yaml.snakeyaml.resolver.Resolver) ParserImpl(org.yaml.snakeyaml.parser.ParserImpl) YAMLException(org.yaml.snakeyaml.error.YAMLException) ConfiguratorException(io.jenkins.plugins.casc.ConfiguratorException)

Aggregations

LoaderOptions (org.yaml.snakeyaml.LoaderOptions)7 DumperOptions (org.yaml.snakeyaml.DumperOptions)4 Representer (org.yaml.snakeyaml.representer.Representer)4 Yaml (org.yaml.snakeyaml.Yaml)3 SafeConstructor (org.yaml.snakeyaml.constructor.SafeConstructor)2 YAMLException (org.yaml.snakeyaml.error.YAMLException)2 ConfiguratorException (io.jenkins.plugins.casc.ConfiguratorException)1 Yaml (io.swagger.v3.core.util.Yaml)1 Method (java.lang.reflect.Method)1 TypeDescription (org.yaml.snakeyaml.TypeDescription)1 Composer (org.yaml.snakeyaml.composer.Composer)1 BaseConstructor (org.yaml.snakeyaml.constructor.BaseConstructor)1 Constructor (org.yaml.snakeyaml.constructor.Constructor)1 PropertyUtils (org.yaml.snakeyaml.introspector.PropertyUtils)1 ParserImpl (org.yaml.snakeyaml.parser.ParserImpl)1 Resolver (org.yaml.snakeyaml.resolver.Resolver)1