Update LocationInput struct to use omitempty for optional fields#29
Update LocationInput struct to use omitempty for optional fields#29filipemarques87 wants to merge 1 commit intodonseba:mainfrom
Conversation
There was a problem hiding this comment.
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
omitemptyJSON tags to optional fields onLocationInput. - Update
TestNewto assert the expected minimal JSON payload forHX-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.
| 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)) |
There was a problem hiding this comment.
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.
| 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"]) | |
| } |
Do not serialize non required field in hx-location header