From 9508f090264adab6c0a2d32214f0deeaab9c16ce Mon Sep 17 00:00:00 2001 From: Lintterer Date: Mon, 29 Apr 2024 14:16:34 +0200 Subject: [PATCH 1/6] fix: using correct content type to decode response --- .../innfactory/smithy4play/CodecDecider.scala | 13 ++- .../smithy4play/SmithyPlayEndpoint.scala | 98 ++++++++++--------- .../app/controller/TestController.scala | 6 +- smithy4playTest/test/TestControllerTest.scala | 22 +++-- .../testSpecs/TestController.smithy | 18 ++++ 5 files changed, 97 insertions(+), 60 deletions(-) diff --git a/smithy4play/src/main/scala/de/innfactory/smithy4play/CodecDecider.scala b/smithy4play/src/main/scala/de/innfactory/smithy4play/CodecDecider.scala index 3c769e65..93a0e044 100644 --- a/smithy4play/src/main/scala/de/innfactory/smithy4play/CodecDecider.scala +++ b/smithy4play/src/main/scala/de/innfactory/smithy4play/CodecDecider.scala @@ -5,12 +5,12 @@ import play.api.http.MimeTypes import smithy4s.capability.instances.either._ import smithy4s.codecs.Writer.CachedCompiler import smithy4s.codecs._ -import smithy4s.http.{ HttpResponse, HttpRestSchema, Metadata, MetadataError } +import smithy4s.http.{HttpResponse, HttpRestSchema, Metadata, MetadataError} import smithy4s.json.Json import smithy4s.kinds.PolyFunction -import smithy4s.schema.CachedSchemaCompiler +import smithy4s.schema.{CachedSchemaCompiler, Schema} import smithy4s.xml.Xml -import smithy4s.{ codecs, Blob } +import smithy4s.{Blob, PartialData, codecs} case class CodecDecider(readerConfig: ReaderConfig) { @@ -87,6 +87,13 @@ case class CodecDecider(readerConfig: ReaderConfig) { _ => Right(()) )(eitherZipper) + def test(): Unit = { + val x = metadataEncoder.mapK( + httpRequestMetadataPipe + ) + + } + def httpMessageEncoder( contentType: Seq[String] ): CachedCompiler[HttpResponse[Blob]] = diff --git a/smithy4play/src/main/scala/de/innfactory/smithy4play/SmithyPlayEndpoint.scala b/smithy4play/src/main/scala/de/innfactory/smithy4play/SmithyPlayEndpoint.scala index 64b682f5..ca88e37b 100644 --- a/smithy4play/src/main/scala/de/innfactory/smithy4play/SmithyPlayEndpoint.scala +++ b/smithy4play/src/main/scala/de/innfactory/smithy4play/SmithyPlayEndpoint.scala @@ -14,58 +14,63 @@ import javax.inject.Inject import scala.concurrent.{ ExecutionContext, Future } class SmithyPlayEndpoint[Alg[_[_, _, _, _, _]], F[_] <: ContextRoute[_], Op[_, _, _, _, _], I, E, O, SI, SO]( - service: Service[Alg], - impl: FunctorInterpreter[Op, F], - middleware: Seq[MiddlewareBase], - endpoint: Endpoint[Op, I, E, O, SI, SO], - codecDecider: CodecDecider -)(implicit cc: ControllerComponents, ec: ExecutionContext) - extends AbstractController(cc) { + service: Service[Alg], + impl: FunctorInterpreter[Op, F], + middleware: Seq[MiddlewareBase], + endpoint: Endpoint[Op, I, E, O, SI, SO], + codecDecider: CodecDecider + )(implicit cc: ControllerComponents, ec: ExecutionContext) + extends AbstractController(cc) { private val httpEndpoint: Either[HttpEndpoint.HttpEndpointError, HttpEndpoint[I]] = HttpEndpoint.cast(endpoint.schema) private val serviceHints = service.hints private val endpointHints = endpoint.hints private val serviceContentType: String = serviceHints.toMimeType - private implicit val inputSchema: Schema[I] = endpoint.input - private implicit val outputSchema: Schema[O] = endpoint.output - - def handler(v1: RequestHeader): Handler = + private implicit val inputSchema: Schema[I] = endpoint.input + private implicit val outputSchema: Schema[O] = endpoint.output + private val outputMetadataEncoder: Metadata.Encoder[O] = + Metadata.Encoder.fromSchema(outputSchema) + def handler(v1: RequestHeader): Handler = httpEndpoint.map { httpEp => - Action.async(parse.raw) { implicit request => - if (request.body.size > 0 && request.body.asBytes().isEmpty) { - logger.error( - "received body size does not equal the parsed body size. \n" + - "This is probably due to the body being too large and thus play is unable to parse.\n" + - "Try setting play.http.parser.maxMemoryBuffer in application.conf" - ) - } + Action.async(parse.raw) { implicit request => + if (request.body.size > 0 && request.body.asBytes().isEmpty) { + logger.error( + "received body size does not equal the parsed body size. \n" + + "This is probably due to the body being too large and thus play is unable to parse.\n" + + "Try setting play.http.parser.maxMemoryBuffer in application.conf" + ) + } - implicit val epContentType: ContentType = ContentType(request.contentType.getOrElse(serviceContentType)) - val result = for { - pathParams <- getPathParams(v1, httpEp) - metadata = getMetadata(pathParams, v1) - input <- getInput(request, metadata) - endpointLogic = impl(endpoint.wrap(input)) - .asInstanceOf[Kleisli[RouteResult, RoutingContext, O]] - .map(mapToEndpointResult(httpEp.code)) - chainedMiddlewares = middleware.foldRight(endpointLogic)((a, b) => a.middleware(b.run)) - res <- - chainedMiddlewares.run(RoutingContext.fromRequest(request, serviceHints, endpointHints, v1)) - } yield res - result.value.map { - case Left(value) => handleFailure(value) - case Right(value) => handleSuccess(value) + implicit val epContentType: ContentType = ContentType(request.contentType.getOrElse(serviceContentType)) + val result = for { + pathParams <- getPathParams(v1, httpEp) + metadata = getMetadata(pathParams, v1) + input <- getInput(request, metadata) + endpointLogic = impl(endpoint.wrap(input)) + .asInstanceOf[Kleisli[RouteResult, RoutingContext, O]] + .map(mapToEndpointResult(httpEp.code)) + chainedMiddlewares = middleware.foldRight(endpointLogic)((a, b) => a.middleware(b.run)) + res <- + chainedMiddlewares.run(RoutingContext.fromRequest(request, serviceHints, endpointHints, v1)) + } yield res + result.value.map { + case Left(value) => handleFailure(value) + case Right(value) => handleSuccess(value) + } } } - } .getOrElse(Action(NotFound("404"))) private def mapToEndpointResult( - statusCode: Int - )(output: O)(implicit defaultContentType: ContentType): HttpResponse[Blob] = + statusCode: Int + )(output: O): HttpResponse[Blob] = { + val outputMetadata = outputMetadataEncoder.encode(output).headers.get(CaseInsensitive("content-type")) match { + case Some(value) => value + case None => Seq(serviceContentType) + } codecDecider - .httpMessageEncoder(Seq(defaultContentType.value)) + .httpMessageEncoder(outputMetadata) .fromSchema(outputSchema) .write( HttpResponse( @@ -75,11 +80,12 @@ class SmithyPlayEndpoint[Alg[_[_, _, _, _, _]], F[_] <: ContextRoute[_], Op[_, _ ), output ) + } private def getPathParams( - v1: RequestHeader, - httpEp: HttpEndpoint[I] - )(implicit defaultContentType: ContentType): EitherT[Future, ContextRouteError, Map[String, String]] = + v1: RequestHeader, + httpEp: HttpEndpoint[I] + )(implicit defaultContentType: ContentType): EitherT[Future, ContextRouteError, Map[String, String]] = EitherT( Future( matchRequestPath(v1, httpEp) @@ -94,9 +100,9 @@ class SmithyPlayEndpoint[Alg[_[_, _, _, _, _]], F[_] <: ContextRoute[_], Op[_, _ ) private def getInput( - request: Request[RawBuffer], - metadata: Metadata - )(implicit defaultContentType: ContentType): EitherT[Future, ContextRouteError, I] = + request: Request[RawBuffer], + metadata: Metadata + )(implicit defaultContentType: ContentType): EitherT[Future, ContextRouteError, I] = EitherT { Future { val codec = codecDecider.requestDecoder(Seq(defaultContentType.value)) @@ -138,13 +144,13 @@ class SmithyPlayEndpoint[Alg[_[_, _, _, _, _]], F[_] <: ContextRoute[_], Op[_, _ .withHeaders(error.status.headers.toList: _*) .as(error.contentType) - private def handleSuccess(output: HttpResponse[Blob])(implicit defaultContentType: ContentType): Result = { + private def handleSuccess(output: HttpResponse[Blob]): Result = { val status = Results.Status(output.statusCode) val contentTypeKey = CaseInsensitive("content-type") val outputHeadersWithoutContentType = output.headers.-(contentTypeKey).toList.map(h => (h._1.toString, h._2.head)) val contentType = - output.headers.getOrElse(contentTypeKey, Seq(defaultContentType.value)) + output.headers.getOrElse(contentTypeKey, Seq(serviceContentType)) if (!output.body.isEmpty) { status(output.body.toArray) diff --git a/smithy4playTest/app/controller/TestController.scala b/smithy4playTest/app/controller/TestController.scala index 20ec6951..6c51b118 100644 --- a/smithy4playTest/app/controller/TestController.scala +++ b/smithy4playTest/app/controller/TestController.scala @@ -4,7 +4,7 @@ import cats.data.{ EitherT, Kleisli } import controller.models.TestError import de.innfactory.smithy4play.{ AutoRouting, ContextRoute, ContextRouteError } import play.api.mvc.ControllerComponents -import smithy4s.Blob +import smithy4s.{ Blob, Document } import testDefinitions.test._ import javax.inject.{ Inject, Singleton } @@ -67,4 +67,8 @@ class TestController @Inject() (implicit override def testWithOtherStatusCode(): ContextRoute[Unit] = Kleisli { rc => EitherT.rightT[Future, ContextRouteError](()) } + + override def testWithJsonInputAndBlobOutput(body: JsonInput): ContextRoute[BlobResponse] = Kleisli { rc => + EitherT.rightT[Future, ContextRouteError](BlobResponse(Blob(body.message), "image/png")) + } } diff --git a/smithy4playTest/test/TestControllerTest.scala b/smithy4playTest/test/TestControllerTest.scala index 7bdb8c9b..916f1849 100644 --- a/smithy4playTest/test/TestControllerTest.scala +++ b/smithy4playTest/test/TestControllerTest.scala @@ -3,23 +3,17 @@ import de.innfactory.smithy4play.client.GenericAPIClient.EnhancedGenericAPIClien import de.innfactory.smithy4play.client.SmithyPlayTestUtils._ import de.innfactory.smithy4play.compliancetests.ComplianceClient import models.NodeImplicits.NodeEnhancer -import models.{ TestBase, TestJson } +import models.{TestBase, TestJson} import org.scalatest.time.SpanSugar.convertIntToGrainOfTime import play.api.Application import play.api.inject.guice.GuiceApplicationBuilder -import play.api.libs.json.{ Json, OWrites } +import play.api.libs.json.{Json, OWrites} import play.api.mvc.Result import play.api.test.FakeRequest import play.api.test.Helpers._ -import smithy4s.Blob +import smithy4s.{Blob, Document} import smithy4s.http.CaseInsensitive -import testDefinitions.test.{ - SimpleTestResponse, - TestControllerServiceGen, - TestRequestBody, - TestResponseBody, - TestWithOutputResponse -} +import testDefinitions.test.{JsonInput, SimpleTestResponse, TestControllerServiceGen, TestRequestBody, TestResponseBody, TestWithOutputResponse} import java.io.File import java.nio.file.Files @@ -167,6 +161,14 @@ class TestControllerTest extends TestBase { pngAsBytes mustBe result.body.body } + "route 123 to Blob Endpoint" in { + val testString = "StringToBeParsedCorrectly" + val result = genericClient.testWithJsonInputAndBlobOutput(JsonInput(testString)).awaitRight(global, 5.hours) + + result.statusCode mustBe 200 + testString mustBe result.body.body.toUTF8String + } + "route to Auth Test" in { val result = genericClient.testAuth().awaitLeft diff --git a/smithy4playTest/testSpecs/TestController.smithy b/smithy4playTest/testSpecs/TestController.smithy index 066ffd2c..2de835da 100644 --- a/smithy4playTest/testSpecs/TestController.smithy +++ b/smithy4playTest/testSpecs/TestController.smithy @@ -14,6 +14,7 @@ service TestControllerService { Health TestWithBlob TestWithQuery + TestWithJsonInputAndBlobOutput TestThatReturnsError TestAuth TestWithOtherStatusCode @@ -27,6 +28,17 @@ service TestControllerService { operation TestWithOtherStatusCode { } +@auth([]) +@http(method: "POST", uri: "/jsoninput/bloboutput", code: 200) +operation TestWithJsonInputAndBlobOutput { + input:= { + @httpPayload + @required + body: JsonInput + } + output: BlobResponse +} + @auth([]) @http(method: "POST", uri: "/blob", code: 200) operation TestWithBlob { @@ -134,6 +146,12 @@ structure TestResponseBody { bodyMessage: String } + +structure JsonInput { + @required + message: String +} + @http(method: "GET", uri: "/auth", code: 200) operation TestAuth { } From 934fbb1284224a5a784492ef2f1dbef7f03bac71 Mon Sep 17 00:00:00 2001 From: Lintterer Date: Mon, 29 Apr 2024 14:20:12 +0200 Subject: [PATCH 2/6] chore: scalafmt --- .../innfactory/smithy4play/CodecDecider.scala | 6 +- .../smithy4play/SmithyPlayEndpoint.scala | 78 +++++++++---------- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/smithy4play/src/main/scala/de/innfactory/smithy4play/CodecDecider.scala b/smithy4play/src/main/scala/de/innfactory/smithy4play/CodecDecider.scala index 93a0e044..9b2415ee 100644 --- a/smithy4play/src/main/scala/de/innfactory/smithy4play/CodecDecider.scala +++ b/smithy4play/src/main/scala/de/innfactory/smithy4play/CodecDecider.scala @@ -5,12 +5,12 @@ import play.api.http.MimeTypes import smithy4s.capability.instances.either._ import smithy4s.codecs.Writer.CachedCompiler import smithy4s.codecs._ -import smithy4s.http.{HttpResponse, HttpRestSchema, Metadata, MetadataError} +import smithy4s.http.{ HttpResponse, HttpRestSchema, Metadata, MetadataError } import smithy4s.json.Json import smithy4s.kinds.PolyFunction -import smithy4s.schema.{CachedSchemaCompiler, Schema} +import smithy4s.schema.{ CachedSchemaCompiler, Schema } import smithy4s.xml.Xml -import smithy4s.{Blob, PartialData, codecs} +import smithy4s.{ codecs, Blob, PartialData } case class CodecDecider(readerConfig: ReaderConfig) { diff --git a/smithy4play/src/main/scala/de/innfactory/smithy4play/SmithyPlayEndpoint.scala b/smithy4play/src/main/scala/de/innfactory/smithy4play/SmithyPlayEndpoint.scala index ca88e37b..8cd6f66b 100644 --- a/smithy4play/src/main/scala/de/innfactory/smithy4play/SmithyPlayEndpoint.scala +++ b/smithy4play/src/main/scala/de/innfactory/smithy4play/SmithyPlayEndpoint.scala @@ -14,13 +14,13 @@ import javax.inject.Inject import scala.concurrent.{ ExecutionContext, Future } class SmithyPlayEndpoint[Alg[_[_, _, _, _, _]], F[_] <: ContextRoute[_], Op[_, _, _, _, _], I, E, O, SI, SO]( - service: Service[Alg], - impl: FunctorInterpreter[Op, F], - middleware: Seq[MiddlewareBase], - endpoint: Endpoint[Op, I, E, O, SI, SO], - codecDecider: CodecDecider - )(implicit cc: ControllerComponents, ec: ExecutionContext) - extends AbstractController(cc) { + service: Service[Alg], + impl: FunctorInterpreter[Op, F], + middleware: Seq[MiddlewareBase], + endpoint: Endpoint[Op, I, E, O, SI, SO], + codecDecider: CodecDecider +)(implicit cc: ControllerComponents, ec: ExecutionContext) + extends AbstractController(cc) { private val httpEndpoint: Either[HttpEndpoint.HttpEndpointError, HttpEndpoint[I]] = HttpEndpoint.cast(endpoint.schema) private val serviceHints = service.hints @@ -33,38 +33,38 @@ class SmithyPlayEndpoint[Alg[_[_, _, _, _, _]], F[_] <: ContextRoute[_], Op[_, _ Metadata.Encoder.fromSchema(outputSchema) def handler(v1: RequestHeader): Handler = httpEndpoint.map { httpEp => - Action.async(parse.raw) { implicit request => - if (request.body.size > 0 && request.body.asBytes().isEmpty) { - logger.error( - "received body size does not equal the parsed body size. \n" + - "This is probably due to the body being too large and thus play is unable to parse.\n" + - "Try setting play.http.parser.maxMemoryBuffer in application.conf" - ) - } + Action.async(parse.raw) { implicit request => + if (request.body.size > 0 && request.body.asBytes().isEmpty) { + logger.error( + "received body size does not equal the parsed body size. \n" + + "This is probably due to the body being too large and thus play is unable to parse.\n" + + "Try setting play.http.parser.maxMemoryBuffer in application.conf" + ) + } - implicit val epContentType: ContentType = ContentType(request.contentType.getOrElse(serviceContentType)) - val result = for { - pathParams <- getPathParams(v1, httpEp) - metadata = getMetadata(pathParams, v1) - input <- getInput(request, metadata) - endpointLogic = impl(endpoint.wrap(input)) - .asInstanceOf[Kleisli[RouteResult, RoutingContext, O]] - .map(mapToEndpointResult(httpEp.code)) - chainedMiddlewares = middleware.foldRight(endpointLogic)((a, b) => a.middleware(b.run)) - res <- - chainedMiddlewares.run(RoutingContext.fromRequest(request, serviceHints, endpointHints, v1)) - } yield res - result.value.map { - case Left(value) => handleFailure(value) - case Right(value) => handleSuccess(value) - } + implicit val epContentType: ContentType = ContentType(request.contentType.getOrElse(serviceContentType)) + val result = for { + pathParams <- getPathParams(v1, httpEp) + metadata = getMetadata(pathParams, v1) + input <- getInput(request, metadata) + endpointLogic = impl(endpoint.wrap(input)) + .asInstanceOf[Kleisli[RouteResult, RoutingContext, O]] + .map(mapToEndpointResult(httpEp.code)) + chainedMiddlewares = middleware.foldRight(endpointLogic)((a, b) => a.middleware(b.run)) + res <- + chainedMiddlewares.run(RoutingContext.fromRequest(request, serviceHints, endpointHints, v1)) + } yield res + result.value.map { + case Left(value) => handleFailure(value) + case Right(value) => handleSuccess(value) } } + } .getOrElse(Action(NotFound("404"))) private def mapToEndpointResult( - statusCode: Int - )(output: O): HttpResponse[Blob] = { + statusCode: Int + )(output: O): HttpResponse[Blob] = { val outputMetadata = outputMetadataEncoder.encode(output).headers.get(CaseInsensitive("content-type")) match { case Some(value) => value case None => Seq(serviceContentType) @@ -83,9 +83,9 @@ class SmithyPlayEndpoint[Alg[_[_, _, _, _, _]], F[_] <: ContextRoute[_], Op[_, _ } private def getPathParams( - v1: RequestHeader, - httpEp: HttpEndpoint[I] - )(implicit defaultContentType: ContentType): EitherT[Future, ContextRouteError, Map[String, String]] = + v1: RequestHeader, + httpEp: HttpEndpoint[I] + )(implicit defaultContentType: ContentType): EitherT[Future, ContextRouteError, Map[String, String]] = EitherT( Future( matchRequestPath(v1, httpEp) @@ -100,9 +100,9 @@ class SmithyPlayEndpoint[Alg[_[_, _, _, _, _]], F[_] <: ContextRoute[_], Op[_, _ ) private def getInput( - request: Request[RawBuffer], - metadata: Metadata - )(implicit defaultContentType: ContentType): EitherT[Future, ContextRouteError, I] = + request: Request[RawBuffer], + metadata: Metadata + )(implicit defaultContentType: ContentType): EitherT[Future, ContextRouteError, I] = EitherT { Future { val codec = codecDecider.requestDecoder(Seq(defaultContentType.value)) From 48f1e2bc5845b37eaedcff12cefa6bc37d918c3c Mon Sep 17 00:00:00 2001 From: Lintterer Date: Mon, 29 Apr 2024 15:03:28 +0200 Subject: [PATCH 3/6] fix: using request content type as fallback --- .../de/innfactory/smithy4play/SmithyPlayEndpoint.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/smithy4play/src/main/scala/de/innfactory/smithy4play/SmithyPlayEndpoint.scala b/smithy4play/src/main/scala/de/innfactory/smithy4play/SmithyPlayEndpoint.scala index 8cd6f66b..962d01b8 100644 --- a/smithy4play/src/main/scala/de/innfactory/smithy4play/SmithyPlayEndpoint.scala +++ b/smithy4play/src/main/scala/de/innfactory/smithy4play/SmithyPlayEndpoint.scala @@ -64,10 +64,10 @@ class SmithyPlayEndpoint[Alg[_[_, _, _, _, _]], F[_] <: ContextRoute[_], Op[_, _ private def mapToEndpointResult( statusCode: Int - )(output: O): HttpResponse[Blob] = { + )(output: O)(implicit defaultContentType: ContentType): HttpResponse[Blob] = { val outputMetadata = outputMetadataEncoder.encode(output).headers.get(CaseInsensitive("content-type")) match { case Some(value) => value - case None => Seq(serviceContentType) + case None => Seq(defaultContentType.value) } codecDecider .httpMessageEncoder(outputMetadata) @@ -144,13 +144,13 @@ class SmithyPlayEndpoint[Alg[_[_, _, _, _, _]], F[_] <: ContextRoute[_], Op[_, _ .withHeaders(error.status.headers.toList: _*) .as(error.contentType) - private def handleSuccess(output: HttpResponse[Blob]): Result = { + private def handleSuccess(output: HttpResponse[Blob])(implicit defaultContentType: ContentType): Result = { val status = Results.Status(output.statusCode) val contentTypeKey = CaseInsensitive("content-type") val outputHeadersWithoutContentType = output.headers.-(contentTypeKey).toList.map(h => (h._1.toString, h._2.head)) val contentType = - output.headers.getOrElse(contentTypeKey, Seq(serviceContentType)) + output.headers.getOrElse(contentTypeKey, Seq(defaultContentType.value)) if (!output.body.isEmpty) { status(output.body.toArray) From 3ca008bcc678cd5ac6b3dbd9e7d1708d7e12b930 Mon Sep 17 00:00:00 2001 From: Lintterer Date: Mon, 29 Apr 2024 15:04:26 +0200 Subject: [PATCH 4/6] chore: remove unused function --- .../de/innfactory/smithy4play/CodecDecider.scala | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/smithy4play/src/main/scala/de/innfactory/smithy4play/CodecDecider.scala b/smithy4play/src/main/scala/de/innfactory/smithy4play/CodecDecider.scala index 9b2415ee..d26e2a0b 100644 --- a/smithy4play/src/main/scala/de/innfactory/smithy4play/CodecDecider.scala +++ b/smithy4play/src/main/scala/de/innfactory/smithy4play/CodecDecider.scala @@ -5,12 +5,12 @@ import play.api.http.MimeTypes import smithy4s.capability.instances.either._ import smithy4s.codecs.Writer.CachedCompiler import smithy4s.codecs._ -import smithy4s.http.{ HttpResponse, HttpRestSchema, Metadata, MetadataError } +import smithy4s.http.{HttpResponse, HttpRestSchema, Metadata, MetadataError} import smithy4s.json.Json import smithy4s.kinds.PolyFunction -import smithy4s.schema.{ CachedSchemaCompiler, Schema } +import smithy4s.schema.CachedSchemaCompiler import smithy4s.xml.Xml -import smithy4s.{ codecs, Blob, PartialData } +import smithy4s.{Blob, codecs} case class CodecDecider(readerConfig: ReaderConfig) { @@ -87,13 +87,6 @@ case class CodecDecider(readerConfig: ReaderConfig) { _ => Right(()) )(eitherZipper) - def test(): Unit = { - val x = metadataEncoder.mapK( - httpRequestMetadataPipe - ) - - } - def httpMessageEncoder( contentType: Seq[String] ): CachedCompiler[HttpResponse[Blob]] = From 4a901bcf12f18909adc453c07fcc350bb0b94a31 Mon Sep 17 00:00:00 2001 From: Lintterer Date: Mon, 29 Apr 2024 15:05:55 +0200 Subject: [PATCH 5/6] chore: rename tests and variables --- .../smithy4play/SmithyPlayEndpoint.scala | 4 ++-- smithy4playTest/test/TestControllerTest.scala | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/smithy4play/src/main/scala/de/innfactory/smithy4play/SmithyPlayEndpoint.scala b/smithy4play/src/main/scala/de/innfactory/smithy4play/SmithyPlayEndpoint.scala index 962d01b8..ff24b1f4 100644 --- a/smithy4play/src/main/scala/de/innfactory/smithy4play/SmithyPlayEndpoint.scala +++ b/smithy4play/src/main/scala/de/innfactory/smithy4play/SmithyPlayEndpoint.scala @@ -65,12 +65,12 @@ class SmithyPlayEndpoint[Alg[_[_, _, _, _, _]], F[_] <: ContextRoute[_], Op[_, _ private def mapToEndpointResult( statusCode: Int )(output: O)(implicit defaultContentType: ContentType): HttpResponse[Blob] = { - val outputMetadata = outputMetadataEncoder.encode(output).headers.get(CaseInsensitive("content-type")) match { + val outputContentType = outputMetadataEncoder.encode(output).headers.get(CaseInsensitive("content-type")) match { case Some(value) => value case None => Seq(defaultContentType.value) } codecDecider - .httpMessageEncoder(outputMetadata) + .httpMessageEncoder(outputContentType) .fromSchema(outputSchema) .write( HttpResponse( diff --git a/smithy4playTest/test/TestControllerTest.scala b/smithy4playTest/test/TestControllerTest.scala index 916f1849..f03ea09c 100644 --- a/smithy4playTest/test/TestControllerTest.scala +++ b/smithy4playTest/test/TestControllerTest.scala @@ -3,17 +3,24 @@ import de.innfactory.smithy4play.client.GenericAPIClient.EnhancedGenericAPIClien import de.innfactory.smithy4play.client.SmithyPlayTestUtils._ import de.innfactory.smithy4play.compliancetests.ComplianceClient import models.NodeImplicits.NodeEnhancer -import models.{TestBase, TestJson} +import models.{ TestBase, TestJson } import org.scalatest.time.SpanSugar.convertIntToGrainOfTime import play.api.Application import play.api.inject.guice.GuiceApplicationBuilder -import play.api.libs.json.{Json, OWrites} +import play.api.libs.json.{ Json, OWrites } import play.api.mvc.Result import play.api.test.FakeRequest import play.api.test.Helpers._ -import smithy4s.{Blob, Document} +import smithy4s.{ Blob, Document } import smithy4s.http.CaseInsensitive -import testDefinitions.test.{JsonInput, SimpleTestResponse, TestControllerServiceGen, TestRequestBody, TestResponseBody, TestWithOutputResponse} +import testDefinitions.test.{ + JsonInput, + SimpleTestResponse, + TestControllerServiceGen, + TestRequestBody, + TestResponseBody, + TestWithOutputResponse +} import java.io.File import java.nio.file.Files @@ -161,7 +168,7 @@ class TestControllerTest extends TestBase { pngAsBytes mustBe result.body.body } - "route 123 to Blob Endpoint" in { + "route with json body to Blob Endpoint" in { val testString = "StringToBeParsedCorrectly" val result = genericClient.testWithJsonInputAndBlobOutput(JsonInput(testString)).awaitRight(global, 5.hours) From 29c399462fa1f107cd338c3e56e6c6ee38f939c2 Mon Sep 17 00:00:00 2001 From: Lintterer Date: Mon, 29 Apr 2024 15:06:22 +0200 Subject: [PATCH 6/6] chore: scalafmt --- .../main/scala/de/innfactory/smithy4play/CodecDecider.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/smithy4play/src/main/scala/de/innfactory/smithy4play/CodecDecider.scala b/smithy4play/src/main/scala/de/innfactory/smithy4play/CodecDecider.scala index d26e2a0b..3c769e65 100644 --- a/smithy4play/src/main/scala/de/innfactory/smithy4play/CodecDecider.scala +++ b/smithy4play/src/main/scala/de/innfactory/smithy4play/CodecDecider.scala @@ -5,12 +5,12 @@ import play.api.http.MimeTypes import smithy4s.capability.instances.either._ import smithy4s.codecs.Writer.CachedCompiler import smithy4s.codecs._ -import smithy4s.http.{HttpResponse, HttpRestSchema, Metadata, MetadataError} +import smithy4s.http.{ HttpResponse, HttpRestSchema, Metadata, MetadataError } import smithy4s.json.Json import smithy4s.kinds.PolyFunction import smithy4s.schema.CachedSchemaCompiler import smithy4s.xml.Xml -import smithy4s.{Blob, codecs} +import smithy4s.{ codecs, Blob } case class CodecDecider(readerConfig: ReaderConfig) {