So I am having a very frustrating problem that I have been trying to solve for 2 days. I am not able to create a second version of our API because in order to call my cloud run endpoints cors needs to be set in GCP Cloud Endpoints like so:
x-google-endpoints:
- name: gateway-url
allowCors: true
However I cannot deploy this to two separate endpoint files, for example:
Version 1
swagger: '2.0'
host: gateway-url
x-google-endpoints:
- name: gateway-url
allowCors: true
info:
title: Title
description: Description
version: 1.0.0
schemes:
- https
produces:
- application/json
consumes:
- application/json
basePath: /v1
Version 2
swagger: '2.0'
host: gateway-url
x-google-endpoints:
- name: gateway-url
allowCors: true
info:
title: Title
description: Description
version: 1.0.0
schemes:
- https
produces:
- application/json
consumes:
- application/json
basePath: /v2
As you can see above I have 2 gateway files and I create a different gateway file for each version. However when I create anything more than a single cloud endpoints file I get the following error:
OpenAPI spec is invalid. Multiple endpoint entries are defined in the extension 'x-google-endpoints'. At most one entry is allowed.
I have tried many different things including taking the x-google-endpoints code out of my Endpoints file but when I do I lose control over CORS and can no longer handle it in cloud run.
My original cloud run file looked like this:
const express = require('express');
const router = express.Router();
const routes = require('./routes');
const cors = require('cors');
const corsOptions = {
origin: '*',
}
router.options('/', cors(corsOptions));
...
This did not work. Even after completely removing CORS from my Cloud Run service and trying to manually handle it like this:
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type,Authorization');
if (req.method === 'OPTIONS') {
return res.status(204).send('');
}
next();
});
I was still not able to call the endpoint. I have even tried adding options requests to the cloud endpoints YAML file and this still does not work.
options:
summary: CORS Preflight
description: Handle CORS preflight requests
operationId: cors_preflight
x-google-backend:
address: https://my-cloud-run-service
responses:
'204':
description: No Content
So my question is, is there any way to either:
- Handle CORS without using x-google-endpoints in the Endpoints YAML file?
- Handle CORS in my cloud run service without having to set it in Endpoints?
- Something else that Im missing that will easily get me past this?
BTW, I do know that a similar question was asked in 2019 here, But it was never really answered.
Using x-google-endpoints OpenAPI extension with multiple API versions in Cloud Endpoints
I cannot see versioning an API to be a niche use case and for this not to be handled is pretty counterintuitive on Googles side, so Im sure I have to be missing something. Any help with this would be greatly appreciated. Thanks.
3