fix: preserve existing template fields in PATCH requests #19238
+35
−16
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fixes issue #19036 where updating only specific fields (like
default_ttl_ms
) via PATCH request would unintentionally clear other fields (likeicon
,display_name
,description
) when they weren't provided in the request.Problem
When making a PATCH request to
/api/v2/templates/{template_id}
with only{"default_ttl_ms": 12345}
, the template's icon field was being cleared because the request struct would have empty strings for fields not provided in the JSON, and these empty strings were being used to update the database.Solution
The fix preserves existing values for fields that are not explicitly provided in the request:
This follows proper PATCH semantics where only provided fields should be updated.
Changes
coderd/templates.go
: Added logic to preserve existing values forname
,displayName
,description
, andicon
fields when they are empty in the requestcoderd/templates_test.go
: Updated test to verify that fields are preserved when not provided in PATCH requestsTesting
Breaking Changes
This change modifies the behavior of the template PATCH endpoint. Previously, sending an empty string for a field would clear it. Now, empty strings are treated as "preserve existing value". This is generally the expected behavior for PATCH operations, but it means you can no longer explicitly clear fields by sending empty strings.
Fixes
Fixes #19036