Forum Discussion

piyushkurkure10's avatar
piyushkurkure10
New Member
4 years ago

Can I use $ref in examples?

I want to use ref in example for a schema
My openapi code:

 

'200':
  description: The request is successful
  content:
     application/json:
         schema:
            $ref: '#/components/schemas/ResponseSchema'
         example:
            $ref: '#/components/examples/200Example'

 

but I get warning like: 
warning no-invalid-media-type-examples Example value must conform to the schema: must NOT have additional properties `$ref`. 
And the example properly matches the schema so no problem with that.

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Replace "example" with "examples" (plural) as shown below:

    '200':
      description: The request is successful
      content:
         application/json:
             schema:
                $ref: '#/components/schemas/ResponseSchema'
             examples:      # <------
                example1:   # <------ arbitrary name
                   $ref: '#/components/examples/200Example'
    
    ...
    components:
      examples:
        200Example:
          summary: A short summary of this example
          value:
            key1: value1
            key2: value2