Skip to content

Commit

Permalink
Disallow null values in collection elements - Fixes #23
Browse files Browse the repository at this point in the history
  • Loading branch information
A248 committed Dec 4, 2021
1 parent 49be383 commit 1409743
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public Object get(String key) throws MissingKeyException, MissingValueException
if (value == null) {
if (currentMap.containsKey(keyParts[lastIndex])) {
// Null value
throw MissingValueException.forKeyAndMessage(key, UserError.missingKey(key));
throw MissingValueException.forKeyAndMessage(key, UserError.nullValue(key));
} else {
// Absent value
throw MissingKeyException.forKeyAndMessage(key, UserError.missingKey(key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ public static UserError missingKey(String key) {
"the configuration option at " + key + ", then set it to a valid value.");
}

public static UserError nullValue(String key) {
return new UserError(Errors.When.LOAD_CONFIG,
"The configuration option at " + key + " was set to an empty value. " +
"You must set it to a valid value - it cannot be empty.");
}

@Override
public Errors.When when() {
return when;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ final class FlexibleTypeImpl implements FlexibleType {

FlexibleTypeImpl(String key, Object value, ConfigurationOptions options,
ValueSerialiserMap serialisers) {
this.key = key;
this.value = value;
this.options = options;
this.serialisers = serialisers;
this.key = Objects.requireNonNull(key, "key");
this.value = Objects.requireNonNull(value, "value");
this.options = Objects.requireNonNull(options, "options");
this.serialisers = Objects.requireNonNull(serialisers, "serialisers");
}

@Override
Expand Down Expand Up @@ -260,7 +260,12 @@ public <K, V> Map<K, V> getMap(FlexibleTypeMapEntryFunction<? extends K, ? exten
return ImmutableCollections.mapOf(result);
}

private FlexibleTypeImpl deriveFlexibleObject(Object value) {
private FlexibleTypeImpl deriveFlexibleObject(Object value) throws BadValueException {
if (value == null) {
throw badValueExceptionBuilder()
.message(UserError.nullValue(key))
.build();
}
return new FlexibleTypeImpl(key, value, options, serialisers);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,20 @@

package space.arim.dazzleconf.ext.snakeyaml;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import space.arim.dazzleconf.ConfigurationOptions;
import space.arim.dazzleconf.error.BadValueException;
import space.arim.dazzleconf.error.InvalidConfigException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class NullListElementTest {

@Disabled("Fix scheduled for 1.3.0. See https://github.com/A248/DazzleConf/issues/23")
@Test
public void readLiteralNullListElement() throws IOException, InvalidConfigException {
String content = """
Expand All @@ -43,11 +41,10 @@ public void readLiteralNullListElement() throws IOException, InvalidConfigExcept
- 'value2'
""";
var factory = SnakeYamlConfigurationFactory.create(ListConfig.class, ConfigurationOptions.defaults());
ListConfig config = factory.load(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
assertEquals(List.of("value2"), config.list());
var data = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
assertThrows(BadValueException.class, () -> factory.load(data));
}

@Disabled("See prior")
@Test
public void readImplicitNullListElement() throws IOException, InvalidConfigException {
String content = """
Expand All @@ -56,8 +53,8 @@ public void readImplicitNullListElement() throws IOException, InvalidConfigExcep
-\040
""";
var factory = SnakeYamlConfigurationFactory.create(ListConfig.class, ConfigurationOptions.defaults());
ListConfig config = factory.load(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
assertEquals(List.of("value1"), config.list());
var data = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
assertThrows(BadValueException.class, () -> factory.load(data));
}

public interface ListConfig {
Expand Down

0 comments on commit 1409743

Please sign in to comment.