redoc icon indicating copy to clipboard operation
redoc copied to clipboard

Possiblily to display String as JSON in the Response samples block

Open pqab opened this issue 1 year ago • 2 comments

Describe the problem to be solved Is there any way to detect the media type application/json and display the JSON in the Response samples block?

For example

in this case, it works properly, it can display the JSON in the Response samples block

openapi: 3.0.1
info:
  title: Example
  version: 1.0.0
paths:
  /example:
    get:
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Config'
        "404":
          content:
            application/json:
              schema:
                type: object
                example: {'errors': [{'code': 'E001', 'message': 'NotFound'}]}
components:
  schemas:
    Config:
      type: object
      properties:
        name:
          type: string
      example: {'name': 'A'}

But in this case it will display as a plain text

openapi: 3.0.1
info:
  title: Example
  version: 1.0.0
paths:
  /example:
    get:
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Config'
        "404":
          content:
            application/json:
              schema:
                type: object
                example: "{'errors': [{'code': 'E001', 'message': 'NotFound'}]}"
components:
  schemas:
    Config:
      type: object
      properties:
        name:
          type: string
      example: "{'name': 'A'}"

Describe the solution you'd like Display the example based on the media type

Describe alternatives you've considered N/A

Additional context N/A

pqab avatar Dec 22 '23 01:12 pqab

You are passing a string value in the example rather than the JSON object. So in reality, it's correct as a text/plain

Is there some reason you are passing string values vs JSON objects?

jeremyfiel avatar Dec 28 '23 16:12 jeremyfiel

we are using the micronaut with swagger annotations in JAVA, and the example field is a String in the annotation, it serialized the yaml as is

https://github.com/swagger-api/swagger-core/blob/v2.2.20/modules/swagger-annotations/src/main/java/io/swagger/v3/oas/annotations/media/Schema.java#L256

however swagger ui could display as a JSON, we are not sure whether it's something micronaut or redoc should provide

pqab avatar Dec 30 '23 12:12 pqab