Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

null value in HttpRequest.sendJson and HttpRequest.sendJsonable should be treated as json null #1436

Closed
gagarski opened this issue Oct 24, 2019 · 1 comment
Assignees
Labels
Milestone

Comments

@gagarski
Copy link

gagarski commented Oct 24, 2019

Version

  • vert.x core: 3.8.3
  • vert.x web: 3.8.3

Let's consider the following code:

import io.vertx.core.Vertx
import io.vertx.core.json.JsonObject
import io.vertx.ext.web.client.HttpRequest
import io.vertx.ext.web.client.HttpResponse
import io.vertx.ext.web.client.WebClient
import io.vertx.kotlin.coroutines.awaitResult
import io.vertx.kotlin.coroutines.dispatcher
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

fun <T> Vertx.runSuspend(block: suspend Vertx.() -> T): Unit =
    run {
        GlobalScope.launch(dispatcher()) {
            block()
        }
    }

/**
 * Actually a workaround. message has non-nullable type in Kotlin.
 */
suspend fun <T, U> HttpRequest<T>.sendJsonAwait(obj: U) =
    awaitResult<HttpResponse<T>> { sendJson(obj, it) }

/**
 * Actually a workaround. message has non-nullable type in Kotlin.
 */
suspend fun <T> HttpRequest<T>.sendJsonObjectAwait(obj: JsonObject?) =
    awaitResult<HttpResponse<T>> { sendJsonObject(obj, it) }

fun main() = Vertx.vertx().runSuspend {
    println(
        WebClient
            .create(this)
            .postAbs("http://example.com/")
            .sendJsonAwait(null)
            .body())
    println(
        WebClient
            .create(this)
            .postAbs("http://example.com/")
            .sendJsonObjectAwait(null)
            .body())
}

The result of both requests is HTTP error 411 (Length Required). Looking through the relevant vertx-web code I found out that there is one point where null values passed to send* methods of HttpRequest are handled: HttpContext.handleSendRequest(). In all cases they are treated as sending a request without body. However it does not seems like a right behavior in terms on JSON. There is null value in JSON which is represented by null Java value (in terms of Jackson and io.vertx.core.json). It seems like the right thing to do here is to send a body with null characters and having content length of 4. Otherwise to do it we need external handling of jsonable null values: creating Buffer.buffer("null"), setting content type manually and calling sendBuffer.

@vietj
Copy link
Contributor

vietj commented Oct 25, 2019

right, we should use a marker object for the body when sending null

@vietj vietj added this to the 3.8.4 milestone Oct 25, 2019
@vietj vietj added the bug label Oct 25, 2019
@vietj vietj self-assigned this Oct 25, 2019
vietj added a commit that referenced this issue Oct 25, 2019
@vietj vietj closed this as completed in de88aaa Oct 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants