-
Notifications
You must be signed in to change notification settings - Fork 15
Fix type assertion safety issues to prevent runtime panics #845
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: mawasile <50197777+mawasile@users.noreply.github.com>
Co-authored-by: mawasile <50197777+mawasile@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds safety checks for type assertions across multiple services to prevent runtime panics when API responses have unexpected structures.
- Updated
applyCorrections
to return errors on assertion failures and handled those errors in resource CRUD methods. - Added existence and type checks for map-based API responses in connection, data record, and solution checker services.
- Improved type switch usage by capturing values directly and handling unexpected cases.
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
File | Description |
---|---|
internal/services/tenant_settings/resource_tenant_settings.go | Updated Create/Read/Update/Delete methods to handle errors from applyCorrections |
internal/services/tenant_settings/api_tenant_settings.go | Changed applyCorrections signature to return error and added error on failed assertion |
internal/services/solution_checker_rules/datasource_solution_checker_rules.go | Added nil-check for client before assigning SolutionCheckerRulesClient |
internal/services/data_record/datasource_data_record.go | Refactored type switches to bind values and added default case to skip unknown types |
internal/services/data_record/api_data_record.go | Added safe extraction of OData fields and improved type assertions for total record count |
internal/services/connection/api_connection.go | Added existence check before asserting principal map values |
.changes/unreleased/fixed-20250605-135507.yaml | Added changelog entry for fixed type assertion issues |
Comments suppressed due to low confidence (5)
internal/services/data_record/api_data_record.go:151
- Add unit tests to cover cases where
@odata.context
is missing or not a string to ensure the error paths are exercised.
odataCtxRaw, exists := response["@odata.context"]
internal/services/data_record/api_data_record.go:160
- Add unit tests for malformed
@odata.context
strings (with no#
separator) to validate this error case.
if len(splitParts) < 2 {
internal/services/data_record/api_data_record.go:297
- Add tests for scenarios where
value
is not a[]any
or is empty to exercise alternate branches inGetTableSingularNameFromPlural
.
if rawVal, exists := mapResponse["value"]; exists && rawVal != nil {
internal/services/connection/api_connection.go:177
- Add unit tests for
getPrincipalString
covering missing keys and non-string values to verify error handling.
raw, exists := principal[key]
internal/services/tenant_settings/resource_tenant_settings.go:410
- Add tests to cover the error branch when
applyCorrections
returns an error in the Create, Read, Update, and Delete methods.
stateDto, err := applyCorrections(ctx, plannedSettingsDto, *tenantSettingsDto)
internal/services/solution_checker_rules/datasource_solution_checker_rules.go
Show resolved
Hide resolved
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…hecker_rules.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@copilot run linter and fix issues |
Linter ran successfully with 0 issues found. No fixes needed. |
Co-authored-by: mattdot <266258+mattdot@users.noreply.github.com>
Co-authored-by: mattdot <266258+mattdot@users.noreply.github.com>
Linter ran successfully with 0 issues found. No fixes needed. |
This PR addresses multiple type assertion safety issues across the codebase that could cause runtime panics when API responses have unexpected structure or types.
Issues Fixed
1. Connection Service (
internal/services/connection/api_connection.go
)getPrincipalString
function performed type assertion without checking if the key exists2. Data Record Service (
internal/services/data_record/api_data_record.go
)@odata.context
andvalue
3. Tenant Settings Service (
internal/services/tenant_settings/api_tenant_settings.go
)applyCorrections
function logged error and returnednil
on type assertion failure4. Data Record DataSource (
internal/services/data_record/datasource_data_record.go
)value
but then unsafe cast ofcolumns[key]
5. Solution Checker Rules (
internal/services/solution_checker_rules/datasource_solution_checker_rules.go
)Testing
Impact
These changes make the provider more resilient to unexpected API responses and prevent potential runtime panics that could crash Terraform operations.
Fixes #839.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.