3

I tried to create minimal example of the problem.

Let's say we have simple return object:

public class Result {

    @Schema(example = "2012-01-01")
    private LocalDate sampleDate;

    // omitted getter and setter
}

returned by simple JAX-RS endpoint:

@Path("/example")
@Produces(MediaType.APPLICATION_JSON)
public class Resource {

    public List<Result> example() {
        // omitted implementation
    }

}

MicroProfile OpenAPI in Open Liberty will automatically generate following OpenAPI (Swagger) file:

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

The problem is that embedded Swagger UI is displaying the date example as empty JS object:

Swagger UI screenshot

I'm not sure if this is bug on Swagger UI side because if I don't provide any example in Java annotation = any example in OpenAPI file it will render the example as current day, e.g.:

[
  {
    "sampleDate": "2018-11-27"
  }
]

Everything works correctly when I edit the OpenAPI output manually. Both single and double quotes fix the problem:

...
sampleDate:
  type: string
  format: date
  example: '2012-01-01'

or

...
sampleDate:
  type: string
  format: date
  example: "2012-01-01"

will produce expected output:

[
  {
    "sampleDate": "2012-01-01"
  }
]

Question is how to change the annotations to get desired OpenAPI output. Single quotes are automatically escaped:

@Schema(example = "'2012-01-01'")
private LocalDate sampleDate;

will produce:

...
sampleDate:
  type: string
  format: date
  example: '''2012-01-01'''

Additional double quotes in Java doesn't have any effect on ouput:

@Schema(example = "\"2012-01-01\"")
private LocalDate sampleDate;

will produce same unquoted output:

...
sampleDate:
  type: string
  format: date
  example: 2012-01-01

I know that I can write the OpenAPI yaml output manually but that is my last resort because I don't want to sacrifice automatic generation just because date examples are not behaving as I want. Maybe some OASFilter can be implemented to automatically wrap date's example values or I'm just missing something obvious here.

  • 1
    This is not the answer to your question, but just FYI the UI rendering issue happens because in YAML `2012-01-01` without quotes is a [timestamp](http://yaml.org/type/timestamp.html), not a string. But in OpenAPI, `type: string` (regardless of `format`) expects a string example, e.g. `'2012-01-01'` or `"2012-01-01"`, as you already found out. – Helen Nov 27 '18 at 16:34
  • File a bug report with the tool/framework that generates the OpenAPI file, as it generate date examples incorrectly (without quotes). – Helen Nov 27 '18 at 17:05

1 Answers1

0

I've confirmed the behaviour that you're describing. It the issue with Swagger-UI which is packaged with Microprofile OpenAPI, you could open issue here: Swagger UI GitHub.

The value produced, without quotes is completely valid yaml, so UI should be able to parse it as is.

  • 1
    You should edit the link your post, it's broken and points to the whole repo. It would be better to link to the issue and include the quote the description – Wayne Phipps Nov 27 '18 at 17:11
  • Swagger UI team doesn't consider described behavior to be a problem because there is at least two possible ways how to typecast timestamp into string in YAML format. – Róbert Komorovský Jan 17 '19 at 06:30