Parse SslMode from connection string for MySQL#4488
Conversation
Add logic to extract and set the SslMode property from the calculated connection string if present. Only assign Database.ConnectionString if the calculated string is empty. This improves MySQL connection configuration flexibility.
WalkthroughMySQL connection handling in the Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Review rate limit: 0/1 reviews remaining, refill in 60 minutes.Comment |
Up to standards ✅🟢 Issues
|
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@Ginger/GingerCoreNET/Database/DatabaseOperations.cs`:
- Around line 492-519: The Connect() path currently parses SslMode from
ConnectionStringCalculated but never updates Database.ConnectionString when
ConnectionStringCalculated is non-empty, leaving Database.ConnectionString out
of sync; after you finish parsing/applying values from
ConnectionStringCalculated (i.e. after the loop where you handle SslMode in
DatabaseOperations.Connect or equivalent), set Database.ConnectionString =
my.ConnectionString so Database.ConnectionString always reflects the effective
connection string used by the MySql connection (ensure this is done both when
ConnectionStringCalculated is empty and when it is non-empty so
GetConnectionString()/Connect() invariants hold).
- Around line 505-509: The parsing of SslMode using Enum.TryParse silently
ignores invalid values so a typo downgrades the config; update the block around
Enum.TryParse(value, ignoreCase: true, out MySqlSslMode sslMode) (the code that
assigns my.SslMode) to fail fast: if TryParse returns false, throw an
informative exception (e.g., ArgumentException or FormatException) that includes
the invalid value and expected MySqlSslMode enum names; otherwise assign
my.SslMode as before. This ensures misconfigured SslMode is surfaced
immediately.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: fb62419b-b83a-4884-ac58-09c2464a12ed
📒 Files selected for processing (1)
Ginger/GingerCoreNET/Database/DatabaseOperations.cs
| if (!string.IsNullOrEmpty(ConnectionStringCalculated)) | ||
| { | ||
| foreach (string segment in ConnectionStringCalculated.Split(';', StringSplitOptions.RemoveEmptyEntries)) | ||
| { | ||
| int eq = segment.IndexOf('='); | ||
| if (eq <= 0) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| string key = segment.Substring(0, eq).Trim(); | ||
| if (key.Equals("SslMode", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| string value = segment.Substring(eq + 1).Trim(); | ||
| if (Enum.TryParse(value, ignoreCase: true, out MySqlSslMode sslMode)) | ||
| { | ||
| my.SslMode = sslMode; | ||
| } | ||
|
|
||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (string.IsNullOrEmpty(ConnectionStringCalculated)) | ||
| { | ||
| Database.ConnectionString = my.ConnectionString; | ||
| } |
There was a problem hiding this comment.
Keep Database.ConnectionString aligned with the effective MySQL connection.
This now leaves Database.ConnectionString untouched whenever ConnectionStringCalculated is non-empty. For inputs like SslMode=Required, Connect() succeeds with my.ConnectionString, but ConnectionStringCalculated/GetConnectionString() still resolve to that partial fragment afterward instead of the real connection string. That is a regression from the previous invariant and can break callers that inspect the effective value after Connect().
Suggested fix
- if (!string.IsNullOrEmpty(ConnectionStringCalculated))
+ string calculatedConnectionString = ConnectionStringCalculated;
+ if (!string.IsNullOrEmpty(calculatedConnectionString))
{
- foreach (string segment in ConnectionStringCalculated.Split(';', StringSplitOptions.RemoveEmptyEntries))
+ foreach (string segment in calculatedConnectionString.Split(';', StringSplitOptions.RemoveEmptyEntries))
{
int eq = segment.IndexOf('=');
if (eq <= 0)
{
continue;
@@
}
}
}
-
- if (string.IsNullOrEmpty(ConnectionStringCalculated))
- {
- Database.ConnectionString = my.ConnectionString;
- }
+ Database.ConnectionString = my.ConnectionString;📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if (!string.IsNullOrEmpty(ConnectionStringCalculated)) | |
| { | |
| foreach (string segment in ConnectionStringCalculated.Split(';', StringSplitOptions.RemoveEmptyEntries)) | |
| { | |
| int eq = segment.IndexOf('='); | |
| if (eq <= 0) | |
| { | |
| continue; | |
| } | |
| string key = segment.Substring(0, eq).Trim(); | |
| if (key.Equals("SslMode", StringComparison.OrdinalIgnoreCase)) | |
| { | |
| string value = segment.Substring(eq + 1).Trim(); | |
| if (Enum.TryParse(value, ignoreCase: true, out MySqlSslMode sslMode)) | |
| { | |
| my.SslMode = sslMode; | |
| } | |
| break; | |
| } | |
| } | |
| } | |
| if (string.IsNullOrEmpty(ConnectionStringCalculated)) | |
| { | |
| Database.ConnectionString = my.ConnectionString; | |
| } | |
| string calculatedConnectionString = ConnectionStringCalculated; | |
| if (!string.IsNullOrEmpty(calculatedConnectionString)) | |
| { | |
| foreach (string segment in calculatedConnectionString.Split(';', StringSplitOptions.RemoveEmptyEntries)) | |
| { | |
| int eq = segment.IndexOf('='); | |
| if (eq <= 0) | |
| { | |
| continue; | |
| } | |
| string key = segment.Substring(0, eq).Trim(); | |
| if (key.Equals("SslMode", StringComparison.OrdinalIgnoreCase)) | |
| { | |
| string value = segment.Substring(eq + 1).Trim(); | |
| if (Enum.TryParse(value, ignoreCase: true, out MySqlSslMode sslMode)) | |
| { | |
| my.SslMode = sslMode; | |
| } | |
| break; | |
| } | |
| } | |
| } | |
| Database.ConnectionString = my.ConnectionString; |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@Ginger/GingerCoreNET/Database/DatabaseOperations.cs` around lines 492 - 519,
The Connect() path currently parses SslMode from ConnectionStringCalculated but
never updates Database.ConnectionString when ConnectionStringCalculated is
non-empty, leaving Database.ConnectionString out of sync; after you finish
parsing/applying values from ConnectionStringCalculated (i.e. after the loop
where you handle SslMode in DatabaseOperations.Connect or equivalent), set
Database.ConnectionString = my.ConnectionString so Database.ConnectionString
always reflects the effective connection string used by the MySql connection
(ensure this is done both when ConnectionStringCalculated is empty and when it
is non-empty so GetConnectionString()/Connect() invariants hold).
| string value = segment.Substring(eq + 1).Trim(); | ||
| if (Enum.TryParse(value, ignoreCase: true, out MySqlSslMode sslMode)) | ||
| { | ||
| my.SslMode = sslMode; | ||
| } |
There was a problem hiding this comment.
Fail fast on invalid SslMode values.
If Enum.TryParse fails here, the code silently keeps the builder default and still opens the connection. A typo in SslMode then becomes a hidden config downgrade instead of an actionable error.
Suggested fix
if (key.Equals("SslMode", StringComparison.OrdinalIgnoreCase))
{
string value = segment.Substring(eq + 1).Trim();
- if (Enum.TryParse(value, ignoreCase: true, out MySqlSslMode sslMode))
- {
- my.SslMode = sslMode;
- }
+ if (!Enum.TryParse(value, ignoreCase: true, out MySqlSslMode sslMode))
+ {
+ throw new ArgumentException($"Invalid MySQL SslMode '{value}'.");
+ }
+
+ my.SslMode = sslMode;
break;
}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@Ginger/GingerCoreNET/Database/DatabaseOperations.cs` around lines 505 - 509,
The parsing of SslMode using Enum.TryParse silently ignores invalid values so a
typo downgrades the config; update the block around Enum.TryParse(value,
ignoreCase: true, out MySqlSslMode sslMode) (the code that assigns my.SslMode)
to fail fast: if TryParse returns false, throw an informative exception (e.g.,
ArgumentException or FormatException) that includes the invalid value and
expected MySqlSslMode enum names; otherwise assign my.SslMode as before. This
ensures misconfigured SslMode is surfaced immediately.
Add logic to extract and set the SslMode property from the calculated connection string if present. Only assign Database.ConnectionString if the calculated string is empty. This improves MySQL connection configuration flexibility.
Description
Type of Change
Checklist
[IsSerializedForLocalRepository]where neededReporter.ToLog()patternSummary by CodeRabbit