Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [9.4.1]

- Fixes env var reading with specific types
- Updates dependencies for testing

## [9.4.0]
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ tasks.register('generateMetaInf') {
build.dependsOn(generateMetaInf)

test {
jvmArgs '-Djava.security.egd=file:/dev/urandom'
jvmArgs '-Djava.security.egd=file:/dev/urandom',
'--add-opens=java.base/java.util=ALL-UNNAMED'
minHeapSize = "128m" // initial heap size
maxHeapSize = "1024m" // maximum heap size
testLogging {
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/io/supertokens/storage/postgresql/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -863,18 +863,24 @@ public void updateConfigJsonFromEnv(JsonObject configJson) {
.replace("\\\\", "\\");
}

// Empty/unset stringValue is already handled above (null/empty check),
// so boxed types (Integer, Long, etc.) can safely share branches
// with their primitive counterparts.
if (field.getType().equals(String.class)) {
configJson.addProperty(field.getName(), stringValue);
} else if (field.getType().equals(int.class)) {
} else if (field.getType().equals(int.class) || field.getType().equals(Integer.class)) {
configJson.addProperty(field.getName(), Integer.parseInt(stringValue));
} else if (field.getType().equals(long.class)) {
} else if (field.getType().equals(long.class) || field.getType().equals(Long.class)) {
configJson.addProperty(field.getName(), Long.parseLong(stringValue));
} else if (field.getType().equals(boolean.class)) {
} else if (field.getType().equals(boolean.class) || field.getType().equals(Boolean.class)) {
configJson.addProperty(field.getName(), Boolean.parseBoolean(stringValue));
} else if (field.getType().equals(float.class)) {
} else if (field.getType().equals(float.class) || field.getType().equals(Float.class)) {
configJson.addProperty(field.getName(), Float.parseFloat(stringValue));
} else if (field.getType().equals(double.class)) {
} else if (field.getType().equals(double.class) || field.getType().equals(Double.class)) {
configJson.addProperty(field.getName(), Double.parseDouble(stringValue));
} else {
throw new RuntimeException(
"Unknown field type " + field.getType().getName() + " for config field " + field.getName());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2025, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package io.supertokens.storage.postgresql.test;

import com.google.gson.JsonObject;
import io.supertokens.storage.postgresql.Start;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.Map;

import static org.junit.Assert.*;

public class EnvConfigTest {

private static void setEnv(String key, String value) {
try {
Map<String, String> env = System.getenv();
Class<?> cl = env.getClass();
Field field = cl.getDeclaredField("m");
field.setAccessible(true);
@SuppressWarnings("unchecked")
Map<String, String> writableEnv = (Map<String, String>) field.get(env);
writableEnv.put(key, value);
} catch (Exception e) {
throw new IllegalStateException("Failed to set environment variable", e);
}
}
Comment on lines +30 to +42

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test relies on accessing an internal field named "m" in the System.getenv() implementation. This field name is specific to certain JVM implementations and is not guaranteed across all Java versions or implementations. The test will fail at runtime on JVMs where the environment map implementation uses a different field name.

// This assumes the internal field is named "m"
Field field = cl.getDeclaredField("m");

Consider using a more portable approach for testing environment variable handling, such as:

  • Using a dependency injection pattern to inject the config source
  • Using system properties instead of environment variables for testing
  • Using a library like System Rules or System Lambda that provides safer environment variable mocking
Suggested change
private static void setEnv(String key, String value) {
try {
Map<String, String> env = System.getenv();
Class<?> cl = env.getClass();
Field field = cl.getDeclaredField("m");
field.setAccessible(true);
@SuppressWarnings("unchecked")
Map<String, String> writableEnv = (Map<String, String>) field.get(env);
writableEnv.put(key, value);
} catch (Exception e) {
throw new IllegalStateException("Failed to set environment variable", e);
}
}
private static void setEnv(String key, String value) {
// Use system properties instead of environment variables for testing
// This is more portable and doesn't rely on JVM implementation details
System.setProperty(key, value);
}

Spotted by Graphite

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.


private static void removeEnv(String key) {
try {
Map<String, String> env = System.getenv();
Class<?> cl = env.getClass();
Field field = cl.getDeclaredField("m");
field.setAccessible(true);
@SuppressWarnings("unchecked")
Map<String, String> writableEnv = (Map<String, String>) field.get(env);
writableEnv.remove(key);
} catch (Exception e) {
throw new IllegalStateException("Failed to remove environment variable", e);
}
}

@Test
public void testUpdateConfigJsonFromEnvHandlesBoxedTypes() {
// Regression test: Integer fields like postgresql_minimum_idle_connections
// were silently dropped because updateConfigJsonFromEnv only checked
// primitive types (int.class) not boxed types (Integer.class).
Start start = new Start();
try {
setEnv("POSTGRESQL_MINIMUM_IDLE_CONNECTIONS", "5");

JsonObject configJson = new JsonObject();
start.updateConfigJsonFromEnv(configJson);

assertTrue("POSTGRESQL_MINIMUM_IDLE_CONNECTIONS env var should be loaded into configJson",
configJson.has("postgresql_minimum_idle_connections"));
assertEquals(5, configJson.get("postgresql_minimum_idle_connections").getAsInt());
} finally {
removeEnv("POSTGRESQL_MINIMUM_IDLE_CONNECTIONS");
}
}

@Test
public void testUpdateConfigJsonFromEnvHandlesBoxedTypeWithZero() {
// Tests that Integer value of 0 is correctly loaded (not confused with null/unset)
Start start = new Start();
try {
setEnv("POSTGRESQL_MINIMUM_IDLE_CONNECTIONS", "0");

JsonObject configJson = new JsonObject();
start.updateConfigJsonFromEnv(configJson);

assertTrue("POSTGRESQL_MINIMUM_IDLE_CONNECTIONS env var with value 0 should be loaded",
configJson.has("postgresql_minimum_idle_connections"));
assertEquals(0, configJson.get("postgresql_minimum_idle_connections").getAsInt());
} finally {
removeEnv("POSTGRESQL_MINIMUM_IDLE_CONNECTIONS");
}
}
}
Loading