diff --git a/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLFactoryBuilder.java b/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLFactoryBuilder.java index 6c2b0063..11ff63ac 100644 --- a/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLFactoryBuilder.java +++ b/yaml/src/main/java/com/fasterxml/jackson/dataformat/yaml/YAMLFactoryBuilder.java @@ -1,5 +1,6 @@ package com.fasterxml.jackson.dataformat.yaml; +import com.fasterxml.jackson.core.StreamReadFeature; import org.yaml.snakeyaml.DumperOptions; import com.fasterxml.jackson.core.TSFBuilder; @@ -226,6 +227,11 @@ public YAMLFactoryBuilder yamlVersionToWrite(DumperOptions.Version v) { */ public YAMLFactoryBuilder loaderOptions(LoaderOptions loaderOptions) { _loaderOptions = loaderOptions; + + // If the user wants to block duplicate keys this needs to be set in a different way to work + if (!_loaderOptions.isAllowDuplicateKeys()) { + enable(StreamReadFeature.STRICT_DUPLICATE_DETECTION); + } return this; } diff --git a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/ParserDupHandlingTest.java b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/ParserDupHandlingTest.java index afc5f009..48a26042 100644 --- a/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/ParserDupHandlingTest.java +++ b/yaml/src/test/java/com/fasterxml/jackson/dataformat/yaml/deser/ParserDupHandlingTest.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import org.yaml.snakeyaml.LoaderOptions; public class ParserDupHandlingTest extends ModuleTestBase { @@ -33,6 +34,17 @@ public void testDupChecksEnabled() throws Exception _verifyDupsFail(mapper, YAML_WITH_DUPS, true); } + public void testDupChecksEnabledLoaderOptions() throws Exception + { + LoaderOptions loaderOptions = new LoaderOptions(); + loaderOptions.setAllowDuplicateKeys(false); + YAMLFactory f = YAMLFactory.builder().loaderOptions(loaderOptions).build(); + + ObjectMapper mapper = new ObjectMapper(f); + _verifyDupsFail(mapper, YAML_WITH_DUPS, false); + _verifyDupsFail(mapper, YAML_WITH_DUPS, true); + } + private void _verifyDupsOk(ObjectMapper mapper, String doc, boolean useBytes) throws Exception { JsonParser p = useBytes ? mapper.getFactory().createParser(new ByteArrayInputStream(doc.getBytes("UTF-8")))