I'm using Java 11 and want to create an OpenAPI 3 request to upload multipart form data (a file and some other fields). I have this in my openApi yml file ...
/myobjects/:
post:
tags:
- my-objects
summary: Adding my object
operationId: addmyobject
description: Creates a new my object.
requestBody:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/MyObjectDTO'
required: true
...
MyObjectDTO:
type: object
properties:
myId:
type: integer
format: int
readOnly: true
name:
type: string
maxLength: 100
required: true
example: myRequest
...
myFile:
type: string
format: binary
required:
- name
I'm using the following configuraiton for Swagger code gen (Maven) in my pom.xml file ...
<plugin>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.17</version>
...
<configuration>
...
<generateApis>true</generateApis>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<generateApiDocumentation>true</generateApiDocumentation>
<generateModels>true</generateModels>
<generateSupportingFiles>false</generateSupportingFiles>
<languageSpecificPrimitives>true</languageSpecificPrimitives>
<importMappings>
...
</importMappings>
<configOptions>
<interfaceOnly>true</interfaceOnly>
<java8>false</java8>
<dateLibrary>java8</dateLibrary>
<sourceFolder>.</sourceFolder>
<throwsException>true</throwsException>
<useTags>true</useTags>
</configOptions>
</configuration>
The problem is when my API call generates, not only are all the fields from my DTO included, but they are all marked as required, despite the fact I have only specified that "name" is a required field in my DTO.
@Operation(summary = "Adding my object", description = "Creates a new my object.", tags={ "my-objects" })
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "OK", content = @Content(schema = @Schema(implementation = ResponseData.class))) })
@RequestMapping(value = "/myobjects/",
produces = { "application/json" },
consumes = { "multipart/form-data" },
method = RequestMethod.POST)
default ResponseEntity<ResponseData> addmyobject(
@Parameter(description = "", required=true) @RequestParam(value="myId", required=true) Integer myId
,
@Parameter(description = "", required=true) @RequestParam(value="name", required=true) String name
,
...
,
@Parameter(description = "file detail") @Valid @RequestPart("file") MultipartFile myFile
) throws Exception {
Is there a way to rewrite my yml, or configure code generation so that I can specify which fields are required and which I want included in my multi-part upload call?