https://kotlinlang.org logo
Join the conversationJoin Slack
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by Linen
http4k
  • s

    Sandymcp

    10/05/2021, 1:48 PM
    When debugging I can see there is an SSE handler present as the “trueHandler” in Undertow, but the predicate always resolves to false and I get an NPE
    d
    • 2
    • 1
  • s

    Sandymcp

    10/06/2021, 9:01 AM
    fun testDataGenerator(sse: Sse, start: Long = 0, delay: Long = 0, dataSource: (sequence: Int) -> SseMessage) {
        <http://logger.info|logger.info> { "Request from ${sse.connectRequest.uri}" }
        val closed = AtomicBoolean(false)
        sse.onClose { <http://logger.info|logger.info> { "Response body closed by client"; closed.set(true) } }
        Thread.sleep(start)
        for (i in 1..Int.MAX_VALUE) {
            if (closed.get())
                break
            try {
                val m = dataSource(i)
                if (m.event == null) {
                    sse.send(Data(data = m.data.joinToString(" ")))
                } else {
                    sse.send(Event(event = m.event!!, data = m.data.joinToString(" "), id = m.id))
                }
                logger.debug { "sent ${Clock.System.now().toEpochMilliseconds()}" }
                Thread.sleep(delay)
            } catch (_: EOFException) {
                <http://logger.info|logger.info> { "Response body closed by client" }
                break
            } catch (e: Throwable) {
                logger.error(e) { "Unexpected error writing to response body" }
                break
            }
        }
    }
    d
    • 2
    • 1
  • s

    Sandymcp

    10/06/2021, 9:02 AM
    also interrupting the curl and closing the connection doesn’t result in onClose being called
    • 1
    • 1
  • m

    Matt

    10/06/2021, 9:36 AM
    Could not find locale-lang in MyClass(localeLang=en)
    org.http4k.contract.openapi.v3.NoFieldFound: Could not find locale-lang in MyClass(localeLang=en)
        at app//org.http4k.contract.openapi.v3.FieldRetrieval$Companion.compose$lambda-0(FieldRetrieval.kt:17)
        at app//org.http4k.contract.openapi.v3.AutoJsonToJsonSchema.toObjectSchema(AutoJsonToJsonSchema.kt:99)
        at app//org.http4k.contract.openapi.v3.AutoJsonToJsonSchema.toObjectOrMapSchema(AutoJsonToJsonSchema.kt:83)
        at app//org.http4k.contract.openapi.v3.AutoJsonToJsonSchema.toSchema(AutoJsonToJsonSchema.kt:36)
        at app//org.http4k.contract.openapi.v3.AutoJsonToJsonSchema.toArraySchema(AutoJsonToJsonSchema.kt:53)
        at app//org.http4k.contract.openapi.v3.AutoJsonToJsonSchema.makePropertySchemaFor(AutoJsonToJsonSchema.kt:146)
        at app//org.http4k.contract.openapi.v3.AutoJsonToJsonSchema.toMapSchema(AutoJsonToJsonSchema.kt:124)
        at app//org.http4k.contract.openapi.v3.AutoJsonToJsonSchema.toObjectOrMapSchema(AutoJsonToJsonSchema.kt:83)
        at app//org.http4k.contract.openapi.v3.AutoJsonToJsonSchema.toSchema(AutoJsonToJsonSchema.kt:36)
        at app//org.http4k.contract.openapi.v3.AutoJsonToJsonSchema.toArraySchema(AutoJsonToJsonSchema.kt:53)
        at app//org.http4k.contract.openapi.v3.AutoJsonToJsonSchema.toSchema(AutoJsonToJsonSchema.kt:35)
        at app//org.http4k.contract.openapi.v3.AutoJsonToJsonSchema.toSchema(AutoJsonToJsonSchema.kt:26)
        at app//org.http4k.contract.openapi.ApiRenderer$Companion$Auto$fallbackSchema$1.toSchema(ApiRenderer.kt:30)
        at app//org.http4k.contract.openapi.ApiRenderer$Companion$Auto$1.toSchema(ApiRenderer.kt)
        at app//org.http4k.contract.openapi.v3.OpenApi3.toSchemaContent(OpenApi3.kt:205)
        at app//org.http4k.contract.openapi.v3.OpenApi3.collectSchemas(OpenApi3.kt:141)
        at app//org.http4k.contract.openapi.v3.OpenApi3.responses(OpenApi3.kt:134)
        at app//org.http4k.contract.openapi.v3.OpenApi3.apiPath(OpenApi3.kt:108)
        at app//org.http4k.contract.openapi.v3.OpenApi3.asPath(OpenApi3.kt:94)
        at app//org.http4k.contract.openapi.v3.OpenApi3.description(OpenApi3.kt:70)
        at app//org.http4k.contract.ContractRoutingHttpHandler$descriptionRoute$2$1.invoke(ContractRoutingHttpHandler.kt:66)
        at app//org.http4k.contract.ContractRoutingHttpHandler$descriptionRoute$2$1.invoke(ContractRoutingHttpHandler.kt:66)
        at app//org.http4k.routing.RouterMatch$MatchingHandler.invoke(Router.kt:47)
        at app//org.http4k.routing.RouterMatch$MatchingHandler.invoke(Router.kt:46)
        at app//org.http4k.core.Http4kKt$NoOp$1$1.invoke(Http4k.kt:11)
        at app//org.http4k.core.Http4kKt$NoOp$1$1.invoke(Http4k.kt:11)
        at app//org.http4k.core.Http4kKt$NoOp$1$1.invoke(Http4k.kt:11)
        at app//org.http4k.core.Http4kKt$NoOp$1$1.invoke(Http4k.kt:11)
        at app//org.http4k.core.Http4kKt$NoOp$1$1.invoke(Http4k.kt:11)
        at app//org.http4k.core.Http4kKt$NoOp$1$1.invoke(Http4k.kt:11)
        at app//com.pret.food.extensions.ServerFilterExtensions$BasicForwardedAuth$invoke$1$1.invoke(ServerFilterExtensions.kt:43)
        at app//com.pret.food.extensions.ServerFilterExtensions$BasicForwardedAuth$invoke$1$1.invoke(ServerFilterExtensions.kt:36)
        at app//org.http4k.contract.ContractRoutingHttpHandler$identify$1$1$1.invoke(ContractRoutingHttpHandler.kt:105)
        at app//org.http4k.contract.ContractRoutingHttpHandler$identify$1$1$1.invoke(ContractRoutingHttpHandler.kt:103)
        at app//org.http4k.core.Http4kKt$then$2.invoke(Http4k.kt:15)
        at app//org.http4k.core.Http4kKt$then$2.invoke(Http4k.kt:15)
        at app//org.http4k.routing.RouterMatch$MatchingHandler.invoke(Router.kt:47)
        at app//org.http4k.routing.RouterMatch$MatchingHandler.invoke(Router.kt:46)
        at app//org.http4k.routing.RouterBasedHttpHandler.invoke(RouterBasedHttpHandler.kt:19)
        at app//org.http4k.routing.RouterBasedHttpHandler.invoke(RouterBasedHttpHandler.kt:13)
        at app//AppTest$3.invokeSuspend(AppTest.kt:31)
        at app//AppTest$3.invoke(AppTest.kt)
        at app//AppTest$3.invoke(AppTest.kt)
    d
    • 2
    • 11
  • m

    Moritz Platt

    10/07/2021, 10:00 AM
    I'm struggling to register a custom Gson configuration with my app. It looks as if, when using
    body.auto
    my Gson config is not used:
    val auto: CustomerCreateModel = Body.auto<CustomerCreateModel>().toLens().extract(it)
            val manual: CustomerCreateModel = MyGson.mapper.fromJson(it.bodyString(), CustomerCreateModel::class.java)
            assert(auto == manual) //fails
    I have the following on my classpath:
    object MyGson : ConfigurableGson(
        GsonBuilder()
            .registerTypeAdapter(Country::class.java, CountryDeserializer())
            .registerTypeAdapter(Country::class.java, CountrySerializer())
            .setDateFormat("yyyy MM-dd'T'HH:mm:ss.SSS'Z'")
            .asConfigurable()
            .withStandardMappings()
            .done()
    )
    I'm on a fairly old version of http4k (3.261.0), btw
    d
    • 2
    • 4
  • e

    Erich

    10/11/2021, 4:21 PM
    I’m trying to create dynamic endpoints coming from a list... I see from the docs how to add path params...
    "/book/{title}" bind GET to { req -> Response.invoke(Status.OK).body(GetBookDetails(req.path("title")) }
    "/author/{name}/latest" bind GET to { req -> Response.invoke(Status.OK).body(GetAllBooks(author = req.path("name")).last()) }
    But I’m looking to be able to take a list of Strings at runtime and create paths dynamically (with param) for each...
    ["book","author","character",..., "n"]
    "/book/{key}"
    "/author/{key}"
    "/character/{key}"
    ...
    "/{n}/{key}"
    Is this possible?
    d
    • 2
    • 4
  • a

    Andrew O'Hara

    10/12/2021, 8:26 PM
    I have a scenario where I want to return some data in an
    OPTIONS
    call. The call works fine with a core http4k router, but if I wrap it in a
    contract
    , then the contract router never calls my handler and returns an empty 200. Presumably, this was done with the assumption that
    OPTIONS
    doesn't make much sense in a RESTP API, but that isn't always the case. Is this phenomenon intentional? Is there a way to prevent it?
    @Test
    fun options() {
        val handler = contract {
            routes += "/v1/things" bindContract Method.OPTIONS to { _ ->
                Response(Status.OK).body("lolcats")
            }
        }
    
        val resp = handler(Request(Method.OPTIONS, "/v1/things"))
        resp shouldHaveStatus Status.OK
        resp shouldHaveBody "lolcats"
    }
    Body: expected:<"lolcats"> but was:<<empty string>>
    d
    • 2
    • 2
  • j

    Jim

    10/15/2021, 1:19 PM
    How would you guys go about documenting example error responses in the following format: https://datatracker.ietf.org/doc/html/rfc7807 using the Contract module meta tags? The error response would include the complete path. Which you might not know when setting the meta info using returning() Because the contract might be composed at other places. To clarify: the actual response formatting is easy using the ErrorResponseRenderer. This is purely about generating / defining correct examples without hard coupling spec to code.
    d
    j
    • 3
    • 18
  • s

    spierce7

    10/19/2021, 3:42 AM
    So I’m a little surprised to find this, but I’m not seeing any coroutine support in http4k. Is that the case?
    d
    d
    +2
    • 5
    • 7
  • s

    Sean Abbott

    10/29/2021, 7:03 PM
    I can't figure out how to tell http4k to use a specific deserializer for a particular class. I have https://www.http4k.org/guide/howto/create_a_custom_json_marshaller/ this kinda working, but inside my data class (rather deep inside my data class), I have what is essentially a value class interface and I need to be able to tell my http4k client to use a specific implementation of the interface when deserializing.
    r
    • 2
    • 3
  • m

    Matt

    11/08/2021, 2:57 PM
    Hello all, is it possible to have a path param that maps to a enum and is not case sensitive?
    d
    • 2
    • 10
  • d

    dave

    11/11/2021, 11:56 AM
    you can create a RoutedRequest and use that to test, or send the incoming request through a routing block first..
    m
    • 2
    • 1
  • e

    Eduardo Narros

    11/16/2021, 6:09 PM
    Hey 👋, we have observed that our http4k application will block at times when there is a long running request being processed separately. I.e. given this application:
    fun main() {
        val counter = AtomicLong()
    
        routes(
            "/sleep" bind Method.GET to {
                Thread.sleep(20000).body("woke up")
                Response(Status.OK)
            },
            "/info" bind Method.GET to { Response(Status.OK).body(counter.addAndGet(1L).toString() + " ") },
        ).asServer(Netty(7000)).start()
    }
    If you run it and then using a terminal you run:
    curl localhost:7000/sleep& for run in {1..20}; do curl localhost:7000/info; done
    you would expect to see all the 20 /info responses and then the initial /sleep response after a few seconds. However, what happens is that you get around 15 /info responses, then the next request blocks until the /sleep request completes and then the remaining requests are completed. i.e.
    curl localhost:7000/sleep& for run in {1..20}; do curl localhost:7000/info; done
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 woke up
    16 17 18 19 20
    We have tried in different machines and we most of the time you get up to around 15 before the request blocks so we don’t know if that number is driven by a configuration parameter that we aren’t aware of.
    d
    • 2
    • 2
  • d

    dave

    11/17/2021, 7:40 AM
    Morning! We've upgraded your favourite HTTP library to the newly released Kotlin 1.6.0, and taken the chance to remove all of the existing deprecations in the source. As ever, if you're upgrading from an older version, we recommend going to the latest non-breaking version (4.16.3.0) first and dealing with any deprecations before taking the leap to the new one 🎉 Version v4.17.0.0 available now in Maven Central! 🎉 https://www.http4k.org/changelog/#v41700
    🎉 1
    🙌 1
    👍 4
    j
    s
    +2
    • 5
    • 5
  • a

    Alexander Weickmann

    11/22/2021, 9:01 PM
    Hi all, is it possible to somehow configure the socket timeout for the Java8HttpClient? Cannot find it in the docs 😞
    s
    • 2
    • 5
  • d

    DanielZ

    11/24/2021, 8:59 AM
    Hi, I’m wondering how to connect my http4k application with newRelic for monitoring? Should this be done using newRelic annotations like ´@Trace´ ? Or is there a more http4k way?
    d
    • 2
    • 1
  • b

    Ben Tilford

    11/29/2021, 11:47 PM
    Is there a way to setup
    RequestContext
    for unit tests that are using a mocked request?
    j
    s
    d
    • 4
    • 13
  • d

    dmcg

    12/03/2021, 8:41 AM
    I’ve just been using HandlebarsTemplates in

    https://youtu.be/MxzAr4UBOs0▾

    . Which leads me to ask the question, why does there need to be a ViewModel type?
    d
    • 2
    • 11
  • r

    Razvan

    12/04/2021, 8:37 PM
    what have you done with the swagger mapping... looks like the keys are mapped to lowecase mapping is kinda failing...
    data class TestData(
        val s1: Int? = null,
        val sXe: Int? = null,
    )
    object TestRoute {
        val bodyTest = Body.auto<TestData>().toLens()
    
        operator fun invoke(): ContractRoute {
            val metaData: RouteMetaDsl.() -> Unit = {
                tags += Tag("aaa")
                returning(
                    Status.OK,
                    bodyTest to TestData(s1 = 1, sXe = 22)
                )
            }
            return "/test" meta metaData bindContract Method.GET to { _: Request -> Response(Status.OK).with(bodyTest of TestData(s1 = 1, sXe = 12))  }
        }
    }
    if in the example
    sXe
    has a value the /api/swagger.json fails to load:
    Could not find sxe in TestData(s1=1, sXe=22)
    if i set it to null in the example it loads but when i run the query I get as response body
    {
      "s1": 1,
      "sxe": 12
    }
    Looks like the uppercase
    X
    is no longer in uppercase... In
    4.17.2.0
    d
    • 2
    • 21
  • m

    MrNiamh

    12/06/2021, 3:11 PM
    Another question on the above - If I want to have a separate body along with the file (e.g. I want to upload a file, along with its visibility, and if a signature is required), that’s not possible is it? It would have to be:
    val imageFile = MultipartFormFile.required("inputFile")
    val visibility = MultipartFormField.string().required("visibility")
    val signatureRequired = MultipartFormField.string().required("signatureRequired")
    
    val strictFormBody = Body.multipartForm(Validator.Strict, imageFile, visibility, signatureRequired, diskThreshold = 5).toLens()
    And if that’s the case, is there no way to show through the api docs that
    signatureRequired
    is a boolean? Just want to make sure i’m doing it correctly
    s
    • 2
    • 2
  • l

    Luca Battistelli

    12/16/2021, 4:05 PM
    Hello everyone, I have created a new project using
    http4k generate project -o folder-name -r dD1BQU1BWlFES0FTOEI5Z09FQS1nRDZRU3dCUlEmYz1NYWluJnA9Y29tLmJhdHRpc3RlbGxpLmx1Y2E
    but I don't seem to be able to run tests. What am I missing?
    $ ./gradlew test
    Starting a Gradle Daemon, 8 stopped Daemons could not be reused, use --status for details
    > Task :test FAILED
    
    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Execution failed for task ':test'.
    > There were failing tests. See the report at: file:///Users/lucabattistelli/repos/backend-test-kotlin-2/Main/build/reports/tests/test/index.html
    
    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
    
    * Get more help at <https://help.gradle.org>
    
    BUILD FAILED in 6s
    3 actionable tasks: 1 executed, 2 up-to-date
    s
    • 2
    • 4
  • a

    Andrew O'Hara

    12/16/2021, 8:49 PM
    Does anyone know how I can make a header lens that has a list of options, mapping to a property of an enum? This contrived example compiles, but the tests fail. I know how to use
    map
    on a
    BiDiLensSpec
    to convert between a DTO and a domain model, but I don't if or how I can use that to help here. I want the lens to be backed by an enum so that the openapi spec shows the options; simply validating a string isn't good enough.
    enum class MyEnumDto(val value: String) {
          A("aaaa"),
          B("bbbb"),
          C("cccc")
        }
    
        val lens: BiDiLens<HttpMessage, MyEnumDto> = Header
            .mapWithNewMeta(StringBiDiMappings.enum<MyEnumDto>(), ParamMeta.EnumParam(MyEnumDto::class))
            .defaulted("my-enum", MyEnumDto.A)
    
        @Test
        fun `parse enum lens`() {
            val request = Request(Method.GET, "/").header("my-enum", "bbbb")
    
            lens(request) shouldBe MyEnumDto.B
            /*
            header 'my-enum' must be string
            Caused by: java.lang.IllegalArgumentException: No enum constant com.cacmloud.config.camcapi.api.legacy.LegacyLensesTest.MyEnumDto.bbbb
                at java.base/java.lang.Enum.valueOf(Enum.java:273)
                at com.cacmloud.config.camcapi.api.legacy.LegacyLensesTest$MyEnumDto.valueOf(LegacyLensesTest.kt)
                at com.cacmloud.config.camcapi.api.legacy.LegacyLensesTest$special$$inlined$enum$1.invoke(BiDiMapping.kt:87)
                at com.cacmloud.config.camcapi.api.legacy.LegacyLensesTest$special$$inlined$enum$1.invoke(BiDiMapping.kt:87)
                at org.http4k.lens.BiDiMapping.asOut(BiDiMapping.kt:47)
                at org.http4k.lens.LensSpecKt$mapWithNewMeta$1.invoke(lensSpec.kt:249)
                at org.http4k.lens.LensGet$map$1.invoke(lensSpec.kt:270)
                at org.http4k.lens.LensGet$map$1.invoke(lensSpec.kt:19)
                at org.http4k.lens.LensGet$invoke$1.invoke(lensSpec.kt:17)
                at org.http4k.lens.LensGet$invoke$1.invoke(lensSpec.kt:17)
                at org.http4k.lens.BiDiLensSpec$defaulted$2.invoke(lensSpec.kt:165)
                at org.http4k.lens.Lens.invoke(lens.kt:15)
                ... 109 more
             */
        }
    
        @Test
        fun `write enum lens`() {
            val request = Request(Method.GET, "/")
                .with(lens of MyEnumDto.C)
    
            request.shouldHaveHeader("my-enum", "cccc")
            /*
            Header "my-enum": expected:<"cccc"> but was:<"C">
            Expected :"cccc"
            Actual   :"C"
             */
        }
    s
    • 2
    • 3
  • a

    Andrew O'Hara

    12/17/2021, 9:14 PM
    Related to above, but another issue: when I have
    xml
    and
    json
    content types available for a body in my contract meta, the sample responses aren't honoring the xml-specific jackson annotations (e.g. for element wrappers and root elements). The actual responses are fine. Presumably this is because the
    OpenApi3
    class uses a json converter and ignores the xml annotations. Is there any way to get this to work?
    d
    • 2
    • 3
  • d

    dave

    12/18/2021, 11:48 AM
    PSA: Didn't think that we needed to say this, but what with Log4J 2.17.0 out today - it's worth us stating explicitly that http4k is totally unaffected by the various log4j vulnerabilities. We also would take this opportunity to recommend that everyone also considers using the Structured Events implementation that we provide in the core in lieu of any actual logging library. 😉
    👀 1
    💪 7
    a
    • 2
    • 1
  • a

    andyg

    12/28/2021, 3:01 AM
    Hi, I have a RoutingHttpHandler (a group of routes), is there a way to define a response header just once instead of adding to each route individually (i.e.
    .with(CONTENT_TYPE of TEXT_HTML)
    or similar)? Must be an easy way but I'm missing it. Thanks!
    d
    • 2
    • 2
  • m

    Michael Grigoryan

    12/29/2021, 5:49 AM
    Hey everyone. I am new to http4k and I have a question about transferring JSON information. From what I learned from the documentation, I have to create lens(
    BiDiBodyLens<T>
    ) to send and I guess receive JSON but isn't there any other way of sending JSON as a response in http4k except for I guess "injecting" the response? Also is there any place where I can find all the terminology used in http4k? My code currently looks like this:
    // Will be used for sending JSON encoded emoji as a response
    val lensEmojiResponse: BiDiBodyLens<Emoji> = Body.auto<Emoji>().toLens()
    
    /**
     * Entrypoint of the application. We can consider
     * this as the main router to which all other routes
     * are attached to.
     */
    val app: HttpHandler = { _: Request ->
        val emoji = Emoji("Sob", "😭")
    
        // Setting the response value. It will be already
        // encoded as JSON by kotlinx.serialization
        lensEmojiResponse.inject(emoji, Response(Status.OK))
    }
    d
    • 2
    • 2
  • m

    MrNiamh

    01/02/2022, 1:02 PM
    Hey, is there a way to specify the format for a property of a dto in the OpenAPI json? E.g. I'm trying to make it specify a property
    startDate
    is a date. Currently in my swagger.json it has:
    "startDate": {
      "example": "2021-01-01",
      "description": null,
      "type": "string"
    },
    but (i think) i'm expecting:
    "startDate": {
      "example": "2021-01-01",
      "description": null,
      "type": "string",
      "format": "date"
    },
    I've defined my bidi lens as:
    BiDiMapping<String, LocalDate>({ LocalDate.parse(it) }, { it.format(DateTimeFormatter.ISO_LOCAL_DATE) })
    d
    • 2
    • 3
  • m

    MrNiamh

    01/04/2022, 1:05 PM
    Okay deleted old question. Currently trying to return a MultipartForm from a contract route but does not seem to work (errors in thread). Have something like this, which I would assume should work, however getting errors when loading the swagger:
    fun downloadDocument(documentService: DocumentService): ContractRoute =
        "document" / Path.uuid().of("documentId") meta {
            summary = "Download document"
            returning(OK, formBody to formExample)
        } bindContract Method.GET to
                { documentId ->
                    { 
                            val doc = documentService.getDocument(documentId))
    
                            Response(OK)
                                .with(
                                    formBody of MultipartForm()
                                        .with(outputFile of MultipartFormFile(filename = doc.filename, contentType = ContentType(doc.mimeType), content = doc.data.inputStream()))
                                )
                    }
                }
    
    
    
    
    private val outputFile = MultipartFormFile.required("outputFile")
    private val formBody = Body.multipartForm(Validator.Strict, outputFile).toLens()
    
    private val formExample = MultipartForm()
        .with(outputFile of MultipartFormFile(filename = "test_file.txt", contentType = ContentType.TEXT_PLAIN, content = createTempFileWithName("byteBufferExample", suffix = ".txt")
            .also { it.writeText("example") }
            .inputStream()))
    d
    • 2
    • 5
  • a

    Alen Mujezinovic

    01/12/2022, 12:16 PM
    Hi folks. Quick question: Is there any support for using
    kotlinx.serialization
    with the
    contract
    module? The data classes in
    contract
    are not market with any annotations that the
    kotlinx
    lib needs. Would I have to create custom serialisers?
    d
    p
    • 3
    • 3
  • p

    Philipp Mayer

    01/19/2022, 10:21 PM
    Out of curiousity: What’s the decision behind not using Gradle’s kts version? 🙂
    d
    p
    • 3
    • 5
Powered by Linen
Title
p

Philipp Mayer

01/19/2022, 10:21 PM
Out of curiousity: What’s the decision behind not using Gradle’s kts version? 🙂
d

dave

01/19/2022, 10:25 PM
Tbh - the Kotlin gradle dsl is a bit of a mystery to us - there's no reason other than that's the way it was done when someone helped us do it! If we can make it simpler then PR away! 🙃
p

Philipp Mayer

01/19/2022, 10:35 PM
Not wanting to make any promises but I’d like to look into it. Where can I find the code for the toolbox? (I just realize now that it might’ve not been clear that I was talking about the project wizard. 😬)
d

dave

01/19/2022, 10:44 PM
Ah - the toolbox code is actually private to us. It's pretty involved, and it also contains the OpenApi generator code which we are keeping under our hat because it's actually quite sophisticated secret sauce. 🙃
👍 1
p

Philipp Mayer

01/19/2022, 10:47 PM
I see - fair enough! 😈
p

pabl0rg

01/21/2022, 11:13 AM
Gradle Kts is a minefield. Gradle is already a confusing jumble, but having to guess what’s the latest way to express something in kts is torture
:yes: 2
View count: 5