Skip to content

fix(flagd): improve error messages for validation, if there are multiple errors #1250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import java.net.URI;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;

/** flagd feature flag configuration parser. */
Expand Down Expand Up @@ -59,7 +61,11 @@ public static ParsingResult parseString(final String configuration, boolean thro
Set<ValidationMessage> validationMessages = SCHEMA_VALIDATOR.validate(parser.readValueAsTree());

if (!validationMessages.isEmpty()) {
String message = String.format("Invalid flag configuration: %s", validationMessages.toArray());
List<String> distinctMessages = validationMessages.stream()
.map(ValidationMessage::toString)
.distinct()
.collect(Collectors.toList());
String message = String.format("Invalid flag configuration: %s", distinctMessages);
log.warn(message);
if (throwIfInvalid) {
throw new IllegalArgumentException(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class TestUtils {
public static final String INVALID_FLAG_SET_METADATA = "flagConfigurations/invalid-flag-set-metadata.json";
public static final String VALID_FLAG_SET_METADATA = "flagConfigurations/valid-flag-set-metadata.json";
public static final String INVALID_CFG = "flagConfigurations/invalid-configuration.json";
public static final String INVALID_FLAG_MULTIPLE_ERRORS = "flagConfigurations/invalid-flag-multiple-errors.json";
public static final String UPDATABLE_FILE = "flagConfigurations/updatableFlags.json";

public static String getFlagsFromResource(final String file) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
import static dev.openfeature.contrib.providers.flagd.resolver.process.TestUtils.INVALID_CFG;
import static dev.openfeature.contrib.providers.flagd.resolver.process.TestUtils.INVALID_FLAG;
import static dev.openfeature.contrib.providers.flagd.resolver.process.TestUtils.INVALID_FLAG_METADATA;
import static dev.openfeature.contrib.providers.flagd.resolver.process.TestUtils.INVALID_FLAG_MULTIPLE_ERRORS;
import static dev.openfeature.contrib.providers.flagd.resolver.process.TestUtils.INVALID_FLAG_SET_METADATA;
import static dev.openfeature.contrib.providers.flagd.resolver.process.TestUtils.VALID_FLAG_SET_METADATA;
import static dev.openfeature.contrib.providers.flagd.resolver.process.TestUtils.VALID_LONG;
import static dev.openfeature.contrib.providers.flagd.resolver.process.TestUtils.VALID_SIMPLE;
import static dev.openfeature.contrib.providers.flagd.resolver.process.TestUtils.VALID_SIMPLE_EXTRA_FIELD;
import static dev.openfeature.contrib.providers.flagd.resolver.process.TestUtils.getFlagsFromResource;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;
import java.util.Map;
Expand Down Expand Up @@ -135,24 +136,36 @@ void validJsonConfigurationWithFlagMetadataParsing() throws IOException {
@Test
void invalidFlagThrowsError() throws IOException {
String flagString = getFlagsFromResource(INVALID_FLAG);
assertThrows(IllegalArgumentException.class, () -> FlagParser.parseString(flagString, true));
assertThatThrownBy(() -> FlagParser.parseString(flagString, true)).isInstanceOf(IllegalArgumentException.class);
}

@Test
void invalidFlagMetadataThrowsError() throws IOException {
String flagString = getFlagsFromResource(INVALID_FLAG_METADATA);
assertThrows(IllegalArgumentException.class, () -> FlagParser.parseString(flagString, true));
assertThatThrownBy(() -> FlagParser.parseString(flagString, true)).isInstanceOf(IllegalArgumentException.class);
}

@Test
void invalidFlagSetMetadataThrowsError() throws IOException {
String flagString = getFlagsFromResource(INVALID_FLAG_SET_METADATA);
assertThrows(IllegalArgumentException.class, () -> FlagParser.parseString(flagString, true));
assertThatThrownBy(() -> FlagParser.parseString(flagString, true)).isInstanceOf(IllegalArgumentException.class);
}

@Test
void invalidConfigurationsThrowsError() throws IOException {
String flagString = getFlagsFromResource(INVALID_CFG);
assertThrows(IllegalArgumentException.class, () -> FlagParser.parseString(flagString, true));
assertThatThrownBy(() -> FlagParser.parseString(flagString, true)).isInstanceOf(IllegalArgumentException.class);
}

@Test
void invalidWithMulipleErrorsConfigurationsThrowsError() throws IOException {
String flagString = getFlagsFromResource(INVALID_FLAG_MULTIPLE_ERRORS);
assertThatThrownBy(() -> FlagParser.parseString(flagString, true))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("must be valid to one and only one schema")
.hasMessageContaining("$.flags.myBoolFlag: required property 'defaultVariant' not found")
.hasMessageContaining("$.flags.myBoolFlag: required property 'state' not found")
.hasMessageContaining(
"$.flags.myBoolFlag.metadata.invalid: object found, [string, number, boolean] expected");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "../../../main/resources/flagd/schemas/flags.json",
"flags": {
"myBoolFlag": {
"metadata": {
"string": "string",
"boolean": true,
"float": 1.234,
"invalid": {
"a": "a"
}
}
}
}
}
Loading