https://kotlinlang.org logo
#http4k
Title
p

Philipp Mayer

10/03/2022, 12:17 PM
Out of curiosity: Did one of you ran already into the following? Compiles fine, but I would like to make sure everything looks nice in the IDE too.
d

dave

10/03/2022, 12:21 PM
That class is generated by the Kapt Kotshi plugin. It will be in
build/generated/source/kapt/main
It should be ok once the IDE has had a chance to catch up - try resyncing your project and clean build?
p

Philipp Mayer

10/03/2022, 12:26 PM
Yeah, that’s what I figured. Already resynced, didn’t help, that’s why I went ahead an asked here. Seems like I didn’t do anything wrong this time.. 🌚 Invalidate caches and restart it is..
d

dave

10/03/2022, 12:27 PM
time for lunch! 😂
p

Philipp Mayer

10/03/2022, 12:29 PM
💯
I’m on a train, even worse!
d

dave

10/03/2022, 12:29 PM
well then I hope you're got power because your fans are going to be busy!
p

Philipp Mayer

10/03/2022, 12:34 PM
Now I know why people are look at me in a specific way!!
Solution: If there’s ever someone who finds this thread, here’s the solution: Read this doc and add the following to your `build.gradle`:
Copy code
kotlin {
  sourceSets {
    main.kotlin.srcDirs += 'build/generated/ksp/main/kotlin'
    test.kotlin.srcDirs += 'build/generated/ksp/test/kotlin'
  }
}
d

dave

10/03/2022, 2:50 PM
ooh. - are you using kotshi with KSP? I was wondering how well that worked. If you could share a gradle file with it working that would be amazing 🙂
p

Philipp Mayer

10/03/2022, 2:51 PM
Yeah, with KSP! I didn’t deploy it yet.. but tests are green. 🌚
There you go: github
I would love to know what problems you faced there - I wanted to write a little bit about going serverless with http4k and kotshi, so other frequent problems would be hugely appreciated!
Here’s the adapter. Pretty vanilla, just lmk what you’re searching for. 😁
d

dave

10/03/2022, 3:02 PM
It has actually just been the moving over from Kapt to KSP with Kotshi. The docs aren't particularly good I've found so having a working project will be mega helpful
p

Philipp Mayer

10/03/2022, 3:04 PM
I’d love to help, but gradle/ksp/everything generated is always such a huge mess
d

dave

10/03/2022, 3:04 PM
Am using the http4k/Moshi/Kapt/Lambda stack for a side-project and can confirm that it works really well in the real world 🙂
(specifically it's the gradle stuff which I've found hard)
p

Philipp Mayer

10/03/2022, 3:06 PM
Look at the gradle file I just posted. It has no structure at all. But I feel like once I want to tidy it up and use one of their 53 APIs I just break the build. Not saying that I would have any clue about build systems, but it can be quite a PITA from time to time..
Okay, just checked the logs. Looks like it works, but also throws (how??):
Copy code
No JsonAdapter for interface org.http4k.core.Body (with no annotations)
for interface org.http4k.core.Body body
for class org.http4k.core.MemoryResponse: java.lang.IllegalArgumentException
java.lang.IllegalArgumentException: No JsonAdapter for interface org.http4k.core.Body (with no annotations)
for interface org.http4k.core.Body body
for class org.http4k.core.MemoryResponse
	at com.squareup.moshi.Moshi$LookupChain.exceptionWithLookupStack(Moshi.java:389)
	at com.squareup.moshi.Moshi.adapter(Moshi.java:158)
	at com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory.create(KotlinJsonAdapter.kt:292)
	at com.squareup.moshi.Moshi.adapter(Moshi.java:146)
	at com.squareup.moshi.Moshi.adapter(Moshi.java:106)
	at com.squareup.moshi.Moshi.adapter(Moshi.java:80)
	at org.http4k.format.ConfigurableMoshi.asFormatString(ConfigurableMoshi.kt:32)
	at org.http4k.serverless.AutoMarshallingFnLoader.invoke$lambda-0(AutoMarshallingFnLoader.kt:19)
	at org.http4k.serverless.AwsLambdaEventFunction.handleRequest(AwsLambdaEventFunction.kt:15)
Caused by: java.lang.IllegalArgumentException: No JsonAdapter for interface org.http4k.core.Body (with no annotations)
	at com.squareup.moshi.Moshi.adapter(Moshi.java:156)
	... 7 more
Just double-checked. This has nothing to do with KSP, looks like it also happened before but I never realized because the expected outcome was always there.
d

dave

10/03/2022, 3:30 PM
I think you need something more like this:
Copy code
internal object CommonConnectJson : ConfigurableMoshi(
    Moshi.Builder()
        .add(KotshiCommonConnectJsonAdapterFactory)
        .asConfigurable()
        .done()
)

@KotshiJsonAdapterFactory
internal interface CommonConnectJsonAdapterFactory : JsonAdapter.Factory
p

Philipp Mayer

10/03/2022, 3:31 PM
I feel like my problem was rather that I was returning the
Response
all the way up to
FnHandler
, which then automatically serialized it. Or tried to.
d

dave

10/03/2022, 3:31 PM
^^ the above works for us.
p

Philipp Mayer

10/03/2022, 5:23 PM
Not sure if
.asConfigurable()
is needed for my use case - I think the problem was that it tried to serialize a
Response
. 🤔 But maybe I’m misinterpreting
asConfigurable()
! Thanks for pointing it out anyway, just added it and will run with it.
56 Views