swagger-php icon indicating copy to clipboard operation
swagger-php copied to clipboard

Fully working File Upload

Open imonoid opened this issue 6 years ago • 6 comments

Hello. I can't seem to make the PetController File Upload to display and work in Swagger. Can somebody provide a fully working example for v3? I need the ability to upload CSV, but currently stuck.

Thanks!

imonoid avatar Nov 03 '20 17:11 imonoid

Have you maybe checked #816 and #666? Those previous issues might have some hints for you.

Could you otherwise please describe where you are stuck? Best is to provide fully working (example) code.

Doqnach avatar Nov 03 '20 19:11 Doqnach

Hi, my issue is actually I am unable to generate the upload field. Whenever I click on Try Button, I only see a large textarea with a json string, instead of an actualy upload field.

image

This is my code: /** * @OA\Post( * description="Post regions by file.", * path="/api/v2/regions/upload_rf", * @OA\Response( * response="200", * description="Successful CSV upload response. Once an upload is successfull, processing will be handled in the backend.", * content={ * @OA\MediaType( * mediaType="application/json", * @OA\Schema( * @OA\Property( * property="status", * type="string", * example="true", * description="Response status" * ), * @OA\Property( * property="response", * type="array", * @OA\Items( * @OA\Property(property="id", type="integer", example="15", description="The File Id of the CSV.") * ) * ) * ) * ) * } * ), * @OA\Response(response="404", description="Upload failed"), * operationId="upload_rf", * @OA\RequestBody( * required=true, * @OA\MediaType( * mediaType="multipart/form-data", * @OA\Schema( * required={"userfile"}, * @OA\Property( * description="The CSV File", * property="userfile", * type="string", * format="binary" * ) * ) * ) * ), * @OA\Parameter( * name="ACCESS-KEY", * in="header", * description="Your API access key", * required=true, * @OA\Schema( * type="string" * ) * ) * ) */

imonoid avatar Nov 03 '20 19:11 imonoid

This is because Swagger-UI only supports the file-upload field for media-type "multipart/form-data".

See #816 for more details about this. It describes exactly this problem you are having and provides a solution.

PS: use a code block (using "```") to provide a multiline code example, not the single-` inline code markup.

Doqnach avatar Nov 03 '20 20:11 Doqnach

My old sample with Swagger-UI

/**
 * @OA\Post(
 *  path="/v1/media/upload",
 *  summary="Upload document",
 *  description="",
 *  tags={"Media"},
 *  @OA\Parameter(
 *      name="Content-Type", in="header",
 *      description="Mime type of the document",
 *      @OA\Schema(type="string")
 *  ),
 *  @OA\Parameter(
 *      name="Content-Length", in="header",
 *      description="Size of the document in bytes",
 *      @OA\Schema(type="string")
 *  ),
 *  @OA\Parameter(
 *      name="Content-Disposition", in="header",
 *      description="Set the filename of the document ( inline; filename=""name.ext""; name=""label_of_file""; )",
 *      @OA\Schema(type="string")
 *  ),
 *  @OA\RequestBody(
 *      required=true,
 *      @OA\MediaType(
 *          mediaType="application/octet-stream",
 *          @OA\Schema(
 *              @OA\Property(
 *                  description="Binary content of file",
 *                  property="content",
 *                  type="string",
 *                  format="binary",
 *              ),
 *              required={"content"}
 *          )
 *      )
 *  ),
 *  @OA\Response(
 *      response=200, description="Success",
 *      @OA\Schema(type="string")
 *  ),
 *  @OA\Response(
 *      response=400, description="Bad Request",
 *      @OA\JsonContent(ref="#/components/schemas/Error")
 *  ),
 * )
 */

oxomichael avatar Feb 17 '21 22:02 oxomichael

Looks like this question is answered - closing.

DerManoMann avatar Mar 12 '21 00:03 DerManoMann

This exact example don't work for me. The openapi don't generate the input file and I have a message Example values are not available for application/octet-stream media types.

mvnp avatar Jan 18 '23 01:01 mvnp