3

I have following swagger yaml file

/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

My build.gradle file has following entries which generates the code.

// 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 is generating the following method in my API class.

    @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", response = Document.class), 
        @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;
    }

When I try to hit the request, I am always getting UploadRequest object as null although I am passing the values in below format. However, I am getting the FormDataContentDisposition and InputStream correctly populated.

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

Not too sure what's happening in there. Can someone please help me as why the json object is coming as null. Am I missing something here?

ashishb
  • 81
  • 2
  • 11

0 Answers0