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.26k stars 8.9k forks source link

ExampleObject annotation is not working #6363

Open tibistibi opened 3 years ago

tibistibi commented 3 years ago

I have the following annotation setup:

@RequestBody(description = "Send in your requestor id to subscribe", required = true, content = {
        @Content(mediaType = MediaType.APPLICATION_XML,
                examples = {
                        @ExampleObject(summary = "1", name = "2", value = "3", externalValue = "4", ref = "5",
                                extensions = @Extension(name = "6",
                                        properties = @ExtensionProperty(name = "7", value = "8", parseValue = true)))
                        , @ExampleObject(summary = "a1", name = "a2", value = "a3", externalValue = "a4", ref = "a5",
                        extensions = @Extension(name = "a6",
                                properties = @ExtensionProperty(name = "a7", value = "a8", parseValue = false)))

                }
        )
})

And would expect the example to be present in the swagger-ui but I have this:

image

What did work was the description and the required of the @RequestBody and the mediaType. from the @Content

I use this package: io.swagger.v3.oas.annotations.parameters from swagger annotation 2.0.8 and also not working with the latest at the moment: 2.1.4

tibistibi commented 3 years ago

This is the part what is generated in the yml:


paths:
  /api/siri/vm/subscribe:
    post:
      summary: subscribes to a siri vm stream
      operationId: registerSubscription
      parameters: []
      requestBody:
        description: Send in your requestor id to subscribe
        content:
          application/xml:
            examples:
              2:
                summary: "1"
                value: "3"
                externalValue: "4"
                x-6:
                  7: 8
              a2:
                summary: a1
                value: a3
                externalValue: a4
                x-a6:
                  a7: a8
        required: true
      responses:
        200:
          description: Subscription added or replaced, based on requestor.

So the values are processed.

tibistibi commented 3 years ago

One step further if there is no: schema = @Schema(type="string") in the content than the code will break.

But still no example. only the default example is shown.

image

tibistibi commented 3 years ago

When I change the yml which is generated by hand to this:

      requestBody:
        content:
          application/xml:
            schema:
              type: string
            example: '<xml><bla>asdf asdf asd asf</bla></xml>'

It works: image

But this is not reachable with the current annotations. So to me either the html/javascript code can handle this format:

  requestBody:
    description: Send in your requestor id to subscribe
    content:
      application/xml:
        schema:
          type: string
        examples:
          test2:
            value: "<xml><bla>asdf asdf asd asf</bla></xml>"
    required: true

Or the annotations are changes to generate this:

      requestBody:
        content:
          application/xml:
            schema:
              type: string
            example: '<xml><bla>asdf asdf asd asf</bla></xml>'

Which should be something like this:

@RequestBody(description = "Send in your requestor id to subscribe", required = true, content = {
        @Content(mediaType = MediaType.APPLICATION_XML,
                example = '<xml><bla>asdf asdf asd asf</bla></xml>'
             )
})
WerdnaR commented 3 years ago

@tibistibi You are clearly breaking the spec and I suppose that's a reason why UI can't be generated. Only one of value and externalValue can be defined at a time for an ExampleObject, these fields are mutually exclusive. Also externalValue should be a valid URL.

    /**
     * A string representation of the example.  This is mutually exclusive with the externalValue property, and ignored if the externalValue property is specified.  If the media type associated with the example allows parsing into an object, it may be converted from a string
     *
     * @return the value of the example
     **/
    String value() default "";

    /**
     * A URL to point to an external document to be used as an example.  This is mutually exclusive with the value property.
     *
     * @return an external URL of the example
     **/
    String externalValue() default "";