Search in sources :

Example 1 with LoaderOptions

use of org.yaml.snakeyaml.LoaderOptions in project swagger-parser by swagger-api.

the class DeserializationUtils method buildSnakeYaml.

public static org.yaml.snakeyaml.Yaml buildSnakeYaml(BaseConstructor constructor) {
    try {
        LoaderOptions.class.getMethod("getMaxAliasesForCollections");
    } catch (NoSuchMethodException e) {
        return new org.yaml.snakeyaml.Yaml(constructor);
    }
    try {
        LoaderOptions loaderOptions = new LoaderOptions();
        Method method = LoaderOptions.class.getMethod("setMaxAliasesForCollections", int.class);
        method.invoke(loaderOptions, options.getMaxYamlAliasesForCollections());
        method = LoaderOptions.class.getMethod("setAllowRecursiveKeys", boolean.class);
        method.invoke(loaderOptions, options.isYamlAllowRecursiveKeys());
        method = LoaderOptions.class.getMethod("setAllowDuplicateKeys", boolean.class);
        method.invoke(loaderOptions, false);
        org.yaml.snakeyaml.Yaml yaml = new org.yaml.snakeyaml.Yaml(constructor, new Representer(), new DumperOptions(), loaderOptions, new CustomResolver());
        return yaml;
    } catch (ReflectiveOperationException e) {
        // 
        LOGGER.debug("using snakeyaml < 1.25, not setting YAML Billion Laughs Attack snakeyaml level protection");
    }
    return new org.yaml.snakeyaml.Yaml(constructor);
}
Also used : LoaderOptions(org.yaml.snakeyaml.LoaderOptions) Representer(org.yaml.snakeyaml.representer.Representer) DumperOptions(org.yaml.snakeyaml.DumperOptions) Method(java.lang.reflect.Method) Yaml(io.swagger.v3.core.util.Yaml)

Example 2 with LoaderOptions

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

the class OriginTrackedYamlLoader method createYaml.

@Override
protected Yaml createYaml() {
    LoaderOptions loaderOptions = new LoaderOptions();
    loaderOptions.setAllowDuplicateKeys(false);
    loaderOptions.setMaxAliasesForCollections(Integer.MAX_VALUE);
    loaderOptions.setAllowRecursiveKeys(true);
    return createYaml(loaderOptions);
}
Also used : LoaderOptions(org.yaml.snakeyaml.LoaderOptions)

Example 3 with LoaderOptions

use of org.yaml.snakeyaml.LoaderOptions in project apollo by ctripcorp.

the class YamlParser method createYaml.

/**
 * Create the {@link Yaml} instance to use.
 */
private Yaml createYaml() {
    LoaderOptions loadingConfig = new LoaderOptions();
    loadingConfig.setAllowDuplicateKeys(false);
    return new Yaml(new SafeConstructor(), new Representer(), new DumperOptions(), loadingConfig);
}
Also used : LoaderOptions(org.yaml.snakeyaml.LoaderOptions) Representer(org.yaml.snakeyaml.representer.Representer) SafeConstructor(org.yaml.snakeyaml.constructor.SafeConstructor) DumperOptions(org.yaml.snakeyaml.DumperOptions) Yaml(org.yaml.snakeyaml.Yaml)

Example 4 with LoaderOptions

use of org.yaml.snakeyaml.LoaderOptions in project es6draft by anba.

the class Test262Info method readYaml.

private void readYaml(String descriptor, ErrorHandler error) throws MalformedDataException {
    assert descriptor != null && !descriptor.isEmpty();
    boolean lenient = error == ErrorHandler.Ignore;
    Yaml yaml = null;
    if (lenient) {
        yaml = yamlQueue.poll();
    }
    if (yaml == null) {
        Constructor constructor = new Constructor(TestDescriptor.class);
        if (lenient) {
            PropertyUtils utils = new PropertyUtils();
            utils.setSkipMissingProperties(true);
            constructor.setPropertyUtils(utils);
        }
        yaml = new Yaml(constructor);
        if (!lenient) {
            LoaderOptions loaderOptions = new LoaderOptions();
            loaderOptions.setAllowDuplicateKeys(false);
        }
    }
    TestDescriptor desc;
    try {
        desc = yaml.loadAs(descriptor, TestDescriptor.class);
    } catch (YAMLException e) {
        throw new MalformedDataException(e.getMessage(), e);
    }
    if (lenient) {
        yamlQueue.offer(yaml);
    }
    this.description = desc.getDescription();
    this.includes = desc.getIncludes();
    this.features = desc.getFeatures();
    this.errorType = desc.getNegative().getType();
    this.errorPhase = from(desc.getNegative().getPhase(), lenient);
    this.negative = desc.getNegative().getType() != null;
    if (!desc.getFlags().isEmpty()) {
        if (!lenient) {
            for (String flag : desc.getFlags()) {
                if (!allowedFlags.contains(flag)) {
                    throw new MalformedDataException(String.format("Unknown flag '%s'", flag));
                }
            }
        }
        this.noStrict = desc.getFlags().contains("noStrict");
        this.onlyStrict = desc.getFlags().contains("onlyStrict");
        this.module = desc.getFlags().contains("module");
        this.raw = desc.getFlags().contains("raw");
        this.async = desc.getFlags().contains("async");
    }
    if (!lenient && errorPhase == ErrorPhase.Resolution && !module) {
        throw new MalformedDataException(String.format("Invalid error phase 'resolution' for non-module test"));
    }
}
Also used : LoaderOptions(org.yaml.snakeyaml.LoaderOptions) PropertyUtils(org.yaml.snakeyaml.introspector.PropertyUtils) Constructor(org.yaml.snakeyaml.constructor.Constructor) YAMLException(org.yaml.snakeyaml.error.YAMLException) Yaml(org.yaml.snakeyaml.Yaml)

Example 5 with LoaderOptions

use of org.yaml.snakeyaml.LoaderOptions in project java by kubernetes-client.

the class Yaml method getSnakeYaml.

/**
 * Instantiate a snake yaml with the target model type, and the dump/load option specified..
 * Optionally, passing custom type descriptions if the target serialized name differs from the
 * actual field name.
 *
 * @param type the target model type
 * @param dumperOptions the dumper options
 * @param loaderOptions the loader options
 * @param typeDescriptions additional type descriptions for customizing the serializer
 * @return the new snake yaml instance
 */
public static org.yaml.snakeyaml.Yaml getSnakeYaml(Class<?> type, DumperOptions dumperOptions, LoaderOptions loaderOptions, TypeDescription... typeDescriptions) {
    if (loaderOptions == null) {
        loaderOptions = new LoaderOptions();
    }
    loaderOptions.setEnumCaseSensitive(false);
    BaseConstructor constructor = new SafeConstructor(loaderOptions);
    Representer representer = new CustomRepresenter();
    if (type != null) {
        constructor = new CustomConstructor(type, loaderOptions);
    }
    // register builtin type descriptions
    registerBuiltinGsonCompatibles(constructor, representer);
    for (TypeDescription desc : typeDescriptions) {
        registerCustomTypeDescriptions(constructor, representer, desc);
    }
    if (dumperOptions == null) {
        dumperOptions = new DumperOptions();
        dumperOptions.setDefaultFlowStyle(representer.getDefaultFlowStyle());
        dumperOptions.setDefaultScalarStyle(representer.getDefaultScalarStyle());
        dumperOptions.setAllowReadOnlyProperties(representer.getPropertyUtils().isAllowReadOnlyProperties());
        dumperOptions.setTimeZone(representer.getTimeZone());
    }
    return new org.yaml.snakeyaml.Yaml(constructor, representer, dumperOptions, loaderOptions);
}
Also used : BaseConstructor(org.yaml.snakeyaml.constructor.BaseConstructor) LoaderOptions(org.yaml.snakeyaml.LoaderOptions) Representer(org.yaml.snakeyaml.representer.Representer) SafeConstructor(org.yaml.snakeyaml.constructor.SafeConstructor) TypeDescription(org.yaml.snakeyaml.TypeDescription) DumperOptions(org.yaml.snakeyaml.DumperOptions)

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