Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use $ref within a schema in OpenAPI 3.0?

Tags:

openapi

I want to represent the following JSON as a schema in an OpenAPI 3.0 API definition:

{
get-question: {
  question-id:string
  }
}

So far, I have written:

components:
  schemas:
  #schema of a question-id
    QuestionID:   #{question-id: string}
      properties:
        question-id:
          type: string
      required:
        - question-id

  #schema of a get-question request which contains a question id      
    GetQuestion: #{get-question: {question-id:string}}
      properties:
        get-questions:
          type: $ref:'#/components/schemas/QuestionID'
      required:
        - get-questions

but I am getting these errors in the Swagger Editor:

Schema error at components.schemas['GetQuestion']
should have required property '$ref'
missingProperty: $ref
Jump to line 79
Schema error at components.schemas['GetQuestion']
should match exactly one schema in oneOf
Jump to line 79
Schema error at components.schemas['GetQuestion'].properties['get-questions']
should have required property '$ref'
missingProperty: $ref
Jump to line 81
Schema error at components.schemas['GetQuestion'].properties['get-questions']
should match exactly one schema in oneOf
Jump to line 81
Schema error at components.schemas['GetQuestion'].properties['get-questions'].type
should be equal to one of the allowed values
allowedValues: array, boolean, integer, number, object, string
Jump to line 82

What is the correct syntax for $ref?

like image 916
Manu Chadha Avatar asked Nov 26 '18 06:11

Manu Chadha


People also ask

Where can I use $Ref in OpenAPI?

Actually $ref is only allowed in places where the OpenAPI 3.0 Specification explicitly states that the value may be a reference. For example, $ref cannot be used in the info section and directly under paths : # Incorrect! Any sibling elements of a $ref are ignored.

What is OpenAPI and how does it work?

What Is OpenAPI? OpenAPI Specification (formerly known as Swagger Specification) is an open-source format for describing and documenting APIs. The Specification was originally developed in 2010 by Reverb Technologies (formerly Wordnik) as a way to keep the API design and documentation in sync.

How do I reference OpenAPI code in IBM® API Connect Management?

If you deploy an API to an IBM® API Connect Management server by using the developer toolkit command line, you can use the $ref field in your OpenAPI (Swagger 2.0) YAML and JSON API definition files to reference a fragment of OpenAPI (Swagger 2.0) code that is defined in a separate file.

What are reusable components in OpenAPI?

The OpenAPI Specification has a solution – reusable components that can be used across multiple endpoints in the same API. These components are defined in the global components section and then are referenced in individual endpoints. The Specification defines various types of reusable components:


1 Answers

$ref is used instead of type, not as the value of type. Also note the space after : to separate the key and value in YAML.

        get-questions:
          $ref: '#/components/schemas/QuestionID'

You also need to add type: object to your QuestionID and GetQuestion schemas to indicate that they are objects; the properties keyword alone is not enough.

There also seems to be a typo in one of the property names - it's get-questions (plural) in the GetQuestion schema but get-question (singular) in your JSON example. I guess it should be get-question.

Complete example:

components:
  schemas:
    # schema of a question-id
    QuestionID:      # {question-id: string}
      type: object   # <-----
      properties:
        question-id:
          type: string
      required:
        - question-id

    #schema of a get-question request which contains a question id      
    GetQuestion:     # {get-question: {question-id:string}}
      type: object   # <-----
      properties:
        get-question:
          $ref: '#/components/schemas/QuestionID'   # <-----
      required:
        - get-questions
like image 122
Helen Avatar answered Oct 13 '22 20:10

Helen