swagger-api / swagger-ui

Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
https://swagger.io
Apache License 2.0
26.67k stars 8.97k forks source link

Date example show incorrect result #5744

Open chameleon82 opened 4 years ago

chameleon82 commented 4 years ago

Q&A (please complete the following information)

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"

hkosova commented 4 years ago

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.

chameleon82 commented 4 years ago

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
typhoon2k commented 4 years ago

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

chris-rl commented 4 years ago

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

chameleon82 commented 4 years ago

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

ivenxu commented 3 years ago

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.

jaydreyer commented 3 years ago

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.

dinob68 commented 2 years ago

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

sodik82 commented 2 years ago

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

Zepryit commented 2 years ago

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
muchnik commented 2 years ago

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.

hosarb commented 1 year ago

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

icaroleon commented 10 months ago

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

me 2