Skip to content

Commit bd28e53

Browse files
committed
Added + as a word separator
1 parent 96f90be commit bd28e53

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

Sources/_OpenAPIGeneratorCore/Translator/CommonTranslations/SwiftSafeNames.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ extension String {
9898
}
9999

100100
// 1. Leave leading underscores as-are
101-
// 2. In the middle: word separators: ["_", "-", "/", <space>] -> remove and capitalize next word
101+
// 2. In the middle: word separators: ["_", "-", "/", "+", <space>] -> remove and capitalize
102+
// next word
102103
// 3. In the middle: period: ["."] -> replace with "_"
103104
// 4. In the middle: drop ["{", "}"] -> replace with ""
104105

@@ -188,7 +189,7 @@ extension String {
188189
buffer.append(char)
189190
}
190191
state = .accumulatingFirstWord(context)
191-
} else if ["_", "-", " ", "/"].contains(char) {
192+
} else if ["_", "-", " ", "/", "+"].contains(char) {
192193
// In the middle of an identifier, these are considered
193194
// word separators, so we remove the character and end the current word.
194195
state = .waitingForWordStarter
@@ -218,14 +219,14 @@ extension String {
218219
buffer.append("_")
219220
state = .accumulatingWord
220221
} else if ["{", "}"].contains(char) {
221-
// In the middle of an identifier, curly braces are dropped.
222+
// In the middle of an identifier, these are dropped.
222223
state = .accumulatingWord
223224
} else {
224225
// Illegal character, fall back to the defensive strategy.
225226
state = .fallback
226227
}
227228
case .waitingForWordStarter:
228-
if ["_", "-", ".", "/", "{", "}"].contains(char) {
229+
if ["_", "-", ".", "/", "+", "{", "}"].contains(char) {
229230
// Between words, just drop allowed special characters, since
230231
// we're already between words anyway.
231232
state = .waitingForWordStarter
@@ -250,7 +251,7 @@ extension String {
250251
}
251252

252253
/// A list of word separator characters for the idiomatic naming strategy.
253-
private static let wordSeparators: Set<Character> = ["_", "-", " ", "/"]
254+
private static let wordSeparators: Set<Character> = ["_", "-", " ", "/", "+"]
254255

255256
/// A list of Swift keywords.
256257
///

Sources/_OpenAPIGeneratorCore/Translator/TypeAssignment/TypeAssigner.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ struct TypeAssigner {
541541
componentSeparator = ""
542542
capitalizeNonFirstWords = true
543543
}
544-
let prettifiedSubtype = capitalizeNonFirstWords ? safedSubtype.capitalized : safedSubtype
544+
let prettifiedSubtype = capitalizeNonFirstWords ? safedSubtype.uppercasingFirstLetter : safedSubtype
545545
let prefix = "\(safedType)\(componentSeparator)\(prettifiedSubtype)"
546546
let params = contentType.lowercasedParameterPairs
547547
guard !params.isEmpty else { return prefix }
@@ -551,7 +551,7 @@ struct TypeAssigner {
551551
.split(separator: "=")
552552
.map { component in
553553
let safedComponent = context.asSwiftSafeName(String(component), .noncapitalized)
554-
return capitalizeNonFirstWords ? safedComponent.capitalized : safedComponent
554+
return capitalizeNonFirstWords ? safedComponent.uppercasingFirstLetter : safedComponent
555555
}
556556
.joined(separator: componentSeparator)
557557
}

Tests/OpenAPIGeneratorCoreTests/Translator/TypeAssignment/Test_TypeAssigner.swift

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,27 +105,39 @@ class Test_TypeAssigner: Test_Core {
105105
}
106106

107107
func testContentSwiftName() throws {
108-
let nameMaker = makeTranslator().typeAssigner.contentSwiftName
109-
let cases: [(String, String)] = [
108+
let defensiveNameMaker = makeTranslator().typeAssigner.contentSwiftName
109+
let idiomaticNameMaker = makeTranslator(namingStrategy: .idiomatic).typeAssigner.contentSwiftName
110+
let cases: [(input: String, defensive: String, idiomatic: String)] = [
110111

111112
// Short names.
112-
("application/json", "json"), ("application/x-www-form-urlencoded", "urlEncodedForm"),
113-
("multipart/form-data", "multipartForm"), ("text/plain", "plainText"), ("*/*", "any"),
114-
("application/xml", "xml"), ("application/octet-stream", "binary"), ("text/html", "html"),
115-
("application/yaml", "yaml"), ("text/csv", "csv"), ("image/png", "png"), ("application/pdf", "pdf"),
116-
("image/jpeg", "jpeg"),
113+
("application/json", "json", "json"),
114+
("application/x-www-form-urlencoded", "urlEncodedForm", "urlEncodedForm"),
115+
("multipart/form-data", "multipartForm", "multipartForm"),
116+
("text/plain", "plainText", "plainText"),
117+
("*/*", "any", "any"),
118+
("application/xml", "xml", "xml"),
119+
("application/octet-stream", "binary", "binary"),
120+
("text/html", "html", "html"),
121+
("application/yaml", "yaml", "yaml"),
122+
("text/csv", "csv", "csv"),
123+
("image/png", "png", "png"),
124+
("application/pdf", "pdf", "pdf"),
125+
("image/jpeg", "jpeg", "jpeg"),
117126

118127
// Generic names.
119-
("application/myformat+json", "application_myformat_plus_json"), ("foo/bar", "foo_bar"),
128+
("application/myformat+json", "application_myformat_plus_json", "applicationMyformatJson"),
129+
("foo/bar", "foo_bar", "fooBar"),
130+
("text/event-stream", "text_event_hyphen_stream", "textEventStream"),
120131

121132
// Names with a parameter.
122-
("application/foo", "application_foo"),
123-
("application/foo; bar=baz; boo=foo", "application_foo_bar_baz_boo_foo"),
124-
("application/foo; bar = baz", "application_foo_bar_baz"),
133+
("application/foo", "application_foo", "applicationFoo"),
134+
("application/foo; bar=baz; boo=foo", "application_foo_bar_baz_boo_foo", "applicationFooBarBazBooFoo"),
135+
("application/foo; bar = baz", "application_foo_bar_baz", "applicationFooBarBaz"),
125136
]
126-
for (string, name) in cases {
137+
for (string, defensiveName, idiomaticName) in cases {
127138
let contentType = try XCTUnwrap(ContentType(string: string))
128-
XCTAssertEqual(nameMaker(contentType), name, "Case \(string) failed")
139+
XCTAssertEqual(defensiveNameMaker(contentType), defensiveName, "Case \(string) failed for defensive strategy")
140+
XCTAssertEqual(idiomaticNameMaker(contentType), idiomaticName, "Case \(string) failed for idiomatic strategy")
129141
}
130142
}
131143
}

0 commit comments

Comments
 (0)