feat(java): support postgres URI forms for DATABASE_URL#369
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the Java Spring Boot datasource configuration to accept DATABASE_URL in Postgres URI forms (postgresql:// and postgres://) by normalizing them into JDBC URLs, while preserving SPRING_DATASOURCE_URL behavior as a strict override used “as-is”.
Changes:
- Add explicit precedence for URL resolution:
SPRING_DATASOURCE_URL→ normalizedDATABASE_URL→spring.datasource.urlfallback. - Normalize
DATABASE_URLvalues starting withpostgresql://orpostgres://intojdbc:postgresql://.... - Add unit tests for precedence and basic normalization scenarios; update docs/examples to reflect accepted formats.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/java/src/main/java/org/openapitools/config/DataSourceConfig.java |
Adds URL resolution/normalization logic and updates Hikari configuration to use it. |
src/java/src/test/java/org/openapitools/config/DataSourceConfigTest.java |
Expands unit tests to cover precedence and normalization behavior. |
src/java/src/main/resources/application.properties |
Documents accepted DATABASE_URL formats and precedence. |
src/java/README.md |
Updates setup examples to show postgresql://... and precedence notes. |
| if (url.startsWith("postgresql://")) { | ||
| return "jdbc:" + url; | ||
| } | ||
| if (url.startsWith("postgres://")) { | ||
| return "jdbc:postgresql://" + url.substring("postgres://".length()); |
There was a problem hiding this comment.
normalizeDatabaseUrl() currently only rewrites the URI scheme by prefixing/replacing strings. If DATABASE_URL is a full Postgres URI with userinfo (e.g. postgresql://user:pass@host:5432/db?sslmode=require), the resulting JDBC URL (jdbc:postgresql://user:pass@host:5432/db...) is not a valid PostgreSQL JDBC URL and will fail to connect. Consider parsing the URI (e.g., via java.net.URI) and constructing a proper jdbc:postgresql://host:port/db URL, mapping user/password into either Hikari username/password or query parameters, and preserving query params safely (including URL-decoding/encoding as needed).
There was a problem hiding this comment.
Thanks for the review. This PR intentionally scopes compatibility to scheme normalization only (postgresql:// and postgres:// -> jdbc:postgresql://) for DATABASE_URL, while leaving SPRING_DATASOURCE_URL behavior unchanged. Handling full URI parsing (userinfo/query remapping) is a valid enhancement, but it is out of scope for this change and we can track it as a follow-up.
| void hikariConfig_ShouldNormalizeDatabaseUrlPostgresqlScheme() throws Exception { | ||
| DataSourceConfig config = new DataSourceConfig(); | ||
|
|
||
| setField(config, "springDatasourceUrl", ""); | ||
| setField(config, "databaseUrl", "postgresql://localhost:5432/lamp"); | ||
| setField(config, "fallbackJdbcUrl", "jdbc:postgresql://localhost:5432/fallback"); | ||
| setField(config, "username", "user"); | ||
| setField(config, "password", "pass"); | ||
| setField(config, "driverClassName", "org.postgresql.Driver"); | ||
|
|
||
| HikariConfig result = config.hikariConfig(); | ||
|
|
||
| assertThat(result.getJdbcUrl()).isEqualTo("jdbc:postgresql://localhost:5432/lamp"); |
There was a problem hiding this comment.
The new normalization tests cover scheme-only rewrites but don’t cover the most common DATABASE_URL form that includes credentials and/or query params (e.g. postgres://user:pass@host:5432/db?sslmode=disable). Adding unit tests for these cases will prevent regressions once normalizeDatabaseUrl() is updated to produce valid JDBC URLs for full Postgres URIs.
There was a problem hiding this comment.
Agreed this would be useful once we broaden parsing behavior. For this PR we intentionally kept scope to scheme compatibility only, so tests cover that explicit contract (including precedence and passthrough behavior). I’m leaving full credential/query-param URI parsing and corresponding tests for a follow-up change.
* feat(benchmarks): add cloud run single-instance k6 benchmark harness * feat(benchmarks): add cloud run service mapping and mode switch hooks * docs(benchmarks): document db seeding and gcp runner setup * docs: add postgres startup variable matrix by implementation * docs: refresh postgres startup variables after syncing main * feat(csharp): support DATABASE_URL fallback for connection string (#367) * feat(csharp): add DATABASE_URL fallback for postgres connection * test(csharp): fix file provider in connection resolution tests * chore(csharp): address PR review feedback * feat(java): support postgres URI forms for DATABASE_URL (#369) * docs(go): clarify DATABASE_URL accepts postgres and postgresql schemes (#368) * docs(go): document DATABASE_URL support for postgresql scheme * docs(go): address PR review comments on DB URL schemes * refactor(benchmarks): address PR review comments * docs(benchmarks): align pass setup with database-url mode switching * docs: rewrite postgres setup guide for end users * feat(benchmarks): resolve cloud run project from config by default * feat(benchmarks): support skip-setup and config-based project env * fix(benchmarks): retry precheck and continue on iteration failures * feat(benchmarks): add fast benchmark config profile * chore(benchmarks): enforce Cloud Run startup probe settings * chore(benchmarks): set Cloud Run min instances to 0 * docs(benchmarks): add macOS caffeinate run commands * feat(benchmarks): add cold-start appendix measurement * fix(benchmarks): avoid content-type on bodyless requests (#370)
Summary
Testing