OpenAPITools / OpenAPITools/openapi-generator

[REQ] [JaxRs Resteasy] Handle multipart objects through POJOs thanks to @MultipartForm

Open
#2,293 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Is your feature request related to a problem? Please describe.

I have an api accepting multipart/form-data described like this in yaml:

  requestBody:
    content:
      multipart/form-data:
        encoding:
          picture:
            contentType: application/octet-stream
          pet:
            contentType: application/json
        schema:
          title: AddPetMultipart
          type: object
          required:
            - picture
            - pet
          properties:
            picture:
              description: picture of the new pet
              type: string
              format: binary
            pet:
              $ref: "#/components/schemas/Pet"

When I generate code for JaxRs Resteasy, I get a unused POJO class AddPetMultipart and API looking like:

@POST

@Consumes({ "multipart/form-data" })
@Produces({ "application/json" })
@io.swagger.annotations.ApiOperation(value = "Create a pet", notes = "", response = Void.class, tags={ "pets", })
@io.swagger.annotations.ApiResponses(value = { 
    @io.swagger.annotations.ApiResponse(code = 201, message = "Null response", response = Void.class),
    
    @io.swagger.annotations.ApiResponse(code = 200, message = "unexpected error", response = Error.class) })
public Response createPets(MultipartFormDataInput input,@Context SecurityContext securityContext)
throws NotFoundException {
    return service.createPets(input,securityContext);
}

There is no way of doing bean validation on input based on schema defined in yaml (especially on json part).

Describe the solution you'd like

Resteasy supports POJO mapping for multipart. It should be nice to be able to use it and getting something like

@POST

@Consumes({ "multipart/form-data" })
@Produces({ "application/json" })
@io.swagger.annotations.ApiOperation(value = "Create a pet", notes = "", response = Void.class, tags={ "pets", })
@io.swagger.annotations.ApiResponses(value = { 
    @io.swagger.annotations.ApiResponse(code = 201, message = "Null response", response = Void.class),
    
    @io.swagger.annotations.ApiResponse(code = 200, message = "unexpected error", response = Error.class) })
public Response createPets(@MultipartForm AddPetMultipart input,@Context SecurityContext securityContext)
throws NotFoundException {
    return service.createPets(input,securityContext);
}

and

public class AddPetMultipart   {

private File picture;
private Pet pet = null;

/**
 * picture of the new pet
 **/

@ApiModelProperty(required = true, value = "picture of the new pet")
@FormParam("picture")
@NotNull
public File getPicture() {
  return picture;
}
public void setPicture(File picture) {
  this.picture = picture;
}

/**
 **/

@ApiModelProperty(required = true, value = "")
@FormParam("pet")
@NotNull
public Pet getPet() {
  return pet;
}
public void setPet(Pet pet) {
  this.pet = pet;
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the issue's Resteasy @MultipartForm POJO-mapping reference and compare the generated createPets signature with the unused AddPetMultipart class shown here. Done means multipart endpoints use the generated POJO with @MultipartForm and preserve schema-driven fields and validation, including the JSON pet part; verify the generated output against these examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.