-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Use Jackson ObjectMapper provided by Akka #9494
Merged
mergify
merged 10 commits into
playframework:master
from
marcospereira:issues/9383/use-akka-jackson-object-mapper
Jul 25, 2019
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8806e7f
Use ObjectMapper provided by Akka Jackson support
marcospereira ace4986
Fix deprecation messages
marcospereira cac898b
Add mima filters
marcospereira f8bc49d
Fix documentation issues
marcospereira d65d1d6
Inherit all ObjectMapper defaults from Akka
marcospereira 56366fc
Add test for namespaced object mapper configuration
marcospereira 0f32291
Update documentation/manual/releases/release28/migration28/Migration2…
marcospereira 3d714f1
Some minor improvements
marcospereira c530d82
Merge branch 'master' into issues/9383/use-akka-jackson-object-mapper
octonato 4434850
Fix merge problem
marcospereira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
211 changes: 211 additions & 0 deletions
211
core/play-integration-test/src/it/scala/play/it/libs/json/JavaJsonSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
/* | ||
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package play.it.libs.json | ||
|
||
import java.io.ByteArrayInputStream | ||
import java.time.Instant | ||
import java.util.Optional | ||
import java.util.OptionalInt | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.databind.SerializationFeature | ||
import org.specs2.mutable.Specification | ||
import org.specs2.specification.Scope | ||
import play.api.inject.guice.GuiceApplicationBuilder | ||
import play.api.mvc.Request | ||
import play.core.test.FakeRequest | ||
import play.libs.Json | ||
import play.mvc.Http | ||
import play.mvc.Http.RequestBody | ||
|
||
// Use an `ObjectMapper` which overrides some defaults | ||
class PlayBindingNameJavaJsonSpec extends JavaJsonSpec { | ||
override val createObjectMapper: ObjectMapper = GuiceApplicationBuilder() | ||
// should be able to use `.play.` namespace to override configurations | ||
// for this `ObjectMapper`. | ||
.configure("akka.serialization.jackson.play.serialization-features.WRITE_DURATIONS_AS_TIMESTAMPS" -> false) | ||
.build() | ||
.injector | ||
.instanceOf[ObjectMapper] | ||
|
||
"ObjectMapper" should { | ||
"respect the custom configuration" in new JsonScope { | ||
Json.mapper().isEnabled(SerializationFeature.WRITE_DATES_WITH_ZONE_ID) must beFalse | ||
} | ||
} | ||
} | ||
|
||
// The dependency injected `ObjectMapper` | ||
class ApplicationJavaJsonSpec extends JavaJsonSpec { | ||
marcospereira marked this conversation as resolved.
Show resolved
Hide resolved
|
||
override val createObjectMapper: ObjectMapper = GuiceApplicationBuilder().build().injector.instanceOf[ObjectMapper] | ||
} | ||
|
||
// Classic static `ObjectMapper` from play.libs.Json | ||
class StaticJavaJsonSpec extends JavaJsonSpec { | ||
override val createObjectMapper: ObjectMapper = Json.newDefaultMapper() | ||
} | ||
|
||
trait JavaJsonSpec extends Specification { | ||
|
||
sequential | ||
|
||
def createObjectMapper: ObjectMapper | ||
|
||
private[json] class JsonScope(val mapper: ObjectMapper = createObjectMapper) extends Scope { | ||
val testJsonString = | ||
"""{ | ||
| "foo" : "bar", | ||
| "bar" : "baz", | ||
| "instant" : 1425435861, | ||
| "optNumber" : 55555, | ||
| "optionalInt" : 12345, | ||
| "a" : 2.5, | ||
| "copyright" : "\u00a9", | ||
| "baz" : [ 1, 2, 3 ] | ||
|}""".stripMargin.replaceAll("\r?\n", System.lineSeparator) | ||
|
||
val testJsonInputStream = new ByteArrayInputStream(testJsonString.getBytes("UTF-8")) | ||
|
||
val testJson = mapper.createObjectNode() | ||
testJson | ||
.put("foo", "bar") | ||
.put("bar", "baz") | ||
.put("instant", 1425435861) | ||
.put("optNumber", 55555) | ||
.put("optionalInt", 12345) | ||
.put("a", 2.5) | ||
.put("copyright", "\u00a9") // copyright symbol | ||
.set("baz", mapper.createArrayNode().add(1).add(2).add(3)) | ||
|
||
Json.setObjectMapper(mapper) | ||
} | ||
|
||
"Json" should { | ||
|
||
"use the correct object mapper" in new JsonScope { | ||
Json.mapper() must_== mapper | ||
} | ||
|
||
"parse" in { | ||
|
||
"from string" in new JsonScope { | ||
Json.parse(testJsonString) must_== testJson | ||
} | ||
|
||
"from UTF-8 byte array" in new JsonScope { | ||
Json.parse(testJsonString.getBytes("UTF-8")) must_== testJson | ||
} | ||
|
||
"from InputStream" in new JsonScope { | ||
Json.parse(testJsonInputStream) must_== testJson | ||
} | ||
} | ||
|
||
"stringify" in { | ||
|
||
"stringify" in new JsonScope { | ||
Json.stringify(testJson) must_== Json.stringify(Json.parse(testJsonString)) | ||
} | ||
|
||
"asciiStringify" in new JsonScope { | ||
val resultString = Json.stringify(Json.parse(testJsonString)).replace("\u00a9", "\\u00A9") | ||
Json.asciiStringify(testJson) must_== resultString | ||
} | ||
|
||
"prettyPrint" in new JsonScope { | ||
Json.prettyPrint(testJson) must_== testJsonString | ||
} | ||
|
||
"serialize Java Optional fields" in new JsonScope { | ||
val optNumber = Optional.of[Integer](55555) | ||
val optInt = OptionalInt.of(12345) | ||
|
||
// The configured mapper should be able to handle optional values | ||
Json.mapper().writeValueAsString(optNumber) must_== "55555" | ||
Json.mapper().writeValueAsString(optInt) must_== "12345" | ||
} | ||
|
||
"serialize Java Time field" in new JsonScope { | ||
val instant: Instant = Instant.ofEpochSecond(1425435861) | ||
|
||
// The configured mapper should be able to handle Java Time fields | ||
Json.mapper().writeValueAsString(instant) must_== """"2015-03-04T02:24:21Z"""" | ||
} | ||
} | ||
|
||
"when deserializing to a POJO" should { | ||
|
||
"deserialize from request body" in new JsonScope(createObjectMapper) { | ||
val validRequest: Request[Http.RequestBody] = | ||
Request[Http.RequestBody](FakeRequest(), new RequestBody(testJson)) | ||
val javaPOJO = validRequest.body.parseJson(classOf[JavaPOJO]).get() | ||
|
||
javaPOJO.getBar must_== "baz" | ||
javaPOJO.getFoo must_== "bar" | ||
javaPOJO.getInstant must_== Instant.ofEpochSecond(1425435861L) | ||
javaPOJO.getOptNumber must_== Optional.of(55555) | ||
javaPOJO.getOptionalInt must_== OptionalInt.of(12345) | ||
} | ||
|
||
"deserialize even if there are missing fields" in new JsonScope(createObjectMapper) { | ||
val testJsonMissingFields: Request[Http.RequestBody] = | ||
Request[Http.RequestBody](FakeRequest(), new RequestBody(mapper.createObjectNode())) | ||
testJsonMissingFields.body.parseJson(classOf[JavaPOJO]).get().getBar must_== null | ||
} | ||
|
||
"return empty when request body is not a JSON" in new JsonScope(createObjectMapper) { | ||
val testNotJsonBody: Request[Http.RequestBody] = | ||
Request[Http.RequestBody](FakeRequest(), new RequestBody("foo")) | ||
testNotJsonBody.body.parseJson(classOf[JavaPOJO]) must_== Optional.empty() | ||
} | ||
|
||
"ignore unknown fields" in new JsonScope(createObjectMapper) { | ||
val javaPOJO = Json.fromJson(testJson, classOf[JavaPOJO]) | ||
javaPOJO.getBar must_== "baz" | ||
javaPOJO.getFoo must_== "bar" | ||
javaPOJO.getInstant must_== Instant.ofEpochSecond(1425435861L) | ||
javaPOJO.getOptNumber must_== Optional.of(55555) | ||
} | ||
} | ||
|
||
"when serializing from a POJO" should { | ||
"serialize to a response body" in new JsonScope(createObjectMapper) { | ||
val pojo = new JavaPOJO( | ||
"Foo String", | ||
"Bar String", | ||
Instant.ofEpochSecond(1425435861), | ||
Optional.of[Integer](55555), | ||
OptionalInt.of(12345) | ||
) | ||
val jsonNode: JsonNode = Json.toJson(pojo) | ||
|
||
// Regular fields | ||
jsonNode.get("foo").asText() must_== "Foo String" | ||
jsonNode.get("bar").asText() must_== "Bar String" | ||
|
||
// Optional fields | ||
jsonNode.get("optNumber").asText() must_== "55555" | ||
jsonNode.get("optionalInt").asText() must_== "12345" | ||
|
||
// Java Time fields | ||
jsonNode.get("instant").asText() must_== "2015-03-04T02:24:21Z" | ||
} | ||
|
||
"include null fields" in new JsonScope(createObjectMapper) { | ||
val pojo = new JavaPOJO( | ||
null, // foo | ||
"Bar String", // bar | ||
Instant.ofEpochSecond(1425435861), | ||
Optional.of[Integer](55555), | ||
OptionalInt.of(12345) | ||
) | ||
val jsonNode: JsonNode = Json.toJson(pojo) | ||
|
||
jsonNode.has("foo") must beTrue | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is wrong. It tests for
WRITE_DATES_WITH_ZONE_ID
butWRITE_DURATIONS_AS_TIMESTAMPS
is configured. Makes no sense. No matter if I setWRITE_DURATIONS_AS_TIMESTAMPS
totrue
orfalse
(like here) the test always passes...