Skip to content

Commit

Permalink
Revert "Add custom enum deserializer to improve error messaging, impr…
Browse files Browse the repository at this point in the history
…ove byte count error mesages (opensearch-project#5076)"

This reverts commit e9bffee.
  • Loading branch information
graytaylor0 committed Oct 25, 2024
1 parent c29f002 commit 0d03ff8
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 355 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,11 @@ public static ByteCount parse(final String string) {
final String unitString = matcher.group("unit");

if(unitString == null) {
throw new ByteCountInvalidInputException("Byte counts must have a unit. Valid byte units include: " +
Arrays.stream(Unit.values()).map(unitValue -> unitValue.unitString).collect(Collectors.toList()));
throw new ByteCountInvalidInputException("Byte counts must have a unit.");
}

final Unit unit = Unit.fromString(unitString)
.orElseThrow(() -> new ByteCountInvalidInputException("Invalid byte unit: '" + unitString + "'. Valid byte units include: "
+ Arrays.stream(Unit.values()).map(unitValue -> unitValue.unitString).collect(Collectors.toList())));
.orElseThrow(() -> new ByteCountInvalidInputException("Invalid byte unit: '" + unitString + "'"));

final BigDecimal valueBigDecimal = new BigDecimal(valueString);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ByteCount deserialize(final JsonParser parser, final DeserializationConte
try {
return ByteCount.parse(byteString);
} catch (final Exception ex) {
throw new IllegalArgumentException(ex.getMessage());
throw new IllegalArgumentException(ex);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.junit.jupiter.params.provider.ValueSource;
import org.opensearch.dataprepper.model.types.ByteCount;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -32,28 +31,9 @@ void setUp() {
}

@ParameterizedTest
@ValueSource(strings = {"1", "10"})
void convert_with_no_byte_unit_throws_expected_exception(final String invalidByteString) {
final IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> objectMapper.convertValue(invalidByteString, ByteCount.class));
assertThat(exception.getMessage(), containsString("Byte counts must have a unit. Valid byte units include: [b, kb, mb, gb]"));
}

@ParameterizedTest
@ValueSource(strings = {"10 2b", "bad"})
void convert_with_non_parseable_values_throws(final String invalidByteString) {
final IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> objectMapper.convertValue(invalidByteString, ByteCount.class));
assertThat(exception.getMessage(), containsString("Unable to parse bytes"));
}

@ParameterizedTest
@CsvSource({
"10f, f",
"1vb, vb",
"3g, g"
})
void convert_with_invalid_byte_units_throws(final String invalidByteString, final String invalidUnit) {
final IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> objectMapper.convertValue(invalidByteString, ByteCount.class));
assertThat(exception.getMessage(), containsString("Invalid byte unit: '" + invalidUnit + "'. Valid byte units include: [b, kb, mb, gb]"));
@ValueSource(strings = {"1", "1b 2b", "1vb", "bad"})
void convert_with_invalid_values_throws(final String invalidByteString) {
assertThrows(IllegalArgumentException.class, () -> objectMapper.convertValue(invalidByteString, ByteCount.class));
}

@ParameterizedTest
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.opensearch.dataprepper.model.types.ByteCount;
import org.opensearch.dataprepper.pipeline.parser.ByteCountDeserializer;
import org.opensearch.dataprepper.pipeline.parser.DataPrepperDurationDeserializer;
import org.opensearch.dataprepper.pipeline.parser.EnumDeserializer;
import org.opensearch.dataprepper.pipeline.parser.EventKeyDeserializer;
import org.springframework.context.annotation.Bean;

Expand All @@ -34,7 +33,6 @@ public class ObjectMapperConfiguration {
ObjectMapper extensionPluginConfigObjectMapper() {
final SimpleModule simpleModule = new SimpleModule();
simpleModule.addDeserializer(Duration.class, new DataPrepperDurationDeserializer());
simpleModule.addDeserializer(Enum.class, new EnumDeserializer());
simpleModule.addDeserializer(ByteCount.class, new ByteCountDeserializer());

return new ObjectMapper()
Expand All @@ -49,7 +47,6 @@ ObjectMapper pluginConfigObjectMapper(
final SimpleModule simpleModule = new SimpleModule();
simpleModule.addDeserializer(Duration.class, new DataPrepperDurationDeserializer());
simpleModule.addDeserializer(ByteCount.class, new ByteCountDeserializer());
simpleModule.addDeserializer(Enum.class, new EnumDeserializer());
simpleModule.addDeserializer(EventKey.class, new EventKeyDeserializer(eventKeyFactory));
TRANSLATE_VALUE_SUPPORTED_JAVA_TYPES.stream().forEach(clazz -> simpleModule.addDeserializer(
clazz, new DataPrepperScalarTypeDeserializer<>(variableExpander, clazz)));
Expand Down
Loading

0 comments on commit 0d03ff8

Please sign in to comment.