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

Incorrect handling of example values for dates when a literal is used in the openapi specification. #697

Closed
cosnicolaou opened this issue Dec 12, 2022 · 0 comments · Fixed by #698

Comments

@cosnicolaou
Copy link
Contributor

The following spec, when loaded, will result in an example value of "2019-09-12T00:00:00Z". This is because the literal 2019-09-12 is parsed as a time.Time by the go-yaml package with 0 values for the hours, minutes etc and this is then formatted
as the full value when marshaled back to json using. the invopop/yaml package. The fix is somewhat ugly and shown below, I'll put together a PR shortly for it, but am open to better solutions.

openapi: 3.0.1
components:
  schemas:
    API:
      properties:
        dateExample:
          type: string
          format: date
          example: 2019-09-12

A possible fix is the following change:

func (schema *Schema) UnmarshalJSON(data []byte) error {
	err := jsoninfo.UnmarshalStrictStruct(data, schema)
	if schema.Format == "date" {
		if eg, ok := schema.Example.(string); ok {
			eg = strings.TrimSuffix(eg, "T00:00:00Z")
			schema.Example = eg
		}
	}
	return err
}
@fenollp fenollp linked a pull request Dec 13, 2022 that will close this issue
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