-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Kotlin unable to inherit type for WebTestClient#BodySpec [SPR-15692] #20251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Sébastien Deleuze commented Good catch, I have raised the point on Kotlin issue tracker (see this KT-5464 comment) cc Rossen Stoyanchev. |
Mikhail Konovalov commented I'm not sure the problem is with Kotlin compiler. |
Mikhail Konovalov commented Sébastien Deleuze, I've used your approach you've mentioned in KT-5464 and it started to compile @Test
fun `test get 3`() {
val bar: BodySpec<Person, *> = client.get().uri("/person/27").exchange()
.expectBody(Person::class.java).consumeWith { person -> Assert.assertTrue(true) } // compile but leads to NPE
} But if I generate Kotlin bytecode and then decompile it I get @Test
public final void test_get_3/* $FF was: test get 3*/() {
this.client.get().uri("/person/27", new Object[0]).exchange().expectBody(Person.class).consumeWith((Consumer)null.INSTANCE);
throw null;
} (also updated the gist with new case ) |
Sébastien Deleuze commented Latest comment seems to show this is an issue on Kotlin side that could be fixed in an upcoming Kotlin major version. They also suggest a workaround |
Mikhail Konovalov commented Looks like <Noting?> fixes NPE but call chain will be finished on this. interface BodySpec<B, S extends BodySpec<B, S>> |
Sébastien Deleuze commented Not sure, do you have a alternative proposal? It seems they plan to fix this in Kotlin 1.2 or 1.3, so without any concrete proposal / PR to discuss with Rossen Stoyanchev, I tend to think we should not enforce artificially another design for a temporary Kotlin issue. |
Mikhail Konovalov commented I agree not to break everything for Kotlin issue. BodySpec<B, ?> It looks like we create strict recursive structure in interface and then relax it with wildcards. |
Sébastien Deleuze commented KT-5464 is expected to be fixed for Kotlin 1.3, and is likely to fix the issue described here, so I prefer keep things as they are if Kotlin is the only relevant reason. Please vote for KT-5464 in order to make sure it will remain high priority. |
Sébastien Deleuze commented I am reopening this issue since it seems we can provide a workaround for this very annoying issue by providing a Kotlin extension calling |
Sébastien Deleuze commented Resolved by updating the |
Sébastien Deleuze commented Side note: be aware of the lack of autocomplete on |
Is there any example how to use interface BodySpec<B, S extends BodySpec<B, S>> using WebTestClient with Kotlin similar to expectBodyList? |
Please use stackoverflow for such question, thanks. |
Hey,
Error : Type expected
Error : @sdeleuze can you help me in this . |
Mikhail Konovalov opened SPR-15692 and commented
It seems that due to recursive generics in
BodySpec
interfaceand due to
expectBody
method returnsWebTestClient
cannot be used in Kotlin.Kotlin inherits the result of
.expectBody(Person::class.java)
asBodySpec<Person, *>
and thus the following methods in chain cannot be constructed due to the following error:And it applies only
Nothing
as a type parameter.But in this case generated bytecode contains the following line
Example:
If you work with list the situation is a bit better - Kotlin still cannot inherit type param automatically but you can specify it explicitly due to method
expectBodyList
in interfaceListBodySpec
doesn't return wildcardsExample:
Full example with java and kotlin can be found here.
Tests in java works well in these cases.
Affects: 5.0 RC2
Reference URL: https://gist.github.com/mskonovalov/42761bbc548e92c2af16c40cffcfcaf3
Issue Links:
Referenced from: commits 91c8b62, 568a0b5
0 votes, 5 watchers
The text was updated successfully, but these errors were encountered: