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

Should uuid.Nil marshal to null instead of all zeroes #119

Open
maroux opened this issue Aug 4, 2023 · 3 comments
Open

Should uuid.Nil marshal to null instead of all zeroes #119

maroux opened this issue Aug 4, 2023 · 3 comments

Comments

@maroux
Copy link

maroux commented Aug 4, 2023

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 to 00000000-0000-0000-0000-000000000000 (play) instead of null. Should this behavior be changed? Marshaling to text can probably yield an error, or just empty string, not sure.

@petkostas
Copy link

petkostas commented Oct 27, 2023

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"`

@tyliec
Copy link

tyliec commented Mar 6, 2024

Following up on this, but did you ever find a solution?

Personally was expecting omitempty to result in nothing, rather than a bunch of 00000000-0000-0000-0000-000000000000s

@mazar
Copy link

mazar commented Mar 25, 2024

I'm interested in a solution for this as well.

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

No branches or pull requests

4 participants