From 14097436feb7acaec2fa230daf2ad8d773f92ff7 Mon Sep 17 00:00:00 2001 From: A248 Date: Sat, 4 Dec 2021 10:29:45 -0500 Subject: [PATCH] Disallow null values in collection elements - Fixes #23 --- .../arim/dazzleconf/internal/NestedMapHelper.java | 2 +- .../arim/dazzleconf/internal/error/UserError.java | 6 ++++++ .../internal/processor/FlexibleTypeImpl.java | 15 ++++++++++----- .../ext/snakeyaml/NullListElementTest.java | 15 ++++++--------- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/space/arim/dazzleconf/internal/NestedMapHelper.java b/core/src/main/java/space/arim/dazzleconf/internal/NestedMapHelper.java index 7eaec1e..2cd54e3 100644 --- a/core/src/main/java/space/arim/dazzleconf/internal/NestedMapHelper.java +++ b/core/src/main/java/space/arim/dazzleconf/internal/NestedMapHelper.java @@ -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)); diff --git a/core/src/main/java/space/arim/dazzleconf/internal/error/UserError.java b/core/src/main/java/space/arim/dazzleconf/internal/error/UserError.java index 2649311..58b6f2b 100644 --- a/core/src/main/java/space/arim/dazzleconf/internal/error/UserError.java +++ b/core/src/main/java/space/arim/dazzleconf/internal/error/UserError.java @@ -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; diff --git a/core/src/main/java/space/arim/dazzleconf/internal/processor/FlexibleTypeImpl.java b/core/src/main/java/space/arim/dazzleconf/internal/processor/FlexibleTypeImpl.java index de900ea..98fef8f 100644 --- a/core/src/main/java/space/arim/dazzleconf/internal/processor/FlexibleTypeImpl.java +++ b/core/src/main/java/space/arim/dazzleconf/internal/processor/FlexibleTypeImpl.java @@ -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 @@ -260,7 +260,12 @@ public Map getMap(FlexibleTypeMapEntryFunction factory.load(data)); } - @Disabled("See prior") @Test public void readImplicitNullListElement() throws IOException, InvalidConfigException { String content = """ @@ -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 {