Skip to content

Commit 0e8a8e9

Browse files
committed
[Runtime] Improve parameter handling of MIME types in content types
1 parent 26e8ae3 commit 0e8a8e9

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

Sources/OpenAPIRuntime/Conversion/Converter+Server.swift

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,34 @@ extension Converter {
5656
// Drop everything after the optional semicolon (q, extensions, ...)
5757
value.split(separator: ";")[0].trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
5858
}
59-
6059
if acceptValues.isEmpty { return }
61-
if acceptValues.contains("*/*") { return }
62-
if acceptValues.contains("\(substring.split(separator: "/")[0].lowercased())/*") { return }
63-
if acceptValues.contains(where: { $0.localizedCaseInsensitiveContains(substring) }) { return }
60+
guard let parsedSubstring = OpenAPIMIMEType(substring) else {
61+
throw RuntimeError.invalidAcceptSubstring(substring)
62+
}
63+
guard case .concrete(let substringType, let substringSubtype) = parsedSubstring.kind else {
64+
// If the substring content type has a wildcard, just let it through.
65+
// It's not well defined how such a case should behave, so be permissive.
66+
return
67+
}
68+
69+
// Look for the first match.
70+
for acceptValue in acceptValues {
71+
// Fast path.
72+
if acceptValue == substring { return }
73+
guard let parsedAcceptValue = OpenAPIMIMEType(acceptValue) else {
74+
throw RuntimeError.invalidExpectedContentType(acceptValue)
75+
}
76+
switch parsedAcceptValue.kind {
77+
case .any: return
78+
case .anySubtype(type: let type): if substringType.lowercased() == type.lowercased() { return }
79+
case .concrete(type: let type, subtype: let subtype):
80+
if type.lowercased() == substringType.lowercased()
81+
&& subtype.lowercased() == substringSubtype.lowercased()
82+
{
83+
return
84+
}
85+
}
86+
}
6487
throw RuntimeError.unexpectedAcceptHeader(acceptHeader)
6588
}
6689

Sources/OpenAPIRuntime/Errors/RuntimeError.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ internal enum RuntimeError: Error, CustomStringConvertible, LocalizedError, Pret
2121
case invalidServerURL(String)
2222
case invalidServerVariableValue(name: String, value: String, allowedValues: [String])
2323
case invalidExpectedContentType(String)
24+
case invalidAcceptSubstring(String)
2425
case invalidHeaderFieldName(String)
2526
case invalidBase64String(String)
2627

@@ -85,6 +86,7 @@ internal enum RuntimeError: Error, CustomStringConvertible, LocalizedError, Pret
8586
return
8687
"Invalid server variable named: '\(name)', which has the value: '\(value)', but the only allowed values are: \(allowedValues.map { "'\($0)'" }.joined(separator: ", "))"
8788
case .invalidExpectedContentType(let string): return "Invalid expected content type: '\(string)'"
89+
case .invalidAcceptSubstring(let string): return "Invalid Accept header content type: '\(string)'"
8890
case .invalidHeaderFieldName(let name): return "Invalid header field name: '\(name)'"
8991
case .invalidBase64String(let string):
9092
return "Invalid base64-encoded string (first 128 bytes): '\(string.prefix(128))'"

Tests/OpenAPIRuntimeTests/Conversion/Test_Converter+Server.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ final class Test_ServerConverterExtensions: Test_Runtime {
3939
.accept: "text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8"
4040
]
4141
let multiple: HTTPFields = [.accept: "text/plain, application/json"]
42+
let params: HTTPFields = [.accept: "application/json; foo=bar"]
4243
let cases: [(HTTPFields, String, Bool)] = [
4344
// No Accept header, any string validates successfully
4445
(emptyHeaders, "foobar", true),
4546

46-
// Accept: */*, any string validates successfully
47-
(wildcard, "foobar", true),
47+
// Accept: */*, any MIME type validates successfully
48+
(wildcard, "foobaz/bar", true),
4849

4950
// Accept: text/*, so text/plain succeeds, application/json fails
5051
(partialWildcard, "text/plain", true), (partialWildcard, "application/json", false),
@@ -58,6 +59,10 @@ final class Test_ServerConverterExtensions: Test_Runtime {
5859

5960
// Multiple values
6061
(multiple, "text/plain", true), (multiple, "application/json", true), (multiple, "application/xml", false),
62+
63+
// Params
64+
(params, "application/json; foo=bar", true), (params, "application/json; charset=utf-8; foo=bar", true),
65+
(params, "application/json", true), (params, "text/plain", false),
6166
]
6267
for (headers, contentType, success) in cases {
6368
if success {

0 commit comments

Comments
 (0)