Swagger / OpenAPI spec featuring file upload rejected by Google Endpoints
Question:
My goal is to set up a simple API for uploading a file via Google Endpoints.
This is my simplified OpenAPI specification which is valid according to Swagger validation:
swagger: "2.0"
info:
title: "JSON Ingester"
description: "Receive JSON files, transform and load them."
version: "1.0.0"
host: "project-id.appspot.com"
schemes:
- "https"
paths:
/uploadFile:
post:
operationId: uploadFile
consumes:
- multipart/form-data
parameters:
-
in: formData
name: upfile
type: file
description: file
responses:
200:
description: "File uploaded."
400:
description: "Error during file upload."
I always end up with this obscure error message:
user@cloudshell:~/google-cloud-json-ingester (project-id)$ gcloud endpoints services deploy ./openapi.yaml
ERROR: (gcloud.endpoints.services.deploy) INVALID_ARGUMENT: Cannot convert to service config.
'location: "openapi.yaml: Operation 'post' in path '/uploadFile'"
message: "Operation does not require an API key; callers may invoke the method without specifying an associated API-consuming project. To enable API key all the SecurityRequirement Objects (https://gi
thub.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#security-requirement-object) inside security definition must reference at least one SecurityDefinition of type : 'apiKey'."
location: "unknown location"
kind: ERROR
message: "http: repeated message field 'google.protobuf.Struct.fields' referred to by message 'UploadFileRequest' cannot be mapped as an HTTP parameter."
location: "unknown location"
kind: ERROR
message: "http: cyclic message field 'google.protobuf.Struct.FieldsEntry.value' referred to by message 'UploadFileRequest' in method 'method 1.project_id_appspot_com.UploadFile' cannot
be mapped as an HTTP parameter."
location: "unknown location"
kind: ERROR
message: "http: cyclic message field 'google.protobuf.ListValue.values' referred to by message 'UploadFileRequest' in method 'method 1.project_id_appspot_com.UploadFile' cannot be mapp
ed as an HTTP parameter."
'
I’ve run out of ideas as to what might be the cause.
Any suggestions?
Answers:
Looks like it requires at least some authentication: https://cloud.google.com/endpoints/docs/openapi/authentication-method
I also think that Cloud Endpoints don’t support type: file, so you have to use type: string and use equivalent to curl -X POST -F “[email protected]” http://myservice.com/endpoint to upload.
I ran into this problem today and created a repro. I believe that your issue likely lies in the order of:
in: formData
name: upfile
If you are still seeing this issue, can you please try reversing the order:
name: upfile
in: formData
I solved this by copying and pasting my swagger file into https://editor.swagger.io/ to identify bugs and referencing https://swagger.io/specification/v2/ to fix the errors.
My goal is to set up a simple API for uploading a file via Google Endpoints.
This is my simplified OpenAPI specification which is valid according to Swagger validation:
swagger: "2.0"
info:
title: "JSON Ingester"
description: "Receive JSON files, transform and load them."
version: "1.0.0"
host: "project-id.appspot.com"
schemes:
- "https"
paths:
/uploadFile:
post:
operationId: uploadFile
consumes:
- multipart/form-data
parameters:
-
in: formData
name: upfile
type: file
description: file
responses:
200:
description: "File uploaded."
400:
description: "Error during file upload."
I always end up with this obscure error message:
user@cloudshell:~/google-cloud-json-ingester (project-id)$ gcloud endpoints services deploy ./openapi.yaml
ERROR: (gcloud.endpoints.services.deploy) INVALID_ARGUMENT: Cannot convert to service config.
'location: "openapi.yaml: Operation 'post' in path '/uploadFile'"
message: "Operation does not require an API key; callers may invoke the method without specifying an associated API-consuming project. To enable API key all the SecurityRequirement Objects (https://gi
thub.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#security-requirement-object) inside security definition must reference at least one SecurityDefinition of type : 'apiKey'."
location: "unknown location"
kind: ERROR
message: "http: repeated message field 'google.protobuf.Struct.fields' referred to by message 'UploadFileRequest' cannot be mapped as an HTTP parameter."
location: "unknown location"
kind: ERROR
message: "http: cyclic message field 'google.protobuf.Struct.FieldsEntry.value' referred to by message 'UploadFileRequest' in method 'method 1.project_id_appspot_com.UploadFile' cannot
be mapped as an HTTP parameter."
location: "unknown location"
kind: ERROR
message: "http: cyclic message field 'google.protobuf.ListValue.values' referred to by message 'UploadFileRequest' in method 'method 1.project_id_appspot_com.UploadFile' cannot be mapp
ed as an HTTP parameter."
'
I’ve run out of ideas as to what might be the cause.
Any suggestions?
Looks like it requires at least some authentication: https://cloud.google.com/endpoints/docs/openapi/authentication-method
I also think that Cloud Endpoints don’t support type: file, so you have to use type: string and use equivalent to curl -X POST -F “[email protected]” http://myservice.com/endpoint to upload.
I ran into this problem today and created a repro. I believe that your issue likely lies in the order of:
in: formData
name: upfile
If you are still seeing this issue, can you please try reversing the order:
name: upfile
in: formData
I solved this by copying and pasting my swagger file into https://editor.swagger.io/ to identify bugs and referencing https://swagger.io/specification/v2/ to fix the errors.