Skip to content
Open
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 NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### New Features and Improvements

### Bug Fixes
* `UserAgent.env()` no longer throws `NullPointerException` when `PATH` variable is not found

### Security Vulnerabilities

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,21 @@ public String getValue() {

private static final ArrayList<Info> otherInfo = new ArrayList<>();

// TODO: check if reading from
// /META-INF/maven/com.databricks/databrics-sdk-java/pom.properties
// or getClass().getPackage().getImplementationVersion() is enough.
private static final String version = "0.79.0";
// Try to get version from package metadata or fallback to hardcoded value
private static final String version;
static {
String v = null;
try {
Package pkg = UserAgent.class.getPackage();
if (pkg != null) {
v = pkg.getImplementationVersion();
}
} catch (Exception ignored) {}
if (v == null) {
v = "0.79.0";
}
version = v;
}

public static void withProduct(String product, String productVersion) {
UserAgent.product = product;
Expand Down Expand Up @@ -233,11 +244,16 @@ private static String cicdProvider() {

private static Environment env() {
if (env == null) {
env =
new Environment(
System.getenv(),
System.getenv("PATH").split(File.pathSeparator),
System.getProperty("os.name"));
Map<String, String> sysEnv = System.getenv();
if (sysEnv == null) {
sysEnv = Collections.emptyMap();
}
String pathVar = sysEnv.get("PATH");
String[] pathArr = pathVar != null ? pathVar.split(File.pathSeparator) : new String[0];
env = new Environment(
sysEnv,
pathArr,
System.getProperty("os.name"));
}
return env;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,13 @@ public void testUserAgentCicdTwoProvider() {
Assertions.assertTrue(UserAgent.asString().contains("cicd/gitlab"));
UserAgent.env = null;
}

@Test
public void testEnvNoPath() {
// Fix NullPointerException when System.getenv() returns null or missing PATH property
UserAgent.env = null;
Environment environment = UserAgent.env();
Assertions.assertNotNull(environment);
UserAgent.env = null;
}
}