Skip to content

Commit 04e7da1

Browse files
authored
fix htmx GET routes with path params (#84)
1 parent 7e5829a commit 04e7da1

File tree

5 files changed

+32
-12
lines changed

5 files changed

+32
-12
lines changed

spring-funk-htmx/src/main/kotlin/com/github/wakingrufus/funk/htmx/route/ParamRoute.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ class ParamRoute<CONTROLLER : Any, REQ : Record, RESP : Any>(
3232
beanFactory.getBean<ObjectMapper>()
3333
.convertValue(request.params().toSingleValueMap(), requestClass)
3434
} else {
35-
log.warn { "unsupported content type $contentType" }
36-
null
35+
beanFactory.getBean<ObjectMapper>()
36+
.convertValue(request.pathVariables(), requestClass)
3737
}
38-
val resp = beanFactory.getBean(controllerClass).binding(req!!)
38+
val resp = beanFactory.getBean(controllerClass).binding(req)
3939

4040
val acceptedType = request.headers().accept()
4141
if (MediaType.APPLICATION_JSON.isCompatibleWith(contentType) && acceptedType.any {

test-application/src/main/kotlin/com/github/wakingrufus/funk/example/ExampleApplication.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.github.wakingrufus.funk.htmx.HttpVerb
77
import com.github.wakingrufus.funk.htmx.htmx
88
import com.github.wakingrufus.funk.htmx.hxPost
99
import com.github.wakingrufus.funk.htmx.swap.HxSwapType
10+
import com.github.wakingrufus.funk.htmx.template.htmxTemplate
1011
import com.github.wakingrufus.funk.logging.logging
1112
import com.github.wakingrufus.funk.webmvc.webmvc
1213
import io.github.oshai.kotlinlogging.KotlinLogging
@@ -21,6 +22,14 @@ import org.springframework.web.servlet.function.ServerResponse
2122

2223
const val helloWorldUrl = "/load"
2324

25+
val responseTemplate = htmxTemplate<HelloWorldResponse> {
26+
div {
27+
span {
28+
+it.message
29+
}
30+
}
31+
}
32+
2433
class ExampleApplication : SpringFunkApplication {
2534
private val log = KotlinLogging.logger {}
2635
override fun dsl(): SpringDslContainer.() -> Unit = {
@@ -33,15 +42,6 @@ class ExampleApplication : SpringFunkApplication {
3342
}
3443
htmx {
3544
page("/index") {
36-
37-
route(HttpVerb.POST, helloWorldUrl, ExampleService::sayHello) {
38-
div {
39-
span {
40-
+it.message
41-
}
42-
}
43-
}
44-
4545
initialLoad {
4646
form {
4747
hxPost(helloWorldUrl) {
@@ -61,6 +61,8 @@ class ExampleApplication : SpringFunkApplication {
6161
+"Click Me"
6262
}
6363
}
64+
route(HttpVerb.POST, helloWorldUrl, ExampleService::sayHello, responseTemplate)
65+
route(HttpVerb.GET,"thing/{id}", ExampleService::getThingById, responseTemplate)
6466
}
6567
}
6668
webmvc {

test-application/src/main/kotlin/com/github/wakingrufus/funk/example/ExampleService.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ class ExampleService {
88
HelloWorldResponse("Hello World")
99
}
1010
}
11+
12+
fun getThingById(request: ThingByIdRequest): HelloWorldResponse {
13+
return HelloWorldResponse("Hello ${request.id}")
14+
}
1115
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
package com.github.wakingrufus.funk.example
22

3+
import java.util.UUID
4+
35
@JvmRecord
46
data class HelloWorldRequest(val name: String? = null)
7+
8+
@JvmRecord
9+
data class ThingByIdRequest(val id: UUID)

test-application/src/test/kotlin/com/github/wakingrufus/funk/example/ExampleApplicationTest.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.springframework.http.MediaType
1111
import org.springframework.test.web.reactive.server.WebTestClient
1212
import org.springframework.web.reactive.function.BodyInserters
1313
import java.nio.charset.StandardCharsets
14+
import java.util.UUID
1415

1516

1617
@SpringBootTest(
@@ -58,5 +59,13 @@ class ExampleApplicationTest {
5859
formResponseJson.returnResult(String::class.java).responseBodyContent?.toString(StandardCharsets.UTF_8)
5960
log.info { responseString }
6061
assertThat(responseString).isEqualTo("""{"message":"Hello bob"}""")
62+
63+
val pathParamResponse = client.get()
64+
.uri("/thing/${UUID.randomUUID()}")
65+
.accept(MediaType.TEXT_HTML)
66+
.exchange()
67+
log.info {
68+
pathParamResponse.returnResult(String::class.java).responseBodyContent?.toString(StandardCharsets.UTF_8)
69+
}
6170
}
6271
}

0 commit comments

Comments
 (0)