Swagger / OpenAPI规格具有Google Endpoint拒绝的文件上传



我的目标是设置一个简单的API,用于通过Google Endpoints上传。

这是我简化的OpenAPI规范,根据Swagger验证是有效的:

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."

我总是得到这个晦涩难懂的错误消息:

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."
'

我已经用完了关于原因的想法。

有什么建议?

看起来至少需要一些身份验证:https://cloud.google.com/endpoints/docs/openapi/authentication-method

我还认为云端点不支持 type:file ,因此您必须使用 type:string 并使用等效于 curl -x post-f" file_upload=@file.txt" http://myservice.com/endpoint 上传。

我通过将摇摇欲坠的文件复制并粘贴到https://editor.swagger.io/来识别错误并引用https://swagger.io/specification/v2/将其粘贴到https://editor.swagger.io/解决了这一问题。修复错误。

我今天遇到了这个问题并创建了一个repro。我相信您的问题可能在于以下顺序:

in:formdata
名称:Upfile

如果您仍在看到此问题,您可以尝试扭转订单:

名称:Upfile
在:formdata

最新更新