Skip to content

Commit

Permalink
provision to choose to raise error if there are duplicate keys in YAM…
Browse files Browse the repository at this point in the history
…L - issue EsotericSoftware#57 (EsotericSoftware#60)

* fix for EsotericSoftware#57

* as per review by @NathanSweet on fix for EsotericSoftware#57

* removing the whitespace in the eof

* trying to remove the spaces in files so that git does not complain about

* adding gitattributes file to fix the line endings to clrf (windows style)

* reverting my changes to see if I can redo them to fix the line ending now.. also added a .gitattributes file to keep the line endings to windows style

* Saving files before refreshing line endings

* redone my changes

* correcting the .gitattributes file

* removing .gitattributes

* fix line endings to windows

* fixing line endings to windows format
  • Loading branch information
sanjusoftware authored and NathanSweet committed Nov 5, 2016
1 parent 8f9858a commit ad36665
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@

bin/
target/
.idea
*.iml
8 changes: 7 additions & 1 deletion src/com/esotericsoftware/yamlbeans/YamlConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class YamlConfig {
boolean beanProperties = true;
boolean privateFields;
boolean privateConstructors = true;
boolean allowDuplicates = true;

public YamlConfig () {
scalarSerializers.put(Date.class, new DateSerializer());
Expand All @@ -58,6 +59,11 @@ public YamlConfig () {
tagToClass.put("tag:yaml.org,2002:float", Float.class);
}

/** Allows duplicate keys in YAML document. Default is true. */
public void setAllowDuplicates (boolean allowDuplicates) {
this.allowDuplicates = allowDuplicates;
}

/** Allows the specified tag to be used in YAML instead of the full class name. */
public void setClassTag (String tag, Class type) {
if (tag == null) throw new IllegalArgumentException("tag cannot be null.");
Expand Down Expand Up @@ -264,4 +270,4 @@ static class ConstructorParameters {
public static enum WriteClassName {
ALWAYS, NEVER, AUTO
}
}
}
11 changes: 10 additions & 1 deletion src/com/esotericsoftware/yamlbeans/YamlReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ private Object readValueInternal (Class type, Class elementType, String anchor)
throw new YamlReaderException("Error creating object.", ex);
}
if (anchor != null) anchors.put(anchor, object);
ArrayList keys = new ArrayList();
while (true) {
if (parser.peekNextEvent().type == MAPPING_END) {
parser.getNextEvent();
Expand All @@ -325,10 +326,18 @@ private Object readValueInternal (Class type, Class elementType, String anchor)
if (object instanceof Map) {
// Add to map.
if (!isExplicitKey) value = readValue(elementType, null, null);
if (!config.allowDuplicates && ((Map) object).containsKey(key)) {
throw new YamlReaderException("Duplicate key found '" + key + "'");
}
((Map)object).put(key, value);
} else {
// Set field on object.
try {
if (!config.allowDuplicates && keys.contains(key)) {
throw new YamlReaderException("Duplicate key found '" + key + "'");
}
keys.add(key);

Property property = Beans.getProperty(type, (String)key, config.beanProperties, config.privateFields, config);
if (property == null) {
if (config.readConfig.ignoreUnknownProperties) continue;
Expand Down Expand Up @@ -421,4 +430,4 @@ public static void main (String[] args) throws Exception {
YamlReader reader = new YamlReader(new FileReader("test/test.yml"));
System.out.println(reader.read());
}
}
}
45 changes: 44 additions & 1 deletion test/com/esotericsoftware/yamlbeans/YamlReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.esotericsoftware.yamlbeans;

import java.io.StringReader;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -318,6 +319,48 @@ public void testIgnoreUnknownProperties () {
}
}

public void testDuplicateKeysAreNotAllowedIfAllowDuplicateIsSetSo () {
String inputWithDuplicates = "a: 1\na: 2\nc: 3";
YamlConfig yamlConfig = new YamlConfig();
yamlConfig.setAllowDuplicates(false);
YamlReader yamlReader = new YamlReader(inputWithDuplicates, yamlConfig);
try {
yamlReader.read();
fail("Duplicates should not have been allowed.");
} catch (YamlException e) {
}

String inputWithoutDuplicates = "a: 1\nb: 2\nc: 3";
YamlReader yamlReader1 = new YamlReader(inputWithoutDuplicates, yamlConfig);
try {
yamlReader1.read();
} catch (YamlException e) {
fail("Duplicates should have been allowed.");
}
}

public void testDuplicateKeysAreAllowedIfAllowDuplicateIsSetSo () {
String inputWithDuplicates = "a: 1\na: 2\nc: 3";
YamlReader yamlReader = new YamlReader(inputWithDuplicates);
try {
yamlReader.read();
} catch (YamlException e) {
e.printStackTrace();
fail("Duplicates should have been allowed.");
}

String inputWithoutDuplicates = "a: 1\nb: 2\nc: 3";
YamlConfig yamlConfig = new YamlConfig();
yamlConfig.setAllowDuplicates(false);
YamlReader yamlReader1 = new YamlReader(inputWithoutDuplicates, yamlConfig);
try {
yamlReader1.read();
} catch (YamlException e) {
e.printStackTrace();
fail("Duplicates should have been allowed.");
}
}

private static class TypeTagIgnoringReader extends YamlReader {

public TypeTagIgnoringReader(String yaml) {
Expand Down Expand Up @@ -385,4 +428,4 @@ public void testIgnoreTypeTagsEmbedded () throws YamlException {
assertEquals(1, lake.fish.size());
assertEquals("Walleye", lake.fish.get(0).species);
}
}
}

0 comments on commit ad36665

Please sign in to comment.