Skip to content

Commit

Permalink
Merge pull request #112 from innFactory/feature/update-smithy4s
Browse files Browse the repository at this point in the history
chore: update smithy4s version from 0.17.2 to 0.17.4
patsta32 authored Mar 3, 2023
2 parents 5139268 + 2b35c62 commit 56ee6ee
Showing 9 changed files with 86 additions and 33 deletions.
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -21,8 +21,10 @@ val githubSettings = Seq(
)
)

scalaVersion := "2.13.8"

val defaultProjectSettings = Seq(
scalaVersion := "2.13.10",
scalaVersion := "2.13.8",
organization := "de.innfactory",
version := releaseVersion
) ++ githubSettings
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ object Dependencies {
val typesafePlay = "com.typesafe.play" %% "play" % playVersion

val scalaVersion = "2.13.8"
val smithy4sVersion = "0.17.2"
val smithy4sVersion = "0.17.4"
val smithyCore = "com.disneystreaming.smithy4s" %% "smithy4s-core" % smithy4sVersion
val smithyJson = "com.disneystreaming.smithy4s" %% "smithy4s-json" % smithy4sVersion
val smithy4sCompliance = "com.disneystreaming.smithy4s" %% "smithy4s-compliance-tests" % smithy4sVersion
3 changes: 2 additions & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
sbt.version = 1.7.2
sbt.version = 1.8.2
scala.version = 2.13.2
16 changes: 10 additions & 6 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
addSbtPlugin("com.codecommit" % "sbt-github-packages" % "0.5.3")
addSbtPlugin("org.wartremover" % "sbt-wartremover" % "3.0.11")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.0")
addSbtPlugin("com.disneystreaming.smithy4s" % "smithy4s-sbt-codegen" % "0.17.2")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.19")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.9.3")
addSbtPlugin("com.codecommit" %% "sbt-github-packages" % "0.5.3")
addSbtPlugin("org.wartremover" %% "sbt-wartremover" % "3.0.11")
addSbtPlugin("org.scalameta" %% "sbt-scalafmt" % "2.5.0")
addSbtPlugin("com.disneystreaming.smithy4s" %% "smithy4s-sbt-codegen" % "0.17.4")
addSbtPlugin("com.typesafe.play" %% "sbt-plugin" % "2.8.19")
addSbtPlugin("org.scoverage" %% "sbt-scoverage" % "1.9.3")

ThisBuild / dependencyOverrides ++= Seq(
"org.scala-lang.modules" %% "scala-xml" % "1.3.0"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package de.innfactory.smithy4play

import smithy4s.{ HintMask, Schema }
import smithy4s.http.{ CodecAPI, PayloadError }
import smithy4s.http.json.codecs
import smithy4s.internals.InputOutput

object CodecUtils {

private val codecs: codecs =
smithy4s.http.json.codecs(alloy.SimpleRestJson.protocol.hintMask ++ HintMask(InputOutput))

def writeInputToBody[I](input: I, inputSchema: Schema[I], codecAPI: CodecAPI): Array[Byte] = {
val codec = codecAPI.compileCodec(inputSchema)
codecAPI.writeToArray(codec, input)
}

def readFromBytes[E](data: Array[Byte], inputSchema: Schema[E], codecAPI: CodecAPI): Either[PayloadError, E] =
codecAPI.decodeFromByteArray(codecAPI.compileCodec(inputSchema), data)

def writeEntityToJsonBytes[E](e: E, schema: Schema[E]) = writeInputToBody[E](e, schema, codecs)

def readFromJsonBytes[E](bytes: Array[Byte], schema: Schema[E]): Option[E] =
readFromBytes(bytes, schema, codecs).toOption

def extractCodec(headers: Map[String, Seq[String]]): CodecAPI = {
val contentType =
headers.getOrElse("content-type", List("application/json"))
val codecApi = contentType match {
case List("application/json") => codecs
case _ => CodecAPI.nativeStringsAndBlob(codecs)
}
codecApi
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package de.innfactory
package smithy4play
package client
import smithy4s.http.json.codecs
import smithy4s.{ Endpoint, HintMask, Schema }
import smithy4s.{ Endpoint, Schema }
import smithy4s.http.{ CaseInsensitive, CodecAPI, HttpEndpoint, Metadata, MetadataError, PayloadError }
import cats.implicits._
import smithy4s.internals.InputOutput

import scala.concurrent.{ ExecutionContext, Future }

@@ -18,9 +16,6 @@ private[smithy4play] class SmithyPlayClientEndpoint[Op[_, _, _, _, _], I, E, O,
client: RequestClient
)(implicit executionContext: ExecutionContext) {

private val codecs: codecs =
smithy4s.http.json.codecs(alloy.SimpleRestJson.protocol.hintMask ++ HintMask(InputOutput))

private val inputSchema: Schema[I] = endpoint.input
private val outputSchema: Schema[O] = endpoint.output

@@ -38,7 +33,7 @@ private[smithy4play] class SmithyPlayClientEndpoint[Op[_, _, _, _, _], I, E, O,
val headers = metadata.headers.map(x => (x._1.toString.toLowerCase, x._2))
val headersWithAuth = if (additionalHeaders.isDefined) headers.combine(additionalHeaders.get) else headers
val code = httpEndpoint.code
val codecApi: CodecAPI = extractCodec(headers)
val codecApi: CodecAPI = CodecUtils.extractCodec(headers)
val send = client.send(httpEndpoint.method.toString, path, headersWithAuth, _)
val response = if (inputHasBody) {
val codec = codecApi.compileCodec(inputSchema)
@@ -48,16 +43,6 @@ private[smithy4play] class SmithyPlayClientEndpoint[Op[_, _, _, _, _], I, E, O,
decodeResponse(response, code)
}

private def extractCodec(headers: Map[String, Seq[String]]): CodecAPI = {
val contentType =
headers.getOrElse("content-type", List("application/json"))
val codecApi = contentType match {
case List("application/json") => codecs
case _ => CodecAPI.nativeStringsAndBlob(codecs)
}
codecApi
}

private def decodeResponse(
response: Future[SmithyClientResponse],
expectedCode: Int
@@ -77,7 +62,7 @@ private[smithy4play] class SmithyPlayClientEndpoint[Op[_, _, _, _, _], I, E, O,
case None =>
for {
metadataPartial <- outputMetadataDecoder.decode(metadata)
codecApi = extractCodec(headers)
codecApi = CodecUtils.extractCodec(headers)
bodyPartial <-
codecApi.decodeFromByteArrayPartial(codecApi.compileCodec(outputSchema), response.body.get)
} yield metadataPartial.combine(bodyPartial)
2 changes: 1 addition & 1 deletion smithy4playTest/app/controller/TestController.scala
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ package controller

import cats.data.{ EitherT, Kleisli }
import controller.models.TestError
import de.innfactory.smithy4play.{ AutoRouting, ContextRoute, ContextRouteError }
import de.innfactory.smithy4play.{ AutoRouting, CodecUtils, ContextRoute, ContextRouteError }
import play.api.mvc.ControllerComponents
import smithy4s.ByteArray
import testDefinitions.test._
27 changes: 22 additions & 5 deletions smithy4playTest/test/TestControllerTest.scala
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import controller.models.TestError
import de.innfactory.smithy4play.CodecUtils
import de.innfactory.smithy4play.client.GenericAPIClient.EnhancedGenericAPIClient
import de.innfactory.smithy4play.client.{ RequestClient, SmithyClientResponse }
import de.innfactory.smithy4play.client.{RequestClient, SmithyClientResponse}
import de.innfactory.smithy4play.client.SmithyPlayTestUtils._
import de.innfactory.smithy4play.compliancetests.ComplianceClient
import org.scalatestplus.play.{ BaseOneAppPerSuite, FakeApplicationFactory, PlaySpec }
import models.TestJson
import org.scalatestplus.play.{BaseOneAppPerSuite, FakeApplicationFactory, PlaySpec}
import play.api.Application
import play.api.Play.materializer
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.json.{ Json, OWrites }
import play.api.mvc.{ AnyContentAsEmpty, Result }
import play.api.libs.json.{Json, OWrites}
import play.api.mvc.{AnyContentAsEmpty, Result}
import play.api.test.FakeRequest
import play.api.test.Helpers._
import testDefinitions.test.{ TestControllerServiceGen, TestRequestBody }
import testDefinitions.test.{SimpleTestResponse, TestControllerServiceGen, TestRequestBody}
import smithy4s.ByteArray

import java.io.File
@@ -149,5 +151,20 @@ class TestControllerTest extends PlaySpec with BaseOneAppPerSuite with FakeAppli

result.statusCode mustBe 401
}

"manual writing json" in {

val writtenData = CodecUtils.writeEntityToJsonBytes(SimpleTestResponse(Some("Test")), SimpleTestResponse.schema)

val writtenJson = Json.parse(writtenData).as[TestJson]

val readData = CodecUtils.readFromJsonBytes(
Json.toBytes(Json.toJson(TestJson(Some("Test")))),
SimpleTestResponse.schema
)

writtenJson.message mustBe Some("Test")
readData.get.message mustBe Some("Test")
}
}
}
9 changes: 9 additions & 0 deletions smithy4playTest/test/models/TestJson.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package models

import play.api.libs.json.Json

case class TestJson(message: Option[String])

object TestJson {
implicit val format = Json.format[TestJson]
}

0 comments on commit 56ee6ee

Please sign in to comment.