go-openapi / strfmt

openapi toolkit common string formats
Apache License 2.0
102 stars 62 forks source link

DateTaime.IsZero() should support pointer receiver #106

Closed pavel-boldyrev-form3 closed 1 year ago

pavel-boldyrev-form3 commented 1 year ago

I think this is a regression after https://github.com/go-openapi/strfmt/pull/105, particularly this line:

func (t DateTime) IsZero() bool {
    return time.Time(t).IsZero()
}

I think IsZero() func should accept a pointer receiver, as it may be called on nil values.

For example, I have a generated code from a swagger model that does the following check on an optional date-time field created_on in the request:

func (m *Entity) validateCreatedOn(formats strfmt.Registry) error {
    if swag.IsZero(m.CreatedOn) { // not required
        return nil
    }

    if err := validate.FormatOf("created_on", "body", "date-time", m.CreatedOn.String(), formats); err != nil {
        return err
    }

    return nil
}

which now panics on the very first if statement.

piepmatz commented 1 year ago

Alternative: https://github.com/go-openapi/swag/pull/67