Skip to content

Improve environment component null values handling #9188

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
merged 1 commit into from
Jul 17, 2025
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 @@ -16,7 +16,8 @@ private EnvironmentVariables() {}
* Gets an environment variable value.
*
* @param name The environment variable name.
* @return The environment variable value, {@code null} if missing or can't be retrieved.
* @return The environment variable value, {@code null} if missing, can't be retrieved, or the
* environment variable name is {@code null}.
*/
public static @Nullable String get(String name) {
return getOrDefault(name, null);
Expand All @@ -28,9 +29,13 @@ private EnvironmentVariables() {}
* @param name The environment variable name.
* @param defaultValue The default value to return if the environment variable is missing or can't
* be retrieved.
* @return The environment variable value, {@code defaultValue} if missing or can't be retrieved.
* @return The environment variable value, {@code defaultValue} if missing, can't be retrieved or
* the environment variable name is {@code null}.
*/
public static String getOrDefault(@Nonnull String name, String defaultValue) {
if (name == null) {
return defaultValue;
}
try {
String value = System.getenv(name);
return value == null ? defaultValue : value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package datadog.environment;

import javax.annotation.Nonnull;

/**
* Safely queries system properties against security manager.
*
Expand All @@ -13,7 +15,8 @@ private SystemProperties() {}
* Gets a system property value.
*
* @param property The system property name.
* @return The system property value, {@code null} if missing or can't be retrieved.
* @return The system property value, {@code null} if missing, can't be retrieved, or the system
* property name is {@code null}.
*/
public static String get(String property) {
return getOrDefault(property, null);
Expand All @@ -25,9 +28,13 @@ public static String get(String property) {
* @param property The system property name.
* @param defaultValue The default value to return if the system property is missing or can't be
* retrieved.
* @return The system property value, {@code defaultValue} if missing or can't be retrieved.
* @return The system property value, {@code defaultValue} if missing, can't be retrieved, or the
* system property name is {@code null}.
*/
public static String getOrDefault(String property, String defaultValue) {
public static String getOrDefault(@Nonnull String property, String defaultValue) {
if (property == null) {
return defaultValue;
}
try {
return System.getProperty(property, defaultValue);
} catch (SecurityException ignored) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
package datadog.environment;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.Test;

class EnvironmentVariablesTest {
private static final String EXISTING_ENV_VAR = "JAVA_8_HOME";
private static final String MISSING_ENV_VAR = "UNDEFINED_ENV_VAR";
private static final String DEFAULT_VALUE = "DEFAULT";

@Test
void testGet() {
// Existing environment variable
assertNotNull(EnvironmentVariables.get(EXISTING_ENV_VAR));
// Missing environment variable
assertNull(EnvironmentVariables.get(MISSING_ENV_VAR));
assertThrows(NullPointerException.class, () -> EnvironmentVariables.get(null));
// Null values
assertDoesNotThrow(() -> EnvironmentVariables.get(null));
assertNull(EnvironmentVariables.get(null));
}

@Test
void testGetOrDefault() {
// Existing environment variable
assertNotNull(EnvironmentVariables.getOrDefault(EXISTING_ENV_VAR, null));

assertEquals("", EnvironmentVariables.getOrDefault(MISSING_ENV_VAR, ""));
// Missing environment variable
assertEquals(DEFAULT_VALUE, EnvironmentVariables.getOrDefault(MISSING_ENV_VAR, DEFAULT_VALUE));
assertNull(EnvironmentVariables.getOrDefault(MISSING_ENV_VAR, null));
// Null values
assertDoesNotThrow(() -> EnvironmentVariables.getOrDefault(null, DEFAULT_VALUE));
assertEquals(DEFAULT_VALUE, EnvironmentVariables.getOrDefault(null, DEFAULT_VALUE));
assertDoesNotThrow(() -> EnvironmentVariables.getOrDefault(MISSING_ENV_VAR, null));
assertNull(EnvironmentVariables.getOrDefault(MISSING_ENV_VAR, null));

assertThrows(NullPointerException.class, () -> EnvironmentVariables.getOrDefault(null, ""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,32 @@
class SystemPropertiesTest {
private static final String EXISTING_SYSTEM_PROPERTY = "java.home";
private static final String MISSING_SYSTEM_PROPERTY = "undefined.system.property";
private static final String DEFAULT_VALUE = "DEFAULT";

@Test
void testGet() {
// Existing system properties
assertNotNull(SystemProperties.get(EXISTING_SYSTEM_PROPERTY));
// Missing system properties
assertNull(SystemProperties.get(MISSING_SYSTEM_PROPERTY));
assertThrows(NullPointerException.class, () -> SystemProperties.get(null));
// Null values
assertDoesNotThrow(() -> SystemProperties.get(null));
assertNull(SystemProperties.get(null));
}

@Test
void testGetOrDefault() {
// Existing system properties
assertNotNull(SystemProperties.getOrDefault(EXISTING_SYSTEM_PROPERTY, null));

assertEquals("", SystemProperties.getOrDefault(MISSING_SYSTEM_PROPERTY, ""));
// Missing system properties
assertEquals(
DEFAULT_VALUE, SystemProperties.getOrDefault(MISSING_SYSTEM_PROPERTY, DEFAULT_VALUE));
assertNull(SystemProperties.getOrDefault(MISSING_SYSTEM_PROPERTY, null));
// Null values
assertDoesNotThrow(() -> SystemProperties.getOrDefault(null, DEFAULT_VALUE));
assertEquals(DEFAULT_VALUE, SystemProperties.getOrDefault(null, DEFAULT_VALUE));
assertDoesNotThrow(() -> SystemProperties.getOrDefault(MISSING_SYSTEM_PROPERTY, null));
assertNull(SystemProperties.getOrDefault(MISSING_SYSTEM_PROPERTY, null));

assertThrows(NullPointerException.class, () -> SystemProperties.getOrDefault(null, ""));
}

@Test
Expand Down
Loading