Skip to content

Update LocationInput struct to use omitempty for optional fields#29

Open
filipemarques87 wants to merge 1 commit intodonseba:mainfrom
filipemarques87:main
Open

Update LocationInput struct to use omitempty for optional fields#29
filipemarques87 wants to merge 1 commit intodonseba:mainfrom
filipemarques87:main

Conversation

@filipemarques87
Copy link
Copy Markdown

@filipemarques87 filipemarques87 commented Mar 28, 2026

Do not serialize non required field in hx-location header

Copy link
Copy Markdown

Copilot AI left a 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 updates the LocationInput JSON serialization so optional fields are omitted from the HX-Location header payload, matching the intent of not emitting non-required fields.

Changes:

  • Add omitempty JSON tags to optional fields on LocationInput.
  • Update TestNew to assert the expected minimal JSON payload for HX-Location.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
handler.go Marks optional LocationInput fields with omitempty so empty values aren’t serialized into HX-Location.
htmx_test.go Adds an assertion to verify the HX-Location JSON output omits empty optional fields.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 76 to +78
j, _ := json.Marshal(location)
equal(t, string(j), handler.ResponseHeader(HXLocation))
equal(t, `{"path":"http://new-url.com","source":"source","target":"body"}`, handler.ResponseHeader(HXLocation))
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

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

The test now asserts an exact JSON string for HX-Location. This is fairly brittle (it depends on encoding/json output formatting and field ordering) and it’s also redundant with the prior json.Marshal(location) equality check. Consider instead unmarshalling the header value into a map[string]any (or similar) and asserting the expected keys/values are present and that optional keys are absent (len==3), and apply the same assertion pattern to TestNoRouter so the behavior is consistently validated across both code paths.

Suggested change
j, _ := json.Marshal(location)
equal(t, string(j), handler.ResponseHeader(HXLocation))
equal(t, `{"path":"http://new-url.com","source":"source","target":"body"}`, handler.ResponseHeader(HXLocation))
locationHeader := handler.ResponseHeader(HXLocation)
var locationMap map[string]any
if err := json.Unmarshal([]byte(locationHeader), &locationMap); err != nil {
t.Fatalf("expected HX-Location header to contain valid JSON, got %q: %v", locationHeader, err)
}
if len(locationMap) != 3 {
t.Fatalf("expected HX-Location JSON to have exactly 3 keys, got %d: %#v", len(locationMap), locationMap)
}
if v, ok := locationMap["path"].(string); !ok || v != location.Path {
t.Errorf("expected HX-Location.path %q, got %#v", location.Path, locationMap["path"])
}
if v, ok := locationMap["source"].(string); !ok || v != location.Source {
t.Errorf("expected HX-Location.source %q, got %#v", location.Source, locationMap["source"])
}
if v, ok := locationMap["target"].(string); !ok || v != location.Target {
t.Errorf("expected HX-Location.target %q, got %#v", location.Target, locationMap["target"])
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants