Skip to content

Commit 71849ae

Browse files
committed
WIP on rewriting, doesn't build yet
1 parent ba9d50c commit 71849ae

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

Sources/OpenAPIRuntime/Conversion/Converter+Server.swift

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,41 @@ extension Converter {
6060
guard let parsedSubstring = OpenAPIMIMEType(substring) else {
6161
throw RuntimeError.invalidAcceptSubstring(substring)
6262
}
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-
6963
// Look for the first match.
7064
for acceptValue in acceptValues {
7165
// Fast path.
7266
if acceptValue == substring { return }
7367
guard let parsedAcceptValue = OpenAPIMIMEType(acceptValue) else {
7468
throw RuntimeError.invalidExpectedContentType(acceptValue)
7569
}
76-
switch parsedAcceptValue.kind {
77-
case .any: return
78-
case .anySubtype(type: let type): if substringType.lowercased() == type.lowercased() { return }
70+
switch (parsedAcceptValue.kind, parsedSubstring.kind) {
71+
case (.any, _):
72+
// Accept: */* always matches
73+
return
74+
case (.anySubtype(type: let acceptType), let substring):
75+
switch substring {
76+
case .any:
77+
// */* as a concrete content type is NOT a match for an Accept header of foo/*
78+
break
79+
case .anySubtype(type: let substringType):
80+
if substringType.lowercased() == acceptType.lowercased() {
81+
return
82+
}
83+
case .concrete(type: let substringType, subtype: let substringSubtype):
84+
if
85+
substringType.lowercased() == acceptType.lowercased() &&
86+
87+
{
88+
return
89+
}
90+
91+
if accept.lowercased() == substringType.lowercased()
92+
&& subtype.lowercased() == substringSubtype.lowercased()
93+
{
94+
return
95+
}
96+
}
97+
7998
case .concrete(type: let type, subtype: let subtype):
8099
if type.lowercased() == substringType.lowercased()
81100
&& subtype.lowercased() == substringSubtype.lowercased()

Tests/OpenAPIRuntimeTests/Conversion/Test_Converter+Server.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ final class Test_ServerConverterExtensions: Test_Runtime {
5252

5353
// Accept: text/plain, text/plain succeeds, application/json fails
5454
(short, "text/plain", true), (short, "application/json", false),
55+
(short, "application/*", false), (short, "*/*", false),
5556

5657
// A bunch of acceptable content types
5758
(long, "text/html", true), (long, "application/xhtml+xml", true), (long, "application/xml", true),

0 commit comments

Comments
 (0)