openapigoogle-cloud-runswagger-2.0google-cloud-api-gatewaygoogle-api-gateway

Google API Gateway OpenApi Swaagger 2.0 to CloudRun Parameter configured for Path turns out in query instead of path


I'm testing an API Gateway setup on Google Cloud to access specific endpoints on a service deployed on Cloud Run. I'm following the steps shown here. We need to authenticate using an API Key, so the API Key specific configuration that went into the API Gateway config was picked from this documentation.

The API Gateway config is as shown below:

# api_gateway_config.yaml
swagger: '2.0'
info:
  title: myappapi
  description: API with Cloudrun Backend
  version: 1.0.0
schemes:
  - https
produces:
  - application/json
paths:
  /:
    get:
      summary: Greet a User from service
      operationId: hello
      x-google-backend:
        address: https://myappapi-asldfjoiewjfv-uc.a.run.app/
      security:
      - api_key: []
      responses:
        '200':
          description: A successful response
          schema:
            type: string
  /reports/results/{id}:
    get:
      summary: Get Report Results for specified report id
      operationId: GetReportResults
      x-google-backend:
        address: https://myappapi-asldfjoiewjfv-uc.a.run.app/v1/reports/results/{id}
      parameters:
        - in: path
          name: id
          required: true
          type: integer
      security:
      - api_key: []
      responses:
        '200':
          description: A successful response
          schema:
            type: string

securityDefinitions:
  # This section configures basic authentication with an API Key.
  api_key:
    type: "apiKey"
    name: "key"
    in: "query"

For a sample call to the /reports/results endpoint as http://myappapi/reports/results/1, the expectation is for calls to get converted to https://myappapi-asldfjoiewjfv-uc.a.run.app/v1/reports/results/1?key=MyAPIKeyHere. But instead they turn out as https://myappapi-asldfjoiewjfv-uc.a.run.app/v1/reports/results?key=MyAPIKeyHere&id=1

Is there a way to get the API calls go as https://myappapi-asldfjoiewjfv-uc.a.run.app/v1/reports/results/1?key=MyAPIKeyHere ?

Thanks in Advance!


Solution

  • As mentioned in this documentation

    Set path_translation as part of setting x-google-backend:

       x-google-backend:
         address: https://GCP_REGION-PROJECT_ID.cloudfunctions.net/hello
         path_translation: [ APPEND_PATH_TO_ADDRESS | CONSTANT_ADDRESS ]
    

    The default value of path_translation depends on where you set x-google->backend in your OpenAPI spec:

    • When x-google-backend is used at the top level of the OpenAPI specification, path_translation defaults to APPEND_PATH_TO_ADDRESS.
    • When x-google-backend is used at the operation level of the OpenAPI specification, path_translation defaults to CONSTANT_ADDRESS.

    For more details on path translation, please see the Understanding path translation section. You can also check this stackoverflow thread.