Today if found out that content negotiation is not done in 
Marshal(result).to[__] case. (I should had tests for that)
Only solution I can find is to copy Marshal.toResponseFor method as it is 
not expandable at all. and add flatMap(identity) there:

 implicit def toFutureResponseMarshallable[A](_value: A)(implicit 
_marshaller: FutureResponseMarshaller[A]): ToResponseMarshallable = new 
ToResponseMarshallable{
    type T = A
    def value: T = _value
    implicit def marshaller: ToResponseMarshaller[A] = null

    override def apply(request: HttpRequest)(implicit ec: ExecutionContext): 
Future[HttpResponse] = {
      import akka.http.scaladsl.util.FastFuture._
      import akka.http.scaladsl.marshalling.Marshal._
      val ctn = ContentNegotiator(request.headers)
      _marshaller(value).fast.map { marshallings ⇒
        val supportedAlternatives: List[ContentNegotiator.Alternative] =
          marshallings.collect {
            case Marshalling.WithFixedContentType(ct, _) ⇒ ContentNegotiator
.Alternative(ct)
            case Marshalling.WithOpenCharset(mt, _)      ⇒ ContentNegotiator
.Alternative(mt)
          }(collection.breakOut)
        val bestMarshal = {
          if (supportedAlternatives.nonEmpty) {
            ctn.pickContentType(supportedAlternatives).flatMap {
              case best @ (_: ContentType.Binary | _: ContentType.
WithFixedCharset | _: ContentType.WithMissingCharset) ⇒
                marshallings collectFirst { case Marshalling.
WithFixedContentType(`best`, marshal) ⇒ marshal }
              case best @ ContentType.WithCharset(bestMT, bestCS) ⇒
                marshallings collectFirst {
                  case Marshalling.WithFixedContentType(`best`, marshal) ⇒ 
marshal
                  case Marshalling.WithOpenCharset(`bestMT`, marshal)    ⇒ 
() ⇒ marshal(bestCS)
                }
            }
          } else None
        } orElse {
          marshallings collectFirst { case Marshalling.Opaque(marshal) ⇒ 
marshal }
        } getOrElse {
          throw UnacceptableResponseContentTypeException(
supportedAlternatives.toSet)
        }
        bestMarshal()
      *}.flatMap(identity)*
    }
  }




On Wednesday, September 27, 2017 at 12:34:11 PM UTC+3, Muntis Grube wrote:
>
> Thanks. It seams that it worked: 
>
>   type FutureResponse = Future[HttpResponse]
>   type FutureResponseMarshaller[T] = Marshaller[T, FutureResponse]
>
>   def httpResponse(iterator: Iterator[Data])(implicit ec: ExecutionContext
> ) = {
>     Source.fromGraph(sourceGrap(iterator)).runWith(entitySink).map(entity 
> => HttpResponse(entity = entity))
>   }
>
>   val toResponseIteratorJsonMarshaller: FutureResponseMarshaller[Iterator[
> Data]] =
>     Marshaller.withFixedContentType(`application/json`) {
>       result => httpResponse(result)
>     }
>
>   implicit val toResponseIteratorMarshaller: FutureResponseMarshaller[
> Iterator[Data]] =
>     Marshaller.oneOf(
>       toResponseIteratorJsonMarshaller,
>       toResponseIteratorOdsMarshaller,
>       toResponseIteratorExcelMarshaller
>     )
>
>    complete {
>      val result = ???
>      Marshal(result).to[FutureResponse].flatMap(identity)
>    }
>
>
>
>
> On Tuesday, September 26, 2017 at 4:13:15 PM UTC+3, 
> johannes...@lightbend.com wrote:
>>
>> Oops, one should read the whole question before answering... Just saw 
>> that you already tried that. Unfortunately, it seems that this is indeed a 
>> shortcoming of the current model.
>>
>> I guess with a bit of fiddling you could try making all of those 
>> marshallers marshal to `Future[HttpResponse]` instead of `HttpResponse` and 
>> then use something like
>>
>>
>>
>> val toResponseIteratorJsonMarshaller: Marshaller[Iterator[Data], 
>> Future[HttpResponse]] =
>>     Marshaller.withFixedContentType(`application/json`) {
>>       result => httpResponse(result)
>>     }
>>
>>   implicit val toResponseIteratorMarshaller: Marshaller[Iterator[Data], 
>> Future[HttpResponse]] =
>>     Marshaller.oneOf(
>>       toResponseIteratorJsonMarshaller,
>>       toResponseIteratorOdsMarshaller,
>>       toResponseIteratorExcelMarshaller
>>     )
>>
>> and then in your route:
>>
>> val responseFuture =
>> Marshal(data).toResponseFor(request)(toResponseIteratorMarshaller): // 
>> Future[Future[HttpResponse]]
>>
>> .flatMap(identity)
>> complete(responseFuture)
>>
>> Would be interesting to know if that works.
>>
>> Johannes
>>
>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to