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

Seb*_*ees 4 google-cloud-endpoints google-cloud-platform google-cloud-functions

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

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)
Run Code Online (Sandbox Code Playgroud)

这适用于 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 端点路径变量转换为云函数后端中具有相同名称的查询参数。即在后端/documents?document_id=xyz调用url 。

(我试图实现的是使用后端 url 传递调用/documents/{document_id}

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

先感谢您。

问候, 塞巴斯蒂安

gui*_*ere 6

TL;DR:我假设您的 403 错误不是正确的错误。它应该是 404,但因为端点未知,我猜 403 得到了回答。

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

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

有关更多详细信息,您可以查看此页面

注意:我与 Google 团队就此主题保持联系,并且需要时间才能对此行为进行更新。