DanielZ
06/22/2022, 2:53 PMOpenAPI
and Filter
.
I have something like:
routes(
"/" bind contract {
…
}.withFilter(FilterWhichWouldLikeToUseALenseInside())
Sadly, I can’t use a lens inside of FilterWhichWouldLikeToUseALenseInside()
?dave
06/22/2022, 3:36 PMDanielZ
06/22/2022, 6:35 PMdave
06/22/2022, 6:36 PMDanielZ
06/22/2022, 6:36 PMval nameLens = Path.of("name")
val trackingIdLens = Header.required("tracking-id")
val app = routes(
"/status" bind GET to { Response(OK) },
"/" bind contract {
renderer = OpenApi3(ApiInfo("My Application", "Version 1.0"), Jackson)
descriptionPath = "/swagger.json"
routes += customerEndpoint()
}.withFilter(
ServerFilters.CatchAll()
.then(LogRequestName())
.then(ServerFilters.CatchLensFailure { Response(BAD_REQUEST) })
)
)
private fun customerEndpoint() = ("/v1/customer/name" / nameLens meta {
summary = "..."
description = "..."
headers += trackingIdLens
} bindContract GET to { name: String ->
{ request: Request ->
Response(OK).body(name)
}
})
object LogRequestName {
operator fun invoke(): Filter {
return RequestFilters.Tap { request: Request ->
println("Filter: ${request.something(nameLens)}")
}
}
}
private fun Request.something(nameLens: BiDiPathLens<String>) = nameLens(this)
fun main() {
println(app(Request(GET, "/v1/customer/name/marie").header("tracking-id", "26")))
}
which works fine … I don’t get why we had an issue today on two different machines 😄dave
06/22/2022, 6:41 PMDanielZ
06/22/2022, 6:44 PMdave
06/22/2022, 6:48 PMDanielZ
06/22/2022, 6:48 PMval nameLens = Path.of("name")
val trackingIdLens = Header.required("tracking-id")
val app = routes(
"/status" bind GET to { Response(OK) },
"/" bind contract {
renderer = OpenApi3(ApiInfo("My Application", "Version 1.0"), Jackson)
descriptionPath = "/swagger.json"
routes += customerEndpoint()
}.withFilter(
ServerFilters.CatchAll()
.then(LogRequestName())
.then(ServerFilters.CatchLensFailure { Response(BAD_REQUEST) })
)
)
private fun customerEndpoint() = ("/v1/customer/name" / nameLens meta {
summary = "..."
description = "..."
headers += trackingIdLens
} bindContract GET to { name: String ->
{ request: Request ->
Response(OK).body(name)
}
})
object LogRequestName {
operator fun invoke(): Filter {
return RequestFilters.Tap { request: Request ->
println("Filter: ${request.something(nameLens)}")
}
}
}
private fun Request.something(nameLens: BiDiPathLens<String>) = nameLens(this)
fun main() {
println(nameLens(Request(GET, "/v1/customer/name/marie")))
println(app(Request(GET, "/v1/customer/name/marie").header("tracking-id", "6ed45217-935f-4794-94a5-30b2cfbe181b")))
}
Output:
Exception in thread "main" org.http4k.lens.LensFailure: path 'name' must be string
at org.http4k.lens.Lens.invoke(lens.kt:19)
at MyApplicationKt.main(MyApplication.kt:59)
at MyApplicationKt.main(MyApplication.kt)
Caused by: java.lang.IllegalStateException: Request was not routed, so no uri-template present
at org.http4k.routing.ExtensionsKt.path(extensions.kt:20)
at org.http4k.lens.PathLens$1.invoke(path.kt:18)
at org.http4k.lens.PathLens$1.invoke(path.kt:17)
at org.http4k.lens.Lens.invoke(lens.kt:15)
... 2 more
Any ideas, what I’m doing wrong?dave
06/23/2022, 7:44 AMDanielZ
06/23/2022, 7:48 AMdave
06/23/2022, 7:50 AMDanielZ
06/23/2022, 7:51 AM