Open red010182 opened 1 year ago
/cc @evanchooly (kotlin), @geoand (kotlin)
This is similar to https://github.com/quarkusio/quarkus/issues/19686
I have had problems with @BeanParam
with kotlin too, however I have been able to get it working albeit with a few restrictions.
@red010182 the following should hopefully help
To fix the error you are getting you have to make the type nullable and set the default value to null E.g
data class MyBean(@QueryParam("a") val a: Int? = null)
Which means all of your fields in your bean param have to be nullable and therefore if you wanted to have a required type on your API you would have to do validation yourself to check that.
If you try to run the app with the above change you will then get a different error which will have the following in the stacktrace:
Caused by: javax.enterprise.inject.spi.DeploymentException: No annotations found on fields at 'com.example.MyBean'. Annotations like `@QueryParam` should be used in fields, not in methods
This is because in kotlin when using annotations on a field in a constructor you have to specify the target (you can read more about it here: https://kotlinlang.org/docs/annotations.html#annotation-use-site-targets). To fix this you need to change @QueryParam
to @field:QueryParam
So your data class will now look something like:
data class MyBean(@field:QueryParam("a") val a: Int? = null)
and the application should start up without error.
However if you try to hit your endpoint you will find that you get a 404 Not Found
response.
To fix this issue your field in the bean param has to be mutable. Therefore if you change a
to var
:
data class MyBean(@field:QueryParam("a") var a: Int? = null)
Now when you hit your endpoint it should successfully match your intTest
function and you should get the response you expected
So in summary to use @BeanParam
with kotlin your fields must be:
Most of which are not ideal and mean you have to deal with more validation in the code. I'm not sure if there could be any changes in Quarkus to improve this?
@geoand any ideas if this can be improved? or is this just a restriction of the implementation of @BeanParam
in JAX-RS?
Thanks a lot for the analysis @csh97, that's very helpful.
I totally agree the situation is far from ideal and we'll look into improving it (although I can't make any promised on when that will happen)
or is this just a restriction of the implementation of @BeanParam in JAX-RS?
it's likely a result of how we have implemented it
Thanks a lot for the analysis @csh97, that's very helpful.
I totally agree the situation is far from ideal and we'll look into improving it (although I can't make any promised on when that will happen)
or is this just a restriction of the implementation of @BeanParam in JAX-RS?
it's likely a result of how we have implemented it
Thats great thanks!
For all who run into this tread/issue: I've made it work by just using the annotations inside the bean on the field, so like:
@field:FormParam val foo: String
And it seems to work, even without the nullability and default mentioned above :)
For all who run into this tread/issue: I've made it work by just using the annotations inside the bean on the field, so like:
@field:FormParam val foo: String And it seems to work, even without the nullability and default mentioned above :)
I have tried to replicate, however I am still getting the same behaviour mentioned in my comment above. Could you provide a small example? Also what version of quarkus are you using?
@FroMage remind me please, have made @BeanParam
work with Java records?
Nice!
So it should be possible to make it work for any class that has a single constructor
Everything is always possible, given work :) But yes.
👌
Describe the bug
In resteasy-reactive + kotlin
Expected behavior
Extract the variable
query
as type ofMyBean
.Actual behavior
throw error
How to Reproduce?
quarkus-@BeanParam-kotlin-fail.zip
Output of
uname -a
orver
No response
Output of
java -version
java version "19.0.1" 2022-10-18 Java(TM) SE Runtime Environment (build 19.0.1+10-21) Java HotSpot(TM) 64-Bit Server VM (build 19.0.1+10-21, mixed mode, sharing)
GraalVM version (if different from Java)
No response
Quarkus version or git rev
3.2.4.Final
Build tool (ie. output of
mvnw --version
orgradlew --version
)Apache Maven 3.8.8 (4c87b05d9aedce574290d1acc98575ed5eb6cd39) Maven home: C:\Users\ptrue.m2\wrapper\dists\apache-maven-3.8.8-bin\67c30f74\apache-maven-3.8.8 Java version: 19.0.1, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk-19 Default locale: en_US, platform encoding: UTF-8 OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
Additional information
No response