GCP API网关:无法使用路径参数

时间:2020-11-12 13:21:55

标签: google-cloud-platform yaml openapi google-cloud-api-gateway

我正在努力将路径参数从我的网关传递到实际的端点。

这是我的Open API Yaml:

swagger: '2.0'
info:
  description: |
    Blah blah
  version: 0.0.1
  title: SSAuth
  contact:
    email: blah@gmail.com
schemes:
  - https
produces:
  - application/json
paths:
  /v0/auth/users/echo:
    get:
      summary: check the health of api
      operationId: healthCheck
      consumes:
        - application/json
      produces:
        - application/json
      responses:
        200:
          description: OK
      x-google-backend:
        address: https://path-to-my-cloud-run-service/v0/auth/users/echo
      security:
        - api_key: []

  /v0/auth/users/type/{type}:
    post:
      summary: Add a new user to the user
      operationId: addUser
      consumes:
        - application/json
      produces:
        - application/json
      parameters:
        - name: type
          in: path
          description: provider type of the user
          required: true
          type: string
      responses:
        400:
          description: Invalid input
        200:
          description: OK
      x-google-backend:
        address: https://path-to-my-cloud-run-service/v0/auth/users/type/`type`
      security:
        - api_key: []

securityDefinitions:
  api_key:
    type: apiKey
    name: X-API-Key
    in: header

当我获得第一个路径时,它可以工作。但是在第二个路径中,有一个路径参数,我找不到将参数传递给我的Cloud Run URL的方法。在日志中,我看到的是这个https://path-to-my-cloud-run-service/v0/auth/users/type/%60type%60?type=email而不是https://path-to-my-cloud-run-service/v0/auth/users/type/email,这导致我的服务由于类型无效而被拒绝。

要使这项功能正常工作,我需要更改Yaml吗?

我遇到的另一个问题是,即使我将json放入正文中,GET请求也会收到400个错误请求,即使我指定它消耗了application / json。

1 个答案:

答案 0 :(得分:1)

在挖掘here之后找到了解决方案。

这是path_transaltion,这是有效的Yaml:

swagger: '2.0'
info:
  description: |
    Blahblah
  version: 0.0.1
  title: Title
  contact:
    email: blah@gmail.com
schemes:
  - https
produces:
  - application/json
paths:
  /v0/auth/users/echo:
    get:
      summary: check the health of api
      operationId: healthCheck
      consumes:
        - application/json
      produces:
        - application/json
      responses:
        200:
          description: OK
      x-google-backend:
        address: https://path-to-my-service
        path_translation: APPEND_PATH_TO_ADDRESS
      security:
        - api_key: []

  /v0/auth/users/type/{type}:
    post:
      summary: Add a new user to the user
      operationId: addUser
      consumes:
        - application/json
      produces:
        - application/json
      parameters:
        - name: type
          in: path
          description: provider type of the user
          required: true
          type: string
      responses:
        400:
          description: Invalid input
        200:
          description: OK
      x-google-backend:
        address: https://path-to-my-service
        path_translation: APPEND_PATH_TO_ADDRESS
      security:
        - api_key: []

securityDefinitions:
  api_key:
    type: apiKey
    name: X-API-Key
    in: header