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
9 changes: 8 additions & 1 deletion samples/kotlin/src/main/kotlin/com/grid/sample/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ object Config {
val apiClientSecret: String = getEnvVar("GRID_CLIENT_SECRET")
val webhookPublicKey: String = getEnvVar("GRID_WEBHOOK_PUBKEY").replace("\\n", "\n")

// Optional override for the Grid API base URL (e.g. a dev/RC environment).
// When unset, the SDK uses its default (production) base URL.
val apiBaseUrl: String? = getEnvVarOrNull("GRID_API_BASE_URL")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Empty string not treated as "unset"

getEnvVarOrNull returns an empty string if GRID_API_BASE_URL is set to "" (e.g. an accidentally blank .env entry). That empty string is non-null, so apiBaseUrl will be "" and baseUrl("") will be called on the SDK builder — almost certainly producing a malformed URL at runtime instead of silently falling back to the default.

Suggested change
val apiBaseUrl: String? = getEnvVarOrNull("GRID_API_BASE_URL")
val apiBaseUrl: String? = getEnvVarOrNull("GRID_API_BASE_URL")?.takeIf { it.isNotBlank() }
Prompt To Fix With AI
This is a comment left during a code review.
Path: samples/kotlin/src/main/kotlin/com/grid/sample/Config.kt
Line: 18

Comment:
**Empty string not treated as "unset"**

`getEnvVarOrNull` returns an empty string if `GRID_API_BASE_URL` is set to `""` (e.g. an accidentally blank `.env` entry). That empty string is non-null, so `apiBaseUrl` will be `""` and `baseUrl("")` will be called on the SDK builder — almost certainly producing a malformed URL at runtime instead of silently falling back to the default.

```suggestion
    val apiBaseUrl: String? = getEnvVarOrNull("GRID_API_BASE_URL")?.takeIf { it.isNotBlank() }
```

How can I resolve this? If you propose a fix, please make it concise.


private fun getEnvVar(key: String): String =
getEnvVarOrNull(key)
?: throw IllegalStateException("$key environment variable not set")

private fun getEnvVarOrNull(key: String): String? =
System.getProperty(key)
?: dotenv[key]
?: System.getenv(key)
?: throw IllegalStateException("$key environment variable not set")
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ object GridClientBuilder {
LightsparkGridOkHttpClient.builder()
.username(Config.apiTokenId)
.password(Config.apiClientSecret)
.apply { Config.apiBaseUrl?.let { baseUrl(it) } }
.build()
}
}
Loading