Skip to content

Commit

Permalink
json: serialize default values in server responses
Browse files Browse the repository at this point in the history
Upstream Twirp recomends this behaviour to reduce confusion.
This is fully backwards-compatible.

See-Also: twitchtv/twirp#271
  • Loading branch information
ccmtaylor committed Nov 11, 2020
1 parent 073fbff commit a0a2a89
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.twitter.finagle.http.{MediaType, Request, Response, Status}
import com.twitter.finagle.{Filter, Service}
import com.twitter.io.Buf
import com.twitter.util.Future
import scalapb.json4s.JsonFormat
import scalapb.json4s.{JsonFormat, Printer}
import scalapb.{GeneratedMessage, GeneratedMessageCompanion}

/**
Expand All @@ -17,6 +17,9 @@ private[twinagle] class TwirpEndpointFilter[
Req <: GeneratedMessage: GeneratedMessageCompanion,
Rep <: GeneratedMessage: GeneratedMessageCompanion
] extends Filter[Request, Response, Req, Rep] {

val printer = new Printer().includingDefaultValueFields

override def apply(
request: Request,
service: Service[Req, Rep]
Expand All @@ -26,7 +29,7 @@ private[twinagle] class TwirpEndpointFilter[
service(input).map { r =>
val response = Response(Status.Ok)
response.contentType = MediaType.Json
response.contentString = JsonFormat.toJsonString(r)
response.contentString = printer.print(r)
response
}
case Some("application/protobuf") =>
Expand Down
6 changes: 5 additions & 1 deletion runtime/src/test/protobuf/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ syntax = "proto3";

package com.soundcloud.twinagle.test;

message TestMessage {};
message TestMessage {};

message HasField {
int32 foo = 1;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.soundcloud.twinagle

import com.soundcloud.twinagle.test.TestMessage
import com.soundcloud.twinagle.test.{HasField,TestMessage}
import com.twitter.finagle.Service
import com.twitter.finagle.http.{Request, Status}
import com.twitter.util.{Await, Future, Throw}
Expand Down Expand Up @@ -57,6 +57,20 @@ class TwirpEndpointFilterSpec extends Specification {
response.status ==== Status.Ok
response.contentString ==== "{}"
}

"serializes default values" in {
val svc = new TwirpEndpointFilter[HasField, HasField] andThen
Service.mk[HasField, HasField](msg => Future.value(msg))

val request = Request()
request.contentType = "application/json; charset=UTF-8"
request.contentString = "{}"

val response = Await.result(svc(request))

response.status ==== Status.Ok
response.contentString ==== """{"foo":0}"""
}
}

"un-supported content-type" in new Context {
Expand All @@ -71,7 +85,7 @@ class TwirpEndpointFilterSpec extends Specification {
}
}

"unspecified" in new Context {
"unspecified content-type" in new Context {
val request = Request()
request.contentString = "{}"

Expand Down

0 comments on commit a0a2a89

Please sign in to comment.