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
Should uuid.Nil marshal to null instead of all zeroes #119
Comments
I ran into some problems while using UUID with the echo framework, which caused me a lot of inconvenience. I would be grateful if there is a solution available for this issue. Moreover, this problem results in an extra burden of validations that need to be implemented. TestResultDTO struct {
ConsumerId uuid.UUID `json:"consumer_id,omitempty"`
} func (h *Handler) TestRun(c echo.Context) error {
var testResultDTO models.TestResultDTO
err := c.Bind(&testResultDTO)
if err != nil {
return err
}
jsonString, err := json.Marshal(testResultDTO)
if err != nil {
panic(err)
}
fmt.Println(string(jsonString))
return c.JSON(http.StatusCreated, testResultDTO)
} Sending a POST request to the test endpoint: curl --request POST \
--url http://localhost:xxxx/test \
--header 'Content-Type: application/json' \
--header 'User-Agent: foo' \
--data '{}' {
"consumer_id": "00000000-0000-0000-0000-000000000000"
} Which in this case is undesirable. I attempted the following method, but it did not properly evaluate and had issues with structures that produce new UUIDs. TestResultDTO struct {
ConsumerId uuid.NullUUID `json:"consumer_id,omitempty"`
} Outcome from the same endpoint: {
"consumer_id": null
} There needs to be a solution that is more compliant with other libraries. Especially when you need to perform the following in order to reduce overhead: ProviderId uuid.NullUUID `json:"provider_id,omitempty" validate:"required_without=ConsumerId,uuid"`
ConsumerId uuid.NullUUID `json:"consumer_id,omitempty" validate:"required_without=ProviderId,uuid"` |
Following up on this, but did you ever find a solution? Personally was expecting |
I'm interested in a solution for this as well. |
The library uses the
uuid.Nil
value as an empty value. This suggests to me that Nil value is treated as a special value, which is in line with the RFC. However, the behavior of this value when marshaling to json is questionable. The library serializes to00000000-0000-0000-0000-000000000000
(play) instead ofnull
. Should this behavior be changed? Marshaling to text can probably yield an error, or just empty string, not sure.The text was updated successfully, but these errors were encountered: