Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
czechboy0 committed Dec 17, 2024
1 parent 4cb7b14 commit 837e6ed
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -546,16 +546,15 @@ struct TypeAssigner {
let params = contentType.lowercasedParameterPairs
guard !params.isEmpty else { return prefix }
let safedParams =
params.map { pair in
pair
.split(separator: "=")
.map { component in
let safedComponent = context.asSwiftSafeName(String(component), .noncapitalized)
return capitalizeNonFirstWords ? safedComponent.uppercasingFirstLetter : safedComponent
}
.joined(separator: componentSeparator)
}
.joined(separator: componentSeparator)
params.map { pair in
pair.split(separator: "=")
.map { component in
let safedComponent = context.asSwiftSafeName(String(component), .noncapitalized)
return capitalizeNonFirstWords ? safedComponent.uppercasingFirstLetter : safedComponent
}
.joined(separator: componentSeparator)
}
.joined(separator: componentSeparator)
return prefix + componentSeparator + safedParams
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ final class Test_SwiftSafeNames: Test_Core {
("Retry-After", "Retry_hyphen_After", "RetryAfter", "retryAfter"),

// All uppercase
("HELLO_WORLD", "HELLO_WORLD", "HelloWorld", "helloWorld"),
("HELLO", "HELLO", "Hello", "hello"),
("HELLO_WORLD", "HELLO_WORLD", "HelloWorld", "helloWorld"), ("HELLO", "HELLO", "Hello", "hello"),

// Acronyms
("HTTPProxy", "HTTPProxy", "HTTPProxy", "httpProxy"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,7 @@ final class Test_OperationDescription: Test_Core {
endpoint: endpoint,
pathParameters: pathItem.parameters,
components: .init(),
context: .init(
asSwiftSafeName: { string, _ in string },
namingStrategy: .defensive
)
context: .init(asSwiftSafeName: { string, _ in string }, namingStrategy: .defensive)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,14 @@ class Test_TypeAssigner: Test_Core {
// Short names.
("application/json", "json", "json"),
("application/x-www-form-urlencoded", "urlEncodedForm", "urlEncodedForm"),
("multipart/form-data", "multipartForm", "multipartForm"),
("text/plain", "plainText", "plainText"),
("*/*", "any", "any"),
("application/xml", "xml", "xml"),
("application/octet-stream", "binary", "binary"),
("text/html", "html", "html"),
("application/yaml", "yaml", "yaml"),
("text/csv", "csv", "csv"),
("image/png", "png", "png"),
("application/pdf", "pdf", "pdf"),
("image/jpeg", "jpeg", "jpeg"),
("multipart/form-data", "multipartForm", "multipartForm"), ("text/plain", "plainText", "plainText"),
("*/*", "any", "any"), ("application/xml", "xml", "xml"), ("application/octet-stream", "binary", "binary"),
("text/html", "html", "html"), ("application/yaml", "yaml", "yaml"), ("text/csv", "csv", "csv"),
("image/png", "png", "png"), ("application/pdf", "pdf", "pdf"), ("image/jpeg", "jpeg", "jpeg"),

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

// Names with a parameter.
("application/foo", "application_foo", "applicationFoo"),
Expand All @@ -136,8 +128,16 @@ class Test_TypeAssigner: Test_Core {
]
for (string, defensiveName, idiomaticName) in cases {
let contentType = try XCTUnwrap(ContentType(string: string))
XCTAssertEqual(defensiveNameMaker(contentType), defensiveName, "Case \(string) failed for defensive strategy")
XCTAssertEqual(idiomaticNameMaker(contentType), idiomaticName, "Case \(string) failed for idiomatic strategy")
XCTAssertEqual(
defensiveNameMaker(contentType),
defensiveName,
"Case \(string) failed for defensive strategy"
)
XCTAssertEqual(
idiomaticNameMaker(contentType),
idiomaticName,
"Case \(string) failed for idiomatic strategy"
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2309,53 +2309,52 @@ final class SnippetBasedReferenceTests: XCTestCase {

func testSynthesizedOperationId_defensive() throws {
let paths = """
/pets/{petId}/notifications:
parameters:
- name: petId
in: path
required: true
schema:
type: string
get:
responses:
'204':
description: A success response.
"""
/pets/{petId}/notifications:
parameters:
- name: petId
in: path
required: true
schema:
type: string
get:
responses:
'204':
description: A success response.
"""
try self.assertPathsTranslation(
paths,
"""
public protocol APIProtocol: Sendable {
func get_sol_pets_sol__lcub_petId_rcub__sol_notifications(_ input: Operations.get_sol_pets_sol__lcub_petId_rcub__sol_notifications.Input) async throws -> Operations.get_sol_pets_sol__lcub_petId_rcub__sol_notifications.Output
}
"""
"""
public protocol APIProtocol: Sendable {
func get_sol_pets_sol__lcub_petId_rcub__sol_notifications(_ input: Operations.get_sol_pets_sol__lcub_petId_rcub__sol_notifications.Input) async throws -> Operations.get_sol_pets_sol__lcub_petId_rcub__sol_notifications.Output
}
"""
)
}

func testSynthesizedOperationId_idiomatic() throws {
let paths = """
/pets/{petId}/notifications:
parameters:
- name: petId
in: path
required: true
schema:
type: string
get:
responses:
'204':
description: A success response.
"""
/pets/{petId}/notifications:
parameters:
- name: petId
in: path
required: true
schema:
type: string
get:
responses:
'204':
description: A success response.
"""
try self.assertPathsTranslation(
paths,
namingStrategy: .idiomatic,
"""
public protocol APIProtocol: Sendable {
func getPetsPetIdNotifications(_ input: Operations.GetPetsPetIdNotifications.Input) async throws -> Operations.GetPetsPetIdNotifications.Output
}
"""
"""
public protocol APIProtocol: Sendable {
func getPetsPetIdNotifications(_ input: Operations.GetPetsPetIdNotifications.Input) async throws -> Operations.GetPetsPetIdNotifications.Output
}
"""
)
}

func testServerRegisterHandlers_oneOperation() throws {
try self.assertServerRegisterHandlers(
"""
Expand Down Expand Up @@ -6140,10 +6139,7 @@ extension SnippetBasedReferenceTests {
file: StaticString = #filePath,
line: UInt = #line
) throws {
let translator = try makeTypesTranslator(
namingStrategy: namingStrategy,
componentsYAML: componentsYAML
)
let translator = try makeTypesTranslator(namingStrategy: namingStrategy, componentsYAML: componentsYAML)
let paths = try YAMLDecoder().decode(OpenAPI.PathItem.Map.self, from: pathsYAML)
let translation = try translator.translateAPIProtocol(paths)
try XCTAssertSwiftEquivalent(translation, expectedSwift, file: file, line: line)
Expand Down

0 comments on commit 837e6ed

Please sign in to comment.