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

Marshalled JSON includes attributes with Present: false #21

Open
skplunkerin opened this issue Feb 23, 2024 · 2 comments
Open

Marshalled JSON includes attributes with Present: false #21

skplunkerin opened this issue Feb 23, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@skplunkerin
Copy link

In the provided code, when unmarshalling the JSON the Weight attribute is correctly set to present: false, valid: false as "Weight" isn't specified in the JSON.

However, when marshalling the struct back to JSON the output includes the Weight attribute with a value of null. This is unexpected as I would assume Present: false would omit the attribute from the marshalled JSON (and that the marshalled JSON should match the original JSON).

Steps to Reproduce

  1. Run the following code: (modified from this example in the project README.md)
package main

import (
	"encoding/json"
	"fmt"

	"github.com/LukaGiorgadze/gonull"
)

type MyCustomInt int
type MyCustomFloat32 float32

type Person struct {
	Name    string
	Age     gonull.Nullable[MyCustomInt]     // present: true,  valid: true
	Address gonull.Nullable[string]          // present: true,  valid: false
	Height  gonull.Nullable[MyCustomFloat32] // present: true,  valid: false
	Weight  gonull.Nullable[string]          // present: false, valid: false
}

func main() {
	jsonData := []byte(`{"Name":"Alice","Age":15,"Address":null,"Height":null}`)

	var person Person
	err := json.Unmarshal(jsonData, &person)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Unmarshalled Person: %+v\n", person)

	marshalledData, err := json.Marshal(person)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Marshalled JSON: %s\n", string(marshalledData))
}

Expected Behavior

The marshalled JSON should not include the Weight attribute as it was not present in the original JSON (Present: false):

{
  "Name": "Alice",
  "Age": 15,
  "Address": null,
  "Height": null
}

Actual Behavior

The marshalled JSON includes the Weight attribute with a value of null:

{
  "Name": "Alice",
  "Age": 15,
  "Address": null,
  "Height": null,
  "Weight": null // <-- not expected, this should not be included
}

Environment

Additional Context

This issue was discovered while testing the unmarshalling and marshalling of JSON data with optional fields in the Go Playground. The expected behavior is based on the assumption that Present: false should result in the attribute being omitted from the marshalled JSON.

@LukaGiorgadze LukaGiorgadze added the bug Something isn't working label Feb 26, 2024
@LukaGiorgadze
Copy link
Owner

LukaGiorgadze commented Mar 7, 2024

@skplunkerin thanks for opening an issue.
Currently, to omit non-presenting field is to use a pointer and omitempty tag such way:

Weight *gonull.Nullable[string] `json:"weight,omitempty"`

I'll work on it to fix this issue ASAP.

@rob2244
Copy link

rob2244 commented May 14, 2024

+1 for this, it also doesn't seem to work correctly when omitempty is specified and it's not a pointer to a gonull.Nullable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants