Skip to content

Commit d310c4a

Browse files
committed
Fix JSON marshalling
Signed-off-by: Marco Pracucci <[email protected]>
1 parent 0f4f649 commit d310c4a

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

config/headers.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package config
1818

1919
import (
20+
"encoding/json"
2021
"fmt"
2122
"net/http"
2223
"os"
@@ -50,17 +51,22 @@ var reservedHeaders = map[string]struct{}{
5051

5152
// Headers represents the configuration for HTTP headers.
5253
type Headers struct {
53-
Headers map[string]Header `yaml:",inline" json:",inline"`
54+
Headers map[string]Header `yaml:",inline"`
5455
dir string
5556
}
5657

57-
// Headers represents the configuration for HTTP headers.
58+
// Header represents the configuration for a single HTTP header.
5859
type Header struct {
5960
Values []string `yaml:"values,omitempty" json:"values,omitempty"`
6061
Secrets []Secret `yaml:"secrets,omitempty" json:"secrets,omitempty"`
6162
Files []string `yaml:"files,omitempty" json:"files,omitempty"`
6263
}
6364

65+
func (h Headers) MarshalJSON() ([]byte, error) {
66+
// Inline the Headers map when serializing JSON because json encoder doesn't support "inline" directive.
67+
return json.Marshal(h.Headers)
68+
}
69+
6470
// SetDirectory records the directory to make headers file relative to the
6571
// configuration file.
6672
func (h *Headers) SetDirectory(dir string) {

config/http_config_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,7 +2014,6 @@ func TestHTTPClientConfig_Marshal(t *testing.T) {
20142014
ProxyConfig: ProxyConfig{
20152015
ProxyURL: URL{proxyURL},
20162016
},
2017-
HTTPHeaders: &Headers{},
20182017
}
20192018

20202019
t.Run("YAML", func(t *testing.T) {
@@ -2024,7 +2023,7 @@ func TestHTTPClientConfig_Marshal(t *testing.T) {
20242023
proxy_url: "http://localhost:8080"
20252024
follow_redirects: false
20262025
enable_http2: false
2027-
http_headers: {}
2026+
http_headers: null
20282027
`, string(actualYAML))
20292028

20302029
// Unmarshalling the YAML should get the same struct in input.
@@ -2042,7 +2041,7 @@ http_headers: {}
20422041
"tls_config":{"insecure_skip_verify":false},
20432042
"follow_redirects":false,
20442043
"enable_http2":false,
2045-
"http_headers":{}
2044+
"http_headers":null
20462045
}`, string(actualJSON))
20472046

20482047
// Unmarshalling the JSON should get the same struct in input.

0 commit comments

Comments
 (0)