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

OpenAPI YAML timestamp example value is not displayed correctly #5061

Closed
biobob opened this issue Dec 4, 2018 · 2 comments
Closed

OpenAPI YAML timestamp example value is not displayed correctly #5061

biobob opened this issue Dec 4, 2018 · 2 comments

Comments

@biobob
Copy link

biobob commented Dec 4, 2018

Q&A (please complete the following information)

  • OS: Windows 10
  • Browser: chrome, firefox, ...
  • Version: latest
  • Method of installation: I used https://editor.swagger.io/ only
  • Swagger-UI version: not sure - the one used in online editor
  • Swagger/OpenAPI version: OpenAPI 3.0

Content & configuration

Example Swagger/OpenAPI definition:

openapi: 3.0.0
info:
  title: Deployed APIs
  version: 1.0.0
servers:
- url: http://localhost:9080/api
paths:
  /example:
    get:
      operationId: example
      responses:
        default:
          description: default response
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Result'
components:
  schemas:
    Result:
      type: object
      properties:
        sampleDate:
          type: string
          format: date
          example: 2012-01-01

Above sample YAML file is actually generated from Java code using MicroProfile framework inside Open Liberty web application server.

Describe the bug you're encountering

Example value of date property is not rendered correctly when provided as timestamp value. Only strings (quoted or double-quoted) are rendered as expected.

To reproduce...

Steps to reproduce the behavior:

  1. I pasted the YAML to https://editor.swagger.io/
  2. See the Example Value section
  3. Date example is displayed incorrectly as empty JS object

Expected behavior

I would expect to see the date that is provided in OpenAPI definition.

Screenshots

image

Additional context or thoughts

See the stackoverflow question that pointed to creation of this issue.

@shockey
Copy link
Contributor

shockey commented Dec 7, 2018

Hey @biobob!

This is because our YAML parser (js-yaml) is converting your example value to a JavaScript Date object, which you see as an empty object ({}).

This is in line with the YAML spec, which calls for scalar values that don't explicitly label what type they are to be resolved based on their value:

[Plain scalars] may be matched against a set of regular expressions to provide automatic resolution of integers, floats, timestamps, and similar types. An application may also match the content of mapping nodes against sets of expected keys to automatically resolve points, complex numbers, and similar types.

ref: YAML 1.2 spec §3.3.2, emphasis mine

If you'd like to avoid this, any of these formats for your example will solve this:

example: "2012-01-01"
example: !!str 2012-01-01

Alternatively, if you remove your example field entirely, the latest version of Swagger Editor should automatically generate an example value for you based on the current date.

More context on this over at js-yaml: nodeca/js-yaml#198

Edited: modified usage of "string" vs "scalar" terminology

@shockey
Copy link
Contributor

shockey commented Jan 15, 2019

Closing due to inactivity.

This is simply to keep our issue tracker clean - feel free to comment if there are any further thoughts or concerns, and we'll be happy to reopen this issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants