Skip to content

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

@cosnicolaou

Description

@cosnicolaou

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions