微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从 swagger-codegen v3 为 multipart-form-data 生成的对象为空

如何解决从 swagger-codegen v3 为 multipart-form-data 生成的对象为空

我有以下 swagger yaml 文件

/tasks/{id}/documents/files/data:
    post:
      tags:
        - task-documents
      summary: Upload document
      description: Upload document
      operationId: uploadDocument
      parameters:
        - name: id
          in: path
          description: ID
          required: true
          schema:
            type: string
      requestBody:
        content:
          multipart/form-data:
            schema:
              required:
                - json
                - upfile
              type: object
              properties:
                upfile:
                  type: string
                  description: Document Content InputStream
                  format: binary
                json:
                  description: Document Metadata
                  $ref: "#/components/schemas/UploadRequest"
        required: true
      responses:
        200:
          description: successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Document'
        401:
          description: Unauthorized
          content: {}
        500:
          description: Internal Server Error
          content: {}
components:
  schemas:
     UploadRequest:
      type: object
      properties:
        documentName:
          type: string
          description: File Name of Document
        contentType:
          type: string
          description: Mime type of document file
        description:
          type: string
          description: Description for document
      required:
        - contentType

我的 build.gradle 文件具有以下生成代码的条目。

// Swagger
    compile "io.swagger.codegen.v3:swagger-codegen:${versions.swaggerCodegenVersion}"
    swaggerCodegen "io.swagger.codegen.v3:swagger-codegen-cli:${versions.swaggerCodegenVersion}"
    implementation "io.swagger:swagger-annotations:${versions.swaggerVersion}"
    implementation group: 'io.swagger.codegen.v3',name: 'swagger-codegen-generators',version: '1.0.23'

swagger-codegen-v3 在我的 API 类中生成以下方法

    @POST
    @Path("/{id}/documents/files/data")
    @Consumes({ "multipart/form-data" })
    @Produces({ "application/json" })
    @ApiOperation(value = "Upload document",notes = "Upload document",response = Document.class,authorizations = {
        @Authorization(value = "cut_basic"),@Authorization(value = "cut_oauth",scopes = {
        @AuthorizationScope(scope = "",description = "")})},tags={ "documents",})
    @ApiResponses(value = { 
        @ApiResponse(code = 200,message = "successful operation",response = Document.class),@ApiResponse(code = 401,message = "Unauthorized",@ApiResponse(code = 500,message = "Internal Server Error",response = Document.class)})
    public Response uploadDocument(@Context UriInfo uriInfo,@FormDataParam("upfile") InputStream upfileInputStream,@FormDataParam("upfile") FormDataContentdisposition upfileDetail,@Parameter(description = "",required=true)@FormDataParam("json")  UploadRequest json,@Parameter(description = "ID",required=true) @PathParam("id") String id,@Context SecurityContext securityContext) throws NotFoundException {

        LOG.debug("Upload document : Upload document");
        LOG.debug( "upfile :" + upfile + "," +  "json :" + json + "," +  "id :" + id + "," +  ";");
        delegate.setProviders(providers);
        Response response = delegate.uploadDocument(uriInfo,upfileInputStream,upfileDetail,json,id,securityContext);

        LOG.debug("Exiting - uploadDocument");
        return response;
    }

当我尝试点击请求时,尽管我以以下格式传递值,但我总是将 UploadRequest 对象设为 null。但是,我正确填充了 FormDataContentdisposition 和 InputStream。

--Boundary_123456789_123456789
Content_transfer-encoding: binary
Content-disposition: form-data; name="upfile"; filename="test.txt"

I am a test file!!!

--Boundary_123456789_123456789
Content-disposition: form-data; name="json"
Content-Type: application/json
{
  "documentName": "string","contentType": "string","description": "string"
}

--Boundary_123456789_123456789--

不太清楚那里发生了什么。有人可以帮助我解释为什么 json 对象为空。我在这里遗漏了什么吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。