Skip to content

Commit 246318d

Browse files
authored
Merge pull request #298 from innFactory/feat/ignoreCharset
feat: ignoring charset in content-type for encoding and decoding
2 parents 72d5efd + e058806 commit 246318d

File tree

4 files changed

+68
-14
lines changed

4 files changed

+68
-14
lines changed

smithy4play/src/main/scala/de/innfactory/smithy4play/CodecDecider.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ case class CodecDecider(readerConfig: ReaderConfig) {
2828
def encoder(
2929
contentType: Seq[String]
3030
): CachedSchemaCompiler[codecs.BlobEncoder] =
31-
contentType match {
31+
contentType.map(_.split(";").head) match {
3232
case Seq(MimeTypes.JSON) => jsonEncoder
3333
case Seq(MimeTypes.XML) => Xml.encoders
3434
case _ =>
@@ -129,7 +129,7 @@ case class CodecDecider(readerConfig: ReaderConfig) {
129129
def decoder(
130130
contentType: Seq[String]
131131
): CachedSchemaCompiler[BlobDecoder] =
132-
contentType match {
132+
contentType.map(_.split(";").head) match {
133133
case Seq(MimeTypes.JSON) => jsonDecoder
134134
case Seq(MimeTypes.XML) => Xml.decoders
135135
case _ =>

smithy4playTest/app/controller/XmlController.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@ class XmlController @Inject() (implicit
1818

1919
override def xmlTestWithInputAndOutput(
2020
xmlTest: String,
21-
body: XmlTestInputBody
21+
body: XmlTestInputBody,
22+
contentType: Option[String]
2223
): ContextRoute[XmlTestWithInputAndOutputOutput] =
2324
Kleisli { _ =>
2425
EitherT(
2526
Future(
2627
XmlTestWithInputAndOutputOutput(
27-
XmlTestOutput(body.serverzeit, body.requiredTest + xmlTest, body.requiredInt.map(i => i * i))
28+
XmlTestOutput(body.serverzeit, body.requiredTest + xmlTest, body.requiredInt.map(i => i * i)),
29+
contentType
2830
)
2931
.asRight[ContextRouteError]
3032
)
3133
)
3234
}
35+
3336
}

smithy4playTest/test/XmlControllerTest.scala

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import models.NodeImplicits.NodeEnhancer
44
import models.TestBase
55
import play.api.Application
66
import play.api.inject.guice.GuiceApplicationBuilder
7-
import play.api.libs.json.{Json, OFormat}
7+
import play.api.libs.json.{ Json, OFormat }
88
import play.api.test.FakeRequest
99
import play.api.test.Helpers._
1010
import smithy4s.http.CaseInsensitive
11-
import testDefinitions.test.{XmlControllerDefGen, XmlTestInputBody, XmlTestOutput}
11+
import testDefinitions.test.{ XmlControllerDefGen, XmlTestInputBody, XmlTestOutput }
1212

1313
import scala.concurrent.ExecutionContext.Implicits.global
1414

@@ -33,6 +33,53 @@ class XmlControllerTest extends TestBase {
3333
res.body.body.requiredTestStringConcat mustBe "ThisGetsConcat"
3434
}
3535

36+
"route to xml with charset in header endpoint with smithy client" in {
37+
val res = genericClient
38+
.xmlTestWithInputAndOutput(
39+
"Concat",
40+
XmlTestInputBody("05.02.2024", "ThisGets", Some(10)),
41+
Some("application/xml; charset=utf-8")
42+
)
43+
.awaitRight
44+
45+
res.body.body.requiredIntSquared mustBe Some(100)
46+
res.body.body.requiredTestStringConcat mustBe "ThisGetsConcat"
47+
res.headers.get(CaseInsensitive("content-type")) mustBe Some(List("application/xml; charset=utf-8"))
48+
}
49+
50+
"route to xml with charset in header with external client" in {
51+
val concatVal1 = "ConcatThis"
52+
val concatVal2 = "Test2"
53+
val squareTest = 3
54+
val xml =
55+
<XmlTestInputBody serverzeit="05.02.2024">
56+
<requiredTest>{concatVal1}</requiredTest>
57+
<requiredInt>{squareTest}</requiredInt>
58+
</XmlTestInputBody>
59+
val request = route(
60+
app,
61+
FakeRequest("POST", s"/xml/$concatVal2")
62+
.withHeaders(("content-type", "application/xml; charset=utf-8"))
63+
.withXmlBody(
64+
xml
65+
)
66+
).get
67+
status(request) mustBe 200
68+
69+
val result = scala.xml.XML.loadString(contentAsString(request))
70+
val resContentType = contentType(request)
71+
val resCharset = charset(request)
72+
73+
74+
result.normalize mustBe <XmlTestOutput serverzeit="05.02.2024">
75+
<requiredTestStringConcat>
76+
{concatVal1 + concatVal2}</requiredTestStringConcat>
77+
<requiredIntSquared>
78+
{squareTest * squareTest}</requiredIntSquared>
79+
</XmlTestOutput>.normalize
80+
resContentType.map(_ + "; charset=" + resCharset.getOrElse("")) mustBe Some("application/xml; charset=utf-8")
81+
}
82+
3683
"route to xml test endpoint with external client" in {
3784
val concatVal1 = "ConcatThis"
3885
val concatVal2 = "Test2"
@@ -115,13 +162,13 @@ class XmlControllerTest extends TestBase {
115162
}
116163

117164
"route to test endpoint with external client and json protocol" in {
118-
implicit val formatI: OFormat[XmlTestInputBody] = Json.format[XmlTestInputBody]
119-
implicit val formatO: OFormat[XmlTestOutput] = Json.format[XmlTestOutput]
120-
val concatVal2 = "Test2"
121-
val concatVal1 = "ConcatThis"
122-
val squareTest = Some(15)
123-
val date = "05.02.2024"
124-
val request = route(
165+
implicit val formatI: OFormat[XmlTestInputBody] = Json.format[XmlTestInputBody]
166+
implicit val formatO: OFormat[XmlTestOutput] = Json.format[XmlTestOutput]
167+
val concatVal2 = "Test2"
168+
val concatVal1 = "ConcatThis"
169+
val squareTest = Some(15)
170+
val date = "05.02.2024"
171+
val request = route(
125172
app,
126173
FakeRequest("POST", s"/xml/$concatVal2")
127174
.withHeaders(("content-type", "application/json"))
@@ -130,7 +177,7 @@ class XmlControllerTest extends TestBase {
130177
)
131178
).get
132179
status(request) mustBe 200
133-
val result = contentAsJson(request).as[XmlTestOutput]
180+
val result = contentAsJson(request).as[XmlTestOutput]
134181
result.requiredTestStringConcat mustBe concatVal1 + concatVal2
135182
result.requiredIntSquared mustBe squareTest.map(s => s * s)
136183
result.serverzeit mustBe date

smithy4playTest/testSpecs/XmlController.smithy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ service XmlControllerDef {
1515
operation XmlTestWithInputAndOutput {
1616
input: XmlTestInput
1717
output := {
18+
@httpHeader("content-type")
19+
contentType: String
1820
@required
1921
@httpPayload
2022
body: XmlTestOutput
2123
}
2224
}
2325

2426
structure XmlTestInput {
27+
@httpHeader("content-type")
28+
contentType: String
2529
@httpLabel
2630
@required
2731
xmlTest: String

0 commit comments

Comments
 (0)