https://kotlinlang.org logo
#http4k
Title
# http4k
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:
Copy code
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()))
First one I get
java.lang.IllegalArgumentException: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.http4k.lens.MultipartForm[\“files\“]->java.util.Collections$SingletonMap[\“outputFile\“]->java.util.ArrayList[0]->org.http4k.lens.MultipartFormFile[\“content\“]->java.io.FileInputStream[\“fd\“])
Once I disable that, I get:
org.http4k.contract.openapi.v3.NoFieldFound: Could not find open in sun.nio.ch.FileChannelImpl@12707084\n\tat org.http4k.contract.openapi.v3.FieldRetrieval$Companion.compose$lambda-0(FieldRetrieval.kt:17)\n\tat org.http4k.contract.openapi.v3.AutoJsonToJsonSchema.toObjectSchema(AutoJsonToJsonSchema.kt:99)
Gotten slightly further by changing the example to just
MultipartForm()
. Next issue is that nothing actually returns from the service, just stays stuck loading until I stop the service, then swagger/postman displays the results…. very odd behaviour.
d

dave

01/04/2022, 4:03 PM
for multipart you need to disable the PreExtraction check in the route
that might get you a little further
m

MrNiamh

01/04/2022, 4:33 PM
Still getting the weird behaviour of it not actually returning anything until stopping the application. As a complete baseless guess, is there some stream which isn’t being closed?
3 Views