Here is an example showing how to use OpenAPI schema references in Spring Cloud Gateway for Kubernetes.

OpenAPI references can be used by multiple API routes so that they don't have to duplicate definitions in route configuration. It works via the '$ref' property, which targets an object in the openapi section. Currently, this feature is only supported for requests and responses.

The following example references UserRequest and UserResponse objects, which point to schemas.User:

routes:
- predicates:
    - Path=/api/users
    - Method=POST
  model:
    requestBody:
      content:
        'application/json':
          schema:
            '$ref': "/components/requestBodies/UserRequest"
    responses:
      '200':
        content:
          'application/json':
            schema:
              '$ref': "/components/schemas/UserResponse"
openapi:
  components:
    schemas:
      User:
        type: object
        properties:
          id:
            type: string
          name:
            type: string
          email:
            type: string
            format: email
      UserResponse:
        '$ref': "/components/schemas/User"
    requestBodies:
      UserRequest:
        required: ["name", "email"]
        '$ref': "/components/schemas/User"
check-circle-line exclamation-circle-line close-line
Scroll to top icon