Compare commits

...

1 Commits

Author SHA1 Message Date
Bogdan Carpusor 8d46d9316a fix: Parse string environment variables into numbers 2025-08-22 15:26:21 +03:00
3 changed files with 1677 additions and 1625 deletions

File diff suppressed because it is too large Load Diff

View File

@ -98,17 +98,29 @@ public class ConfigMapper {
if (targetType == String.class) {
return value.getAsString();
} else if (targetType == Integer.class || targetType == int.class) {
if (value.getAsDouble() == (double) value.getAsInt()) {
if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString()) {
return Integer.parseInt(value.getAsString());
} else if (value.getAsDouble() == (double) value.getAsInt()) {
return value.getAsInt();
}
} else if (targetType == Long.class || targetType == long.class) {
if (value.getAsDouble() == (double) value.getAsLong()) {
if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString()) {
return Long.parseLong(value.getAsString());
} else if (value.getAsDouble() == (double) value.getAsLong()) {
return value.getAsLong();
}
} else if (targetType == Double.class || targetType == double.class) {
return value.getAsDouble();
if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString()) {
return Double.parseDouble(value.getAsString());
} else {
return value.getAsDouble();
}
} else if (targetType == Float.class || targetType == float.class) {
return value.getAsFloat();
if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString()) {
return Float.parseFloat(value.getAsString());
} else {
return value.getAsFloat();
}
} else if (targetType == Boolean.class || targetType == boolean.class) {
// Handle boolean conversion from strings like "true", "false"
return handleBooleanConversion(value, fieldName);

View File

@ -292,4 +292,37 @@ public class ConfigMapperTest {
}
}
}
@Test
public void testNumericStringParsing() throws Exception {
// Test parsing of numeric strings from environment variables
// integer from string
{
JsonObject config = new JsonObject();
config.addProperty("int_property", "12345");
assertEquals(12345, ConfigMapper.mapConfig(config, DummyConfig.class).int_property);
}
// long from string
{
JsonObject config = new JsonObject();
config.addProperty("long_property", "9876543210");
assertEquals(9876543210L, ConfigMapper.mapConfig(config, DummyConfig.class).long_property);
}
// double from string
{
JsonObject config = new JsonObject();
config.addProperty("double_property", "123.456");
assertEquals(123.456, ConfigMapper.mapConfig(config, DummyConfig.class).double_property, 0.001);
}
// float from string
{
JsonObject config = new JsonObject();
config.addProperty("float_property", "98.76");
assertEquals(98.76f, ConfigMapper.mapConfig(config, DummyConfig.class).float_property, 0.001);
}
}
}