Skip to content

Commit

Permalink
[#662] Add method to provide object as a body content (#728)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasqueiroz authored Apr 2, 2020
1 parent aa8010f commit 84e4ab7
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 2 deletions.
43 changes: 41 additions & 2 deletions fuel-jackson/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,47 @@ implementation 'com.github.kittinunf.fuel:fuel-jackson:<latest-version>'

## Usage

The Fuel-Jackson module provides a built in support for Jackson deserialization.
This is done by adding the `responseObject` extension function into Fuel `Request` interface.
The Fuel-Jackson module provides a built in support for Jackson serialization and deserialization.

### Serialization

The serialization is done by adding the `objectBody` extension function into Fuel `Request` interface.

By default, the `objectBody` call will use the `Charsets.UTF-8` charset and the `defaultMapper` property defined in `FuelJackson.kt`.

```kotlin
data class FakeObject(val foo: String = "foo")

Fuel.post("/fooBar")
.objectBody(FakeObject())
```

Alternatively, you can provide a custom `charset` as a parameter to it.

```kotlin
data class FakeObject(val foo: String = "foo")

Fuel.post("/fooBar")
.objectBody(FakeObject(), Charsets.UTF_16)
```

You can also provide your own `ObjectMapper` as a parameter.

```kotlin
data class FakeObject(val foo: String = "foo")

val mapper = ObjectMapper().registerKotlinModule()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)

mapper.propertyNamingStrategy = PropertyNamingStrategy.SNAKE_CASE

Fuel.post("/fooBar")
.objectBody(FakeObject(), mapper = mapper)
```

### Deserialization

The deserialization is done by adding the `responseObject` extension function into Fuel `Request` interface.

By default, the `responseObject` call will use the `defaultMapper` property defined in `FuelJackson.kt`.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.kittinunf.fuel.jackson

import com.fasterxml.jackson.databind.ObjectMapper
import com.github.kittinunf.fuel.core.Headers
import com.github.kittinunf.fuel.core.Request
import java.nio.charset.Charset

/**
* Set the body to an Object to be serialized
*/
fun Request.objectBody(
bodyObject: Any,
charset: Charset = Charsets.UTF_8,
mapper: ObjectMapper = defaultMapper
): Request {
val bodyString = mapper.writeValueAsString(bodyObject)
this[Headers.CONTENT_TYPE] = "application/json"
return body(bodyString, charset)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.github.kittinunf.fuel

import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.PropertyNamingStrategy
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import com.github.kittinunf.fuel.core.Headers
import com.github.kittinunf.fuel.core.Method
import com.github.kittinunf.fuel.core.requests.DefaultRequest
import com.github.kittinunf.fuel.jackson.objectBody
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Test
import java.net.URL

class ObjectBodyTest {
@Test
fun setsBodyCorrectly() {
val expectedBody = "{\"foo\":42,\"bar\":\"foo bar\",\"fooBar\":\"foo bar\"}"
val bodyObject = FakeObject()

val request = DefaultRequest(Method.POST, URL("https://test.fuel.com/body"))
.objectBody(bodyObject)

assertThat(expectedBody, equalTo(String(request.body.toByteArray())))
}

@Test
fun setsContentTypeCorrectly() {
val bodyObject = listOf(
42,
mapOf("foo" to "bar")
)

val request = DefaultRequest(Method.POST, URL("https://test.fuel.com/body"))
.objectBody(bodyObject)

assertThat(request[Headers.CONTENT_TYPE].lastOrNull(), equalTo("application/json"))
}

@Test
fun setsBodyCorrectlyWithCustomMapper() {
val mapper = createCustomMapper()
val expectedBody = "{\"foo\":42,\"bar\":\"foo bar\",\"foo_bar\":\"foo bar\"}"
val bodyObject = FakeObject()

val request = DefaultRequest(Method.POST, URL("https://test.fuel.com/body"))
.objectBody(bodyObject, mapper = mapper)

assertThat(expectedBody, equalTo(String(request.body.toByteArray())))
}

private fun createCustomMapper(): ObjectMapper {
val mapper = ObjectMapper().registerKotlinModule()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)

mapper.propertyNamingStrategy = PropertyNamingStrategy.SNAKE_CASE

return mapper
}
}

data class FakeObject(
val foo: Int = 42,
val bar: String = "foo bar",
val fooBar: String = "foo bar"
)

0 comments on commit 84e4ab7

Please sign in to comment.