Reputation: 384
I am using the openapi-generator to create a multipart/form-data. In an ideal situation I should be able to upload a file, and specify in the options what should happen with the file.
I would like the options to be an object. For one or another reason, this does not seem to work. The openapi-generator generates the API interface, etc, but it does not generate the model for the options object.
I can specify the options individually, but I prefer the options to be an object, with the necessary model to it. I believe this provides a more structured way to deal with the options.
My yaml file looks like this (I specified what works and what doesn't work):
/fileuploadwithoptions:
post:
summary: Upload a file and processes it according to the options specified.
requestBody:
content:
multipart/form-data:
schema:
required:
- file
type: object
properties:
file:
type: string
format: binary
option1: <-- this works
type: string
description: A descriptions for option 1.
options: <-- this does not work
#type: application/json
type: object
description: The options.
properties:
option1:
type: string
description: A descriptions for option 1.
option2:
type: string
description: A descriptions for option 2.
encoding:
file:
contentType: application/octet-stream
This generates the following API:
/**
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech) (6.0.1).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package com.teradact.tokenizerplusserver.api;
import com.teradact.tokenizerplusserver.model.FileuploadwithoptionsPostRequestOptions;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Generated;
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2022-10-11T08:11:04.909499+02:00[Europe/Brussels]")
@Validated
@Tag(name = "fileuploadwithoptions", description = "the fileuploadwithoptions API")
public interface FileuploadwithoptionsApi {
default Optional<NativeWebRequest> getRequest() {
return Optional.empty();
}
/**
* POST /fileuploadwithoptions : Upload a file and processes it according to the options specified.
*
* @param file (required)
* @param option1 A descriptions for option 1. (optional)
* @param options (optional)
* @return The file. (status code 200)
* or bad input parameter (status code 400)
*/
@Operation(
operationId = "fileuploadwithoptionsPost",
summary = "Upload a file and processes it according to the options specified.",
responses = {
@ApiResponse(responseCode = "200", description = "The processed file.", content = {
@Content(mediaType = "application/octet-stream", schema = @Schema(implementation = org.springframework.core.io.Resource.class))
}),
@ApiResponse(responseCode = "400", description = "bad input parameter")
}
)
@RequestMapping(
method = RequestMethod.POST,
value = "/fileuploadwithoptions",
produces = { "application/octet-stream" },
consumes = { "multipart/form-data" }
)
default ResponseEntity<org.springframework.core.io.Resource> fileuploadwithoptionsPost(
@Parameter(name = "file", description = "", required = true) @RequestPart(value = "file", required = true) MultipartFile file,
@Parameter(name = "option1", description = "A descriptions for option 1.") @Valid @RequestParam(value = "option1", required = false) String option1,
@Parameter(name = "options", description = "") @Valid @RequestParam(value = "options", required = false) FileuploadwithoptionsPostRequestOptions options
) {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
}
This gives however the following error:
"Cannot resolve symbol 'FileuploadwithoptionsPostRequestOptions", since the model for the object is simply not created.
Thanks in advance for pointing out where I am wrong!
Upvotes: 3
Views: 9141
Reputation: 36
The interface has the name 'FileuploadwithoptionsApi', YAML file has /fileuploadwithoptions and the response entity has name fileuploadwithoptionsPost but the RequestParam has object 'FileuploadwithoptionsPostRequestOptions' is it because of this it cannot resolve the symbol.
@Parameter(name = "options", description = "") @Valid @RequestParam(value = "options", required = false) FileuploadwithoptionsPostRequestOptions options)
Is the object 'FileuploadwithoptionsPostRequestOptions' instantiated or does it have a similar class or interface matching the object present in @Request Param.
Ok, you have imported the required class for the object try using @RequestPart as you have used for file. Try if it works.
Upvotes: 0