From 3d718cf1f15f07610d435d79f88c93ee81075a5d Mon Sep 17 00:00:00 2001 From: Konrad Malawski Date: Mon, 8 Feb 2016 16:07:35 +0100 Subject: [PATCH] +htc #19708 provides default marshaller for akka.Done (200, empty text response) --- .../server/directives/FutureDirectivesExamplesSpec.scala | 1 + .../akka/http/scaladsl/marshalling/MarshallingSpec.scala | 3 +++ .../marshalling/PredefinedToEntityMarshallers.scala | 6 ++++++ .../marshalling/PredefinedToResponseMarshallers.scala | 4 +++- 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/akka-docs/rst/scala/code/docs/http/scaladsl/server/directives/FutureDirectivesExamplesSpec.scala b/akka-docs/rst/scala/code/docs/http/scaladsl/server/directives/FutureDirectivesExamplesSpec.scala index cec10456ba..505a554b3a 100644 --- a/akka-docs/rst/scala/code/docs/http/scaladsl/server/directives/FutureDirectivesExamplesSpec.scala +++ b/akka-docs/rst/scala/code/docs/http/scaladsl/server/directives/FutureDirectivesExamplesSpec.scala @@ -5,6 +5,7 @@ package docs.http.scaladsl.server.directives import java.util.concurrent.TimeUnit +import akka.http.scaladsl.marshalling.ToResponseMarshallable import docs.http.scaladsl.server.RoutingSpec import scala.concurrent.Future diff --git a/akka-http-tests/src/test/scala/akka/http/scaladsl/marshalling/MarshallingSpec.scala b/akka-http-tests/src/test/scala/akka/http/scaladsl/marshalling/MarshallingSpec.scala index 036078e8c8..8d07af4548 100644 --- a/akka-http-tests/src/test/scala/akka/http/scaladsl/marshalling/MarshallingSpec.scala +++ b/akka-http-tests/src/test/scala/akka/http/scaladsl/marshalling/MarshallingSpec.scala @@ -27,6 +27,9 @@ class MarshallingSpec extends FreeSpec with Matchers with BeforeAndAfterAll with "StringMarshaller should marshal strings to `text/plain` content in UTF-8" in { marshal("Ha“llo") shouldEqual HttpEntity("Ha“llo") } + "DoneMarshaller should enable marshalling of akka.Done" in { + marshal(akka.Done) shouldEqual HttpEntity("") + } "CharArrayMarshaller should marshal char arrays to `text/plain` content in UTF-8" in { marshal("Ha“llo".toCharArray) shouldEqual HttpEntity("Ha“llo") } diff --git a/akka-http/src/main/scala/akka/http/scaladsl/marshalling/PredefinedToEntityMarshallers.scala b/akka-http/src/main/scala/akka/http/scaladsl/marshalling/PredefinedToEntityMarshallers.scala index f8c80906ae..e108053d0a 100644 --- a/akka-http/src/main/scala/akka/http/scaladsl/marshalling/PredefinedToEntityMarshallers.scala +++ b/akka-http/src/main/scala/akka/http/scaladsl/marshalling/PredefinedToEntityMarshallers.scala @@ -7,6 +7,7 @@ package akka.http.scaladsl.marshalling import java.nio.CharBuffer import akka.http.scaladsl.model.MediaTypes._ +import akka.http.scaladsl.model.ContentTypes.`text/plain(UTF-8)` import akka.http.scaladsl.model._ import akka.util.ByteString @@ -35,6 +36,11 @@ trait PredefinedToEntityMarshallers extends MultipartMarshallers { HttpEntity(contentType, array) } else HttpEntity.Empty + implicit val DoneMarshaller: ToEntityMarshaller[akka.Done] = + Marshaller.withFixedContentType(`text/plain(UTF-8)`) { done ⇒ + HttpEntity(`text/plain(UTF-8)`, "") + } + implicit val StringMarshaller: ToEntityMarshaller[String] = stringMarshaller(`text/plain`) def stringMarshaller(mediaType: MediaType.WithOpenCharset): ToEntityMarshaller[String] = Marshaller.withOpenCharset(mediaType) { (s, cs) ⇒ HttpEntity(mediaType withCharset cs, s) } diff --git a/akka-http/src/main/scala/akka/http/scaladsl/marshalling/PredefinedToResponseMarshallers.scala b/akka-http/src/main/scala/akka/http/scaladsl/marshalling/PredefinedToResponseMarshallers.scala index 9fb1628d75..55854f2bbf 100644 --- a/akka-http/src/main/scala/akka/http/scaladsl/marshalling/PredefinedToResponseMarshallers.scala +++ b/akka-http/src/main/scala/akka/http/scaladsl/marshalling/PredefinedToResponseMarshallers.scala @@ -4,6 +4,8 @@ package akka.http.scaladsl.marshalling +import akka.stream.impl.ConstantFun + import scala.collection.immutable import akka.http.scaladsl.util.FastFuture._ import akka.http.scaladsl.model.MediaTypes._ @@ -18,7 +20,7 @@ trait PredefinedToResponseMarshallers extends LowPriorityToResponseMarshallerImp implicit m: ToEntityMarshaller[T]): ToResponseMarshaller[T] = fromStatusCodeAndHeadersAndValue compose (t ⇒ (status, headers, t)) - implicit val fromResponse: TRM[HttpResponse] = Marshaller.opaque(conforms) + implicit val fromResponse: TRM[HttpResponse] = Marshaller.opaque(ConstantFun.scalaIdentityFunction) implicit val fromStatusCode: TRM[StatusCode] = Marshaller.withOpenCharset(`text/plain`) { (status, charset) ⇒