passing path parameter in google endpoints to backend not working

余生颓废 提交于 2021-02-08 20:52:21

问题


My setup contains google-endpoints with google-cloud-functions as my backend.

Google endpoints is defined with the following 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)

This works with the POST endpoint without any problems.

The problem is with the GET REST endpoint where the path variable is not passed correctly to the backend.

As in https://cloud.google.com/endpoints/docs/openapi/openapi-extensions I tried to add the x-google-backend parameter as in the swagger api above. (path_translation: APPEND_PATH_TO_ADDRESS).

However this does not work. I get an Unauthorized Error (403) as the cloud function is not hit by the endpoints frontend.

Currently I use an ugly workaround without the path_translation parameter which translates the google endpoints path variable to a query parameter in the cloud function backend with the same name. I.e. in the backend the url /documents?document_id=xyz is called.

(What I try to achieve is to pass the call with the backend url /documents/{document_id})

Does anyone know how to configure path based parameters correctly so that they are passed correctly to the cloud function backend?

Thank you in advance.

Regards, Sebastian


回答1:


TL;DR: I assume that your 403 error isn't the correct error. It should be a 404, but because the endpoint is unknown, I guess that 403 is answered.

Cloud Endpoint is frustrating about this behavior. With the path_translation: APPEND_PATH_TO_ADDRESS, you think that your final called address will be /documents/{document_id}, but NO. The full openAPI path is append to your backend address, in your case: /documents/api/documents/{document_id}

That's why the endpoint doesn't exist and you should have a 404 (and not a 403).

For more details, you can have a look to this page.

Note: I'm in relation with Google team on this topic, and it will take time before having an update on this behavior.



来源:https://stackoverflow.com/questions/60711968/passing-path-parameter-in-google-endpoints-to-backend-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!