Skip to content
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

Fix marshalling of URL wrapper with nil value. #303

Merged

Conversation

pstibrany
Copy link
Contributor

@pstibrany pstibrany commented Jun 2, 2021

MarshalJSON must return valid JSON. This PR fixes marshalling of config.URL wrapper values with nil value. It now returns null.

This PR also changes unmarshalling of null or "" JSON strings into URL: in both cases, wrapper with nil value is returned. (removed from this PR)

Related to prometheus/alertmanager#2607

@pstibrany
Copy link
Contributor Author

(This is currently blocking cortexproject/cortex#4237)

Added unit tests documenting unmarshalling.

Signed-off-by: Peter Štibraný <pstibrany@gmail.com>
@pstibrany pstibrany force-pushed the marshalling-url-wrapper-with-nil branch from 0f50e0c to 558a787 Compare June 3, 2021 08:33
@pstibrany
Copy link
Contributor Author

Based on discussion in prometheus/alertmanager#2607, I've removed changes to unmarshalling in this PR, but still added unit tests documenting the current behaviour.

Copy link

@stevesg stevesg left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -130,7 +130,7 @@ func (u URL) MarshalJSON() ([]byte, error) {
if u.URL != nil {
return json.Marshal(u.URL.String())
}
return nil, nil
return []byte("null"), nil
Copy link

Choose a reason for hiding this comment

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

[uber-nit] This might seem like a crazy idea, but we could save hard-coding the null by marshaling nil:

Suggested change
return []byte("null"), nil
return json.Marshal(nil)

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.

None yet

3 participants