Skip to content

Handle byte string format according to specification #542

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

Merged
merged 8 commits into from
Mar 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ assemblyMergeStrategy in assembly := {
oldStrategy(x)
}

val exampleFrameworkSuites = Map(
"scala" -> List(
("akka-http", "akkaHttp", List("client", "server")),
("endpoints", "endpoints", List("client")),
("http4s", "http4s", List("client", "server"))
),
"java" -> List(
("dropwizard", "dropwizard", List("client", "server")),
("spring-mvc", "springMvc", List("server"))
)
)


val scalaFrameworks = exampleFrameworkSuites("scala").map(_._2)
val javaFrameworks = exampleFrameworkSuites("java").map(_._2)

import com.twilio.guardrail.sbt.ExampleCase
def sampleResource(name: String): java.io.File = file(s"modules/sample/src/main/resources/${name}")
val exampleCases: List[ExampleCase] = List(
Expand Down Expand Up @@ -77,27 +93,16 @@ val exampleCases: List[ExampleCase] = List(
ExampleCase(sampleResource("plain.json"), "tests.dtos"),
ExampleCase(sampleResource("polymorphism.yaml"), "polymorphism"),
ExampleCase(sampleResource("polymorphism-mapped.yaml"), "polymorphismMapped"),
ExampleCase(sampleResource("polymorphism-nested.yaml"), "polymorphismNested").frameworks(Set("akka-http", "endpoints", "http4s")),
ExampleCase(sampleResource("polymorphism-nested.yaml"), "polymorphismNested").frameworks(scalaFrameworks.toSet),
ExampleCase(sampleResource("raw-response.yaml"), "raw"),
ExampleCase(sampleResource("redaction.yaml"), "redaction"),
ExampleCase(sampleResource("server1.yaml"), "tracer").args("--tracing"),
ExampleCase(sampleResource("server2.yaml"), "tracer").args("--tracing"),
ExampleCase(sampleResource("pathological-parameters.yaml"), "pathological"),
ExampleCase(sampleResource("response-headers.yaml"), "responseHeaders"),
ExampleCase(sampleResource("binary.yaml"), "binary").frameworks(Set("http4s")),
ExampleCase(sampleResource("conflicting-names.yaml"), "conflictingNames")
)

val exampleFrameworkSuites = Map(
"scala" -> List(
("akka-http", "akkaHttp", List("client", "server")),
("endpoints", "endpoints", List("client")),
("http4s", "http4s", List("client", "server"))
),
"java" -> List(
("dropwizard", "dropwizard", List("client", "server")),
("spring-mvc", "springMvc", List("server"))
)
ExampleCase(sampleResource("conflicting-names.yaml"), "conflictingNames"),
ExampleCase(sampleResource("base64.yaml"), "base64").frameworks(scalaFrameworks.toSet),
)

def exampleArgs(language: String): List[List[String]] = exampleCases
Expand Down Expand Up @@ -142,9 +147,6 @@ artifact in (Compile, assembly) := {

addArtifact(artifact in (Compile, assembly), assembly)

val scalaFrameworks = exampleFrameworkSuites("scala").map(_._2)
val javaFrameworks = exampleFrameworkSuites("java").map(_._2)

addCommandAlias("resetSample", "; " ++ (scalaFrameworks ++ javaFrameworks).map(x => s"${x}Sample/clean").mkString(" ; "))

// Deprecated command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ object SwaggerUtil {
case (Some("string"), Some("email")) => stringType(None)
case (Some("string"), Some("date")) => dateType()
case (Some("string"), Some("date-time")) => dateTimeType()
case (Some("string"), Some("byte")) => bytesType()
case (Some("string"), fmt @ Some("binary")) => fileType(None).map(log(fmt, _))
case (Some("string"), fmt) => stringType(fmt).map(log(fmt, _))
case (Some("number"), Some("float")) => floatType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ object JavaGenerator {
)
)

case BytesType() => Target.raiseError("format: bytes not supported for Java")
case DateType() => safeParseType("java.time.LocalDate")
case DateTimeType() => safeParseType("java.time.OffsetDateTime")
case UUIDType() => safeParseType("java.util.UUID")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ object ScalaGenerator {
case AlterMethodParameterName(param, name) =>
Target.pure(param.copy(name = name))

case BytesType() => Target.pure(t"Base64String")
case DateType() => Target.pure(t"java.time.LocalDate")
case DateTimeType() => Target.pure(t"java.time.OffsetDateTime")
case UUIDType() => Target.pure(t"java.util.UUID")
Expand Down Expand Up @@ -245,6 +246,22 @@ object ScalaGenerator {
ev.addPath(value)
}
}

class Base64String(val data: Array[Byte]) extends AnyVal {
override def toString() = "Base64String(" + data.toString() + ")"
}
object Base64String {
def apply(bytes: Array[Byte]): Base64String = new Base64String(bytes)
def unapply(value: Base64String): Option[Array[Byte]] = Some(value.data)
private[this] val encoder = java.util.Base64.getEncoder
implicit val encode: Encoder[Base64String] =
Encoder[String].contramap[Base64String](v => new String(encoder.encode(v.data)))

private[this] val decoder = java.util.Base64.getDecoder
implicit val decode: Decoder[Base64String] =
Decoder[String].emapTry(v => scala.util.Try(decoder.decode(v))).map(new Base64String(_))
}

}
"""
Target.pure(Some(WriteTree(pkgPath.resolve("Implicits.scala"), sourceToBytes(implicits))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ case class SelectType[L <: LA](typeNames: NonEmptyList[String])
case class SelectTerm[L <: LA](termNames: NonEmptyList[String]) extends ScalaTerm[L, L#Term]
case class AlterMethodParameterName[L <: LA](param: L#MethodParameter, name: L#TermName) extends ScalaTerm[L, L#MethodParameter]

case class BytesType[L <: LA]() extends ScalaTerm[L, L#Type]
case class UUIDType[L <: LA]() extends ScalaTerm[L, L#Type]
case class DateType[L <: LA]() extends ScalaTerm[L, L#Type]
case class DateTimeType[L <: LA]() extends ScalaTerm[L, L#Type]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class ScalaTerms[L <: LA, F[_]](implicit I: InjectK[ScalaTerm[L, ?], F]) {
def alterMethodParameterName(param: L#MethodParameter, name: L#TermName): Free[F, L#MethodParameter] =
Free.inject[ScalaTerm[L, ?], F](AlterMethodParameterName(param, name))

def bytesType(): Free[F, L#Type] = Free.inject[ScalaTerm[L, ?], F](BytesType())
def uuidType(): Free[F, L#Type] = Free.inject[ScalaTerm[L, ?], F](UUIDType())
def dateType(): Free[F, L#Type] = Free.inject[ScalaTerm[L, ?], F](DateType())
def dateTimeType(): Free[F, L#Type] = Free.inject[ScalaTerm[L, ?], F](DateTimeType())
Expand Down
8 changes: 6 additions & 2 deletions modules/codegen/src/test/scala/tests/core/TypesTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class TypesTest extends FunSuite with Matchers with SwaggerSpecRunner {
| date_time:
| type: string
| format: date-time
| byte:
| type: string
| format: byte
| long:
| type: integer
| format: int64
Expand Down Expand Up @@ -99,6 +102,7 @@ class TypesTest extends FunSuite with Matchers with SwaggerSpecRunner {
string: Option[String] = None,
date: Option[java.time.LocalDate] = None,
date_time: Option[java.time.OffsetDateTime] = None,
byte: Option[Base64String] = None,
long: Option[Long] = None,
int: Option[Int] = None,
float: Option[Float] = None,
Expand All @@ -118,9 +122,9 @@ class TypesTest extends FunSuite with Matchers with SwaggerSpecRunner {
object Types {
implicit val encodeTypes: Encoder.AsObject[Types] = {
val readOnlyKeys = Set[String]()
Encoder.AsObject.instance[Types](a => JsonObject.fromIterable(Vector(("array", a.array.asJson), ("map", a.map.asJson), ("obj", a.obj.asJson), ("bool", a.bool.asJson), ("string", a.string.asJson), ("date", a.date.asJson), ("date_time", a.date_time.asJson), ("long", a.long.asJson), ("int", a.int.asJson), ("float", a.float.asJson), ("double", a.double.asJson), ("number", a.number.asJson), ("integer", a.integer.asJson), ("untyped", a.untyped.asJson), ("custom", a.custom.asJson), ("customComplex", a.customComplex.asJson), ("nested", a.nested.asJson), ("nestedArray", a.nestedArray.asJson), ("requiredArray", a.requiredArray.asJson)))).mapJsonObject(_.filterKeys(key => !(readOnlyKeys contains key)))
Encoder.AsObject.instance[Types](a => JsonObject.fromIterable(Vector(("array", a.array.asJson), ("map", a.map.asJson), ("obj", a.obj.asJson), ("bool", a.bool.asJson), ("string", a.string.asJson), ("date", a.date.asJson), ("date_time", a.date_time.asJson), ("byte", a.byte.asJson), ("long", a.long.asJson), ("int", a.int.asJson), ("float", a.float.asJson), ("double", a.double.asJson), ("number", a.number.asJson), ("integer", a.integer.asJson), ("untyped", a.untyped.asJson), ("custom", a.custom.asJson), ("customComplex", a.customComplex.asJson), ("nested", a.nested.asJson), ("nestedArray", a.nestedArray.asJson), ("requiredArray", a.requiredArray.asJson)))).mapJsonObject(_.filterKeys(key => !(readOnlyKeys contains key)))
}
implicit val decodeTypes: Decoder[Types] = new Decoder[Types] { final def apply(c: HCursor): Decoder.Result[Types] = for (v0 <- c.downField("array").as[Option[Vector[Boolean]]]; v1 <- c.downField("map").as[Option[Map[String, Boolean]]]; v2 <- c.downField("obj").as[Option[io.circe.Json]]; v3 <- c.downField("bool").as[Option[Boolean]]; v4 <- c.downField("string").as[Option[String]]; v5 <- c.downField("date").as[Option[java.time.LocalDate]]; v6 <- c.downField("date_time").as[Option[java.time.OffsetDateTime]]; v7 <- c.downField("long").as[Option[Long]]; v8 <- c.downField("int").as[Option[Int]]; v9 <- c.downField("float").as[Option[Float]]; v10 <- c.downField("double").as[Option[Double]]; v11 <- c.downField("number").as[Option[BigDecimal]]; v12 <- c.downField("integer").as[Option[BigInt]]; v13 <- c.downField("untyped").as[Option[io.circe.Json]]; v14 <- c.downField("custom").as[Option[Foo]]; v15 <- c.downField("customComplex").as[Option[Foo[Bar]]]; v16 <- c.downField("nested").as[Option[Types.Nested]]; v17 <- c.downField("nestedArray").as[Option[Vector[Types.NestedArray]]]; v18 <- c.downField("requiredArray").as[Vector[String]]) yield Types(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18) }
implicit val decodeTypes: Decoder[Types] = new Decoder[Types] { final def apply(c: HCursor): Decoder.Result[Types] = for (v0 <- c.downField("array").as[Option[Vector[Boolean]]]; v1 <- c.downField("map").as[Option[Map[String, Boolean]]]; v2 <- c.downField("obj").as[Option[io.circe.Json]]; v3 <- c.downField("bool").as[Option[Boolean]]; v4 <- c.downField("string").as[Option[String]]; v5 <- c.downField("date").as[Option[java.time.LocalDate]]; v6 <- c.downField("date_time").as[Option[java.time.OffsetDateTime]]; v7 <- c.downField("byte").as[Option[Base64String]]; v8 <- c.downField("long").as[Option[Long]]; v9 <- c.downField("int").as[Option[Int]]; v10 <- c.downField("float").as[Option[Float]]; v11 <- c.downField("double").as[Option[Double]]; v12 <- c.downField("number").as[Option[BigDecimal]]; v13 <- c.downField("integer").as[Option[BigInt]]; v14 <- c.downField("untyped").as[Option[io.circe.Json]]; v15 <- c.downField("custom").as[Option[Foo]]; v16 <- c.downField("customComplex").as[Option[Foo[Bar]]]; v17 <- c.downField("nested").as[Option[Types.Nested]]; v18 <- c.downField("nestedArray").as[Option[Vector[Types.NestedArray]]]; v19 <- c.downField("requiredArray").as[Vector[String]]) yield Types(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19) }
case class Nested(prop1: Option[String] = None)
object Nested {
implicit val encodeNested: Encoder.AsObject[Nested] = {
Expand Down
72 changes: 72 additions & 0 deletions modules/sample-http4s/src/test/scala/core/issues/Issue542.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package core.issues

import cats.effect.IO
import cats.data.Kleisli
import org.http4s._
import org.http4s.circe._
import org.http4s.client.{ Client => Http4sClient }
import org.http4s.headers._
import org.http4s.implicits._
import cats.instances.future._
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.time.SpanSugar._
import org.scalatest.{ EitherValues, FunSuite, Matchers, OptionValues }
import tests.scalatest.EitherTValues

class Issue542Suite extends FunSuite with Matchers with EitherValues with ScalaFutures with EitherTValues with OptionValues {
override implicit val patienceConfig = PatienceConfig(10 seconds, 1 second)

test("base64 bytes can be sent") {
import base64.server.http4s.{ FooResponse, Handler, Resource }
import base64.server.http4s.definitions.Foo
import base64.server.http4s.Implicits.Base64String

val route = new Resource[IO]().routes(new Handler[IO] {
def foo(respond: FooResponse.type)(): IO[FooResponse] = IO.pure(respond.Ok(Foo(Some(new Base64String("foo".getBytes())))))
})

val client = Http4sClient.fromHttpApp[IO](route.orNotFound)

val req = Request[IO](method = Method.GET, uri = Uri.unsafeFromString("/foo"))

client
.fetch(req)({
case Status.Ok(resp) =>
resp.status should equal(Status.Ok)
resp.contentType should equal(Some(`Content-Type`(MediaType.application.json)))
resp.contentLength should equal(Some(16))
jsonOf[IO, Foo].decode(resp, strict = false).rightValue
})
.unsafeRunSync()
.value
.value
.data should equal("foo".getBytes())
}

test("base64 bytes can be received") {
import base64.client.http4s.Client
import base64.client.http4s.definitions.Foo
import base64.client.http4s.Implicits.Base64String
import org.http4s.dsl._

def staticClient: Http4sClient[IO] = {
implicit val fooOkEncoder = jsonEncoderOf[IO, Foo]
val response = new Http4sDsl[IO] {
def route: HttpApp[IO] = Kleisli.liftF(Ok(Foo(Some(Base64String("foo".getBytes())))))
}
Http4sClient.fromHttpApp[IO](response.route)
}

Client
.httpClient(staticClient, "http://localhost:80")
.foo()
.attempt
.unsafeRunSync()
.fold(
_ => fail("Error"),
_.fold(
handleOk = _.value.value.data should equal("foo".getBytes())
)
)
}
}
27 changes: 27 additions & 0 deletions modules/sample/src/main/resources/base64.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
swagger: "2.0"
info:
title: Whatever
version: 1.0.0
host: localhost:1234
schemes:
- http
consumes:
- application/json
produces:
- application/json
paths:
/foo:
get:
operationId: foo
responses:
'200':
description: foo
schema:
$ref: '#/definitions/Foo'
definitions:
Foo:
type: object
properties:
value:
type: string
format: byte