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

yaml marshalling with omitempty is broken due to IsZero on pointer #139

Open
prashantv opened this issue Jan 10, 2025 · 0 comments · May be fixed by #140
Open

yaml marshalling with omitempty is broken due to IsZero on pointer #139

prashantv opened this issue Jan 10, 2025 · 0 comments · May be fixed by #140

Comments

@prashantv
Copy link

The switch to pointer receivers to fix #106 broke YAML marshalling of strfmt.DateTime when used with a omitempty value field.

Repro:

``` package main

import (
"fmt"
"time"

    "github.com/go-openapi/strfmt"
    "gopkg.in/yaml.v3"

)

type S struct {
Name string yaml:"name"
Created strfmt.DateTime yaml:"created,omitempty"
}

func main() {
s := S{
Name: "test",
Created: strfmt.DateTime(time.Now()),
}
marshalled, err := yaml.Marshal(s)
if err != nil {
panic(err)
}

    fmt.Println(string(marshalled))

}

</details>

With 0.21.5, the output of the above is:

name: test
created: "2025-01-09T16:13:43.220-08:00"


With 0.21.6, and all newer versions including 0.23.0, the output is:

name: test

(no output for `created`).

The primary reason is that when the field is specified as a value, only value methods can be called.

The fix was originally added to because `swag.IsZero` did not handle pointer, but that was fixed in https://github.com/go-openapi/swag/pull/67 so I think the the `IsZero` method should be moved back to the value.
prashantv added a commit to prashantv/strfmt that referenced this issue Jan 10, 2025
prashantv added a commit to prashantv/strfmt that referenced this issue Jan 10, 2025
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 a pull request may close this issue.

1 participant