Skip to content

Commit a04f232

Browse files
authored
Allow parameters in content type header (#47)
* allow parameters for content type * only allow empty or UTF-8 charset
1 parent 475f46d commit a04f232

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

router/src/main/kotlin/io/moia/router/DeserializationHandler.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@ class DeserializationHandlerChain(private val handlers: List<DeserializationHand
4444

4545
class JsonDeserializationHandler(private val objectMapper: ObjectMapper) : DeserializationHandler {
4646

47-
private val json = MediaType.parse("application/json")
48-
private val jsonStructuredSuffixWildcard = MediaType.parse("application/*+json")
47+
private val json = MediaType.parse("application/json; charset=UTF-8")
48+
private val jsonStructuredSuffixWildcard = MediaType.parse("application/*+json; charset=UTF-8")
4949

5050
override fun supports(input: APIGatewayProxyRequestEvent) =
5151
if (input.contentType() == null)
5252
false
5353
else {
54-
MediaType.parse(input.contentType()!!).let { json.isCompatibleWith(it) || jsonStructuredSuffixWildcard.isCompatibleWith(it) }
54+
MediaType.parse(input.contentType()!!)
55+
.let { json.isCompatibleWith(it) || jsonStructuredSuffixWildcard.isCompatibleWith(it) }
5556
}
5657

5758
override fun deserialize(input: APIGatewayProxyRequestEvent, target: KType?): Any? {

router/src/main/kotlin/io/moia/router/MediaTypeExtensions.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ fun MediaType.isCompatibleWith(other: MediaType): Boolean =
2020
if (this.`is`(other))
2121
true
2222
else {
23-
type() == other.type() &&
24-
(subtype().contains("+") && other.subtype().contains("+")) &&
25-
this.subtype().substringBeforeLast("+") == "*" &&
26-
this.subtype().substringAfterLast("+") == other.subtype().substringAfterLast("+")
23+
type() == other.type() && (subtype().contains("+") && other.subtype().contains("+")) && this.subtype()
24+
.substringBeforeLast("+") == "*" && this.subtype().substringAfterLast("+") == other.subtype()
25+
.substringAfterLast("+") && (other.parameters().isEmpty || this.parameters() == other.parameters())
2726
}

router/src/test/kotlin/io/moia/router/JsonDeserializationHandlerTest.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,36 @@ class JsonDeserializationHandlerTest {
5757
)
5858
)
5959
}
60+
61+
@Test
62+
fun `should support json with UTF-8 charset parameter`() {
63+
assertTrue(
64+
deserializationHandler.supports(
65+
APIGatewayProxyRequestEvent()
66+
.withHeader("content-type", "application/json; charset=UTF-8")
67+
)
68+
)
69+
assertTrue(
70+
deserializationHandler.supports(
71+
APIGatewayProxyRequestEvent()
72+
.withHeader("content-type", "application/vnd.moia.v1+json; charset=UTF-8")
73+
)
74+
)
75+
}
76+
77+
@Test
78+
fun `should not support json with other charset parameter`() {
79+
assertFalse(
80+
deserializationHandler.supports(
81+
APIGatewayProxyRequestEvent()
82+
.withHeader("content-type", "application/json; charset=UTF-16")
83+
)
84+
)
85+
assertFalse(
86+
deserializationHandler.supports(
87+
APIGatewayProxyRequestEvent()
88+
.withHeader("content-type", "application/vnd.moia.v1+json; charset=UTF-16")
89+
)
90+
)
91+
}
6092
}

0 commit comments

Comments
 (0)