将 google 端点中的路径参数传递给后端不起作用

passing path parameter in google endpoints to backend not working

我的设置包含 google-endpoints 和 google-cloud-functions 作为我的后端。

Google 端点使用以下 swagger v2 yaml 定义:

swagger: "2.0"
info:
  description: "yada..."
  version: "0.0.1"
  title: "yadada.."
  termsOfService: "http://swagger.io/terms/"
  contact:
    name: "blah"
    email: "email@mail.com"
    url: "https://example.com"
host: "(generated service url by google when endpoints is deployed, i.e. 'api-gateway-xyz123123-ew.a.run.app')"
tags:
  - name: "Documents"
    description: "blah"
schemes:
  - "https"
paths:
  /api/documents:
    post:
      tags:
        - "Documents"
      summary: "Add a new document"
      description: ""
      security:
        - firebase: []
      operationId: "addDocument"
      x-google-backend:
        address: "(cloud functions http url)/documents"
      consumes:
        - "application/json"
      produces:
        - "application/json"
      parameters:
        - in: "body"
          name: "body"
          description: "Document supplied"
          required: true
          schema:
            $ref: "#/definitions/Document"
      responses:
        201:
          description: "The document was successfully created."
          schema:
            $ref: "#/definitions/Document"
        400:
          description: "Invalid input. See response for details"
          schema:
            items:
              $ref: "#/definitions/Error"
  /api/documents/{document_id}:
    get:
      tags:
        - "Documents"
      summary: "Get a document with the given ID"
      description: ""
      security:
        - firebase: []
      operationId: "getDocument"
      x-google-backend:
        address: "(cloud function http url)/documents/"
        path_translation: APPEND_PATH_TO_ADDRESS
      produces:
        - "application/json"
      parameters:
        - in: "path"
          name: "document_id"
          description: "ID of the document to modify"
          required: true
          type: "string"
      responses:
        200:
          description: "success."
          schema:
            type: "array"
            items:
              $ref: "#/definitions/Document"
        404:
          description: "Document not found"
          schema:
            items:
              $ref: "#/definitions/Error"
securityDefinitions:
  firebase:
    authorizationUrl: ""
    flow: "implicit"
    type: "oauth2"
    x-google-issuer: "https://securetoken.google.com/%%GOOGLE_PROJECT_ID%%"
    x-google-jwks_uri: "https://www.googleapis.com/service_accounts/v1/metadata/x509/securetoken@system.gserviceaccount.com"
    x-google-audiences: "%%GOOGLE_PROJECT_ID%%"
definitions:
  (a lot of type definitions)

这适用于 POST 端点,没有任何问题。

问题出在 GET REST 端点,其中路径变量未正确传递到后端。

https://cloud.google.com/endpoints/docs/openapi/openapi-extensions 一样,我尝试添加 x-google-backend 参数,就像上面的 swagger api 一样。 (path_translation: APPEND_PATH_TO_ADDRESS).

但是这不起作用。 我收到未经授权的错误 (403),因为端点前端未命中云函数。

目前,我使用不带 path_translation 参数的丑陋解决方法,该参数将 google 端点路径变量转换为云函数后端中具有相同名称的查询参数。 IE。在后端调用 url /documents?document_id=xyz

(我尝试实现的是通过后端传递调用url /documents/{document_id})

有谁知道如何正确配置基于路径的参数,以便将它们正确传递到云函数后端?

提前致谢。

此致, 塞巴斯蒂安

长话短说: 我假设您的 403 错误不是正确的错误。应该是404,但是因为端点未知,所以我猜是403回答了。

Cloud Endpoint 对此行为感到沮丧。使用 path_translation: APPEND_PATH_TO_ADDRESS,您认为您的最终呼叫地址将是 /documents/{document_id},但 NO。完整的 openAPI 路径附加到您的后端地址,在您的情况下:/documents/api/documents/{document_id}

这就是端点不存在的原因,您应该有一个 404(而不是 403)。

更多详情,您可以查看this page

注意:我与 Google 团队就此主题有联系,需要一段时间才能更新此行为。