-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move ContentType.identifier logic into the translator (#141)
Move ContentType.identifier logic into the translator ### Motivation In preparation for changing the mapping of content type names onto enum case names, we need to have the `ContentType.identifier` logic on a translator, which has access to the feature flags, so that we can later conditionalize the logic. ### Modifications Takes the first step - moves the existing logic onto the translator. ### Result NFC ### Test Plan Added unit tests both for the existing logic, and also for the stubbed out logic behind the feature flag. Reviewed by: simonjbeaumont Builds: ✔︎ pull request validation (5.8) - Build finished. ✔︎ pull request validation (5.9) - Build finished. ✔︎ pull request validation (docc test) - Build finished. ✔︎ pull request validation (integration test) - Build finished. ✔︎ pull request validation (nightly) - Build finished. ✔︎ pull request validation (soundness) - Build finished. #141
- Loading branch information
Showing
8 changed files
with
111 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
Sources/_OpenAPIGeneratorCore/Translator/Content/ContentSwiftName.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftOpenAPIGenerator open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
import OpenAPIKit30 | ||
|
||
extension FileTranslator { | ||
|
||
/// Returns a Swift-safe identifier used as the name of the content | ||
/// enum case. | ||
/// | ||
/// - Parameter contentType: The content type for which to compute the name. | ||
func contentSwiftName(_ contentType: ContentType) -> String { | ||
if config.featureFlags.contains(.multipleContentTypes) { | ||
return "unsupported" | ||
} else { | ||
switch contentType { | ||
case .json: | ||
return "json" | ||
case .text: | ||
return "text" | ||
case .binary: | ||
return "binary" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
Tests/OpenAPIGeneratorCoreTests/Translator/Content/Test_ContentSwiftName.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftOpenAPIGenerator open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
import XCTest | ||
import OpenAPIKit30 | ||
@testable import _OpenAPIGeneratorCore | ||
|
||
final class Test_ContentSwiftName: Test_Core { | ||
|
||
func testExisting() throws { | ||
let nameMaker = makeTranslator(featureFlags: []).contentSwiftName | ||
let cases: [(String, String)] = [ | ||
("application/json", "json"), | ||
("application/x-www-form-urlencoded", "binary"), | ||
("multipart/form-data", "binary"), | ||
("text/plain", "text"), | ||
("*/*", "binary"), | ||
("application/xml", "binary"), | ||
("application/octet-stream", "binary"), | ||
("application/myformat+json", "json"), | ||
("foo/bar", "binary"), | ||
] | ||
try _testIdentifiers(cases: cases, nameMaker: nameMaker) | ||
} | ||
|
||
func testProposed() throws { | ||
let nameMaker = makeTranslator(featureFlags: [.multipleContentTypes]).contentSwiftName | ||
let cases: [(String, String)] = [ | ||
("application/json", "unsupported"), | ||
("application/x-www-form-urlencoded", "unsupported"), | ||
("multipart/form-data", "unsupported"), | ||
("text/plain", "unsupported"), | ||
("*/*", "unsupported"), | ||
("application/xml", "unsupported"), | ||
("application/octet-stream", "unsupported"), | ||
("application/myformat+json", "unsupported"), | ||
("foo/bar", "unsupported"), | ||
] | ||
try _testIdentifiers(cases: cases, nameMaker: nameMaker) | ||
} | ||
|
||
func _testIdentifiers(cases: [(String, String)], nameMaker: (ContentType) -> String) throws { | ||
for item in cases { | ||
let contentType = try XCTUnwrap(ContentType(item.0)) | ||
XCTAssertEqual(nameMaker(contentType), item.1, "Case \(item.0) failed") | ||
} | ||
} | ||
} |