OpenAPITools / OpenAPITools/openapi-generator
[BUG] [jaxrs-spec] FormParam used instead of FormDataParam for "multipart/form-data"
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
Description
jaxrs-spec generator adds javax.ws.rs.FormParam annotation to parameters when content type is set to multipart/form-data
It causes the following error with Jersey (jaxrs reference implementation) for primitive types:
java.lang.IllegalStateException: The @FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded
at org.glassfish.jersey.server.internal.inject.FormParamValueParamProvider$FormParamValueProvider.ensureValidRequest(FormParamValueParamProvider.java:107)
When javax.ws.rs.FormParam is applied to a Java bean, Jersey throws a different error:
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public abstract javax.ws.rs.core.Response com.foo.FooApi.addFoo(com.foo.FooDto) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=interface com.foo.FooApi handlerConstructors=[]}, definitionMethod=public abstract javax.ws.rs.core.Response com.foo.FooApi.addFoo(com.foo.FooDto), parameters=[Parameter [type=class com.foo.FooDto, source=metadata, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:371) ~[jersey-server-2.29.1.jar:na]
openapi-generator version
openapi-generator-maven-plugin 4.3.0 with jaxrs-spec generator
OpenAPI declaration file content or url
For simple parameter:
requestBody:
content:
multipart/form-data:
schema:
properties:
foo:
type: integer
format: int32
For bean parameter:
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
metadata:
$ref: '#/components/schemas/FooDto'
Command line used for generation
openapi-generator-maven-plugin with jaxrs-spec generator
Steps to reproduce
- Generate API with openapi-generator-maven-plugin and jaxrs-spec generator
- Implement the API
- Add org.springframework.boot:spring-boot-starter-jersey and org.glassfish.jersey.media:jersey-media-multipart to your project
- In JerseyConfig add register(MultiPartFeature.class);
- Try invoking generated endpoints. It will return the error described above
- (optional) Manually change the annotation in generated classes to FormDataParam and check that it works.
Related issues/PRs
Suggest a fix
I checked that when javax.ws.rs.FormParam is replaced with org.glassfish.jersey.media.multipart.FormDataParam, everything works fine.
However, org.glassfish.jersey.media.multipart.FormDataParam is Jersey specific.
Maybe there should be a configuration option to choose which annotation should be used for multi part parameters.
More about Multipart in Jersey:
https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/media.html#multipart
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the jaxrs-spec generator's handling of multipart/form-data request parameters and reproduce the issue using the provided OpenAPI YAML and Jersey setup. Compare generated FormParam usage with the manually verified FormDataParam behavior; done should mean multipart endpoints work for both primitive and bean parameters without breaking non-Jersey behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100