swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

Date example show incorrect result

Open chameleon82 opened this issue 5 years ago • 13 comments

Q&A (please complete the following information)

  • OS: macOS
  • Browser:chrome
  • Version: 78
  • Method of installation: dist assets
  • Swagger-UI version: 3.24.3 and http://editor.swagger.io/
  • Swagger/OpenAPI version: OpenAPI 3.0

Content & configuration

Example Swagger/OpenAPI definition:

 properties:
        checkInDate:
          title: Date in ISO8201 format
          type: string
          format: date
          example: 2019-02-01

Describe the bug you're encountering

Following the specification i should see correct example in UI "checkInDate": "2019-02-01" but instead i see "checkInDate": {}

In schema block i see: example: OrderedMap {}

Expected behavior

Correct property example in dto object in request body

"checkInDate": "2019-02-01"

chameleon82 avatar Dec 11 '19 05:12 chameleon82

You need to add quotes around the example value:

type: string
format: date
example: '2019-02-01'   # <----

Without the quotes, the value is parsed as a YAML timestamp which is a different data type than string.

hkosova avatar Dec 11 '19 11:12 hkosova

YAML timestamp is exactly i expected to be applied here. There is example under the link date (00:00:00Z): 2002-12-14 and i expecting swagger will format json string from parsed timestamp as a date with specified format format: date and type type:string and as output i will have:

{ 
    "checkInDate": "2019-02-01"
}

This behaviour i expect as well in the swagger-core/swagger-annotations, for example i can define next code:

  @io.swagger.v3.oas.annotations.Operation(
    ...
    requestBody = new RequestBody(
      content = Array(
        new Content(
          schema = new Schema(implementation = classOf[MyDto]),
          examples = Array(
            new ExampleObject(
              name = "example",
              value =
                "{\n  \"checkInDate\": \"2019-02-01\"\n }"
            )
          )
        )
      )
    ),

where example body is

{
    "checkInDate": "2019-02-01"
}

and class is

  class MyDto {
    @Schema(required = true, title = "Date in ISO8201 format", example = "2019-02-01")
    public java.time.LocalDate invoiceDate;
  }

and from swagger-core i see expected output


 requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MyDto'
            examples:
              example:
                description: example
                value:
                  checkInDate: 2019-02-01

  MyDto:
     properties:
        checkInDate:
          title: Date in ISO8201 format
          type: string
          format: date
          example: 2019-02-01

chameleon82 avatar Dec 14 '19 08:12 chameleon82

Hi!

We just encountered the same problem with date/datetime examples. I know that workaround is to use quotes/double-quotes, but in our case swagger bundle is auto-generated and it correctly doesn't have any quotes (as per https://yaml.org/spec/1.2/spec.html#id2788859 only combination of symbols ": " requires quoting). As a result all our date/date-time examples are displayed as {}.

As far as I understand this is a problem with js-yaml library (without quotes vs with quotes).

best regards, ty

typhoon2k avatar Aug 10 '20 12:08 typhoon2k

Hi!

I experienced the same issue with dates in examples. The workaround with putting quotes (either ' or " ) does not work for me as well. It always shows the date value as {}.

Kind regards, Christian

chris-rl avatar Oct 28 '20 09:10 chris-rl

@typhoon2k I think js-yaml correctly parse date/date-time as JavaScript Date object. Seems problem that Swagger cannot convert it to string properly

chameleon82 avatar Nov 04 '20 02:11 chameleon82

You need to add quotes around the example value:

type: string
format: date
example: '2019-02-01'   # <----

Without the quotes, the value is parsed as a YAML timestamp which is a different data type than string.

Quotes solution doesn't work, please fix.

ivenxu avatar May 28 '21 06:05 ivenxu

We are having the same problem with this. Quotes fixes it, but the spec is auto-generated and they'd have to manually update a bunch of params every time.

jaydreyer avatar Aug 31 '21 15:08 jaydreyer

Same problem here. I use Spring boot and my request model has field birthDate like:

    @NotNull
    @Schema(description = "User birthdate.", example = "19680228", format = "yyyyMMdd", required = true, nullable = false)
    private OffsetDateTime birthDate;

I would expect this to work, however, in SwaggerUI, none of this really helps. This is what I get it for that request model and also as example for the request body:

swagger date format example issue1

dinob68 avatar Jun 23 '22 21:06 dinob68

btw - it looks working fine on https://editor.swagger.io/ now...

sodik82 avatar Sep 06 '22 15:09 sodik82

For me it seems that the problem only occurs when a $ref is used

paths:
  /pet/{petId}:
    get:
      parameters:
        # does not work
        - $ref: components/parameters/date.yaml
        # does work
        - name: date
          in: query
          schema:
            title: Date
            type: string
            format: date
          examples:
            example1:
              value: 2022-10-12
      responses:
        '200':
          description: successful operation

components/parameters/date.yaml looks like:

name: date-ref
in: query
schema:
  title: Date
  type: string
  format: date
examples:
  example1:
    value: 2022-10-12

Zepryit avatar Oct 26 '22 15:10 Zepryit

Any update on it? I've got same issue as @Zepryit mentioned above All date example are formed as date-time when $ref is used.

muchnik avatar Nov 14 '22 16:11 muchnik

I have the same issue with dates. the pattern and example I provided in the @Schema, is ignored!!!

hosarb avatar Mar 16 '23 09:03 hosarb

I have the same issue with dates. the pattern and example I provided in the @Schema, is ignored!!!

me 2

icaroleon avatar Jan 28 '24 23:01 icaroleon