Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the frozen keyword only to enums with the public or package modifiers #595

Merged
merged 8 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ struct TextBasedRenderer: RendererProtocol {

/// Renders the specified enum declaration.
func renderEnum(_ enumDesc: EnumDescription) {
if enumDesc.isFrozen {
if requiresFrozenAnnotation(enumDesc) {
writer.writeLine("@frozen ")
writer.nextLineAppendsToLastLine()
}
Expand Down Expand Up @@ -898,4 +898,13 @@ extension TextBasedRenderer {
renderer.renderExpression(expression)
return renderer.renderedContents()
}

/// Checks if the given enum description requires a @frozen annotation.
/// - Parameter enumDesc: The enum description to check.
/// - Returns: A boolean value indicating whether the enum description requires a @frozen annotation.
func requiresFrozenAnnotation(_ enumDesc: EnumDescription) -> Bool {
guard enumDesc.isFrozen else { return false }
guard let accessModifier = enumDesc.accessModifier else { return false }
return accessModifier == .public || accessModifier == .package
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,28 @@ final class Test_TextBasedRenderer: XCTestCase {
}
}

extension Test_TextBasedRenderer {
func testRequiresFrozenAnnotation() {
let renderer = TextBasedRenderer.default
let testCases: [(EnumDescription, Bool)] = [
(EnumDescription(isFrozen: true, accessModifier: .`public`, name: ""), true),
(EnumDescription(isFrozen: true, accessModifier: .`package`, name: ""), true),
(EnumDescription(isFrozen: true, accessModifier: .`internal`, name: ""), false),
(EnumDescription(isFrozen: true, accessModifier: .`fileprivate`, name: ""), false),
(EnumDescription(isFrozen: true, accessModifier: .`private`, name: ""), false),
(EnumDescription(isFrozen: false, accessModifier: .`public`, name: ""), false),
(EnumDescription(isFrozen: false, accessModifier: .`package`, name: ""), false),
(EnumDescription(isFrozen: false, accessModifier: .`internal`, name: ""), false),
(EnumDescription(isFrozen: false, accessModifier: .`fileprivate`, name: ""), false),
(EnumDescription(isFrozen: false, accessModifier: .`private`, name: ""), false),
]

for (enumDesc, expectedResult) in testCases {
XCTAssertEqual(renderer.requiresFrozenAnnotation(enumDesc), expectedResult)
}
}
}

extension Test_TextBasedRenderer {

func _test<Input>(
Expand Down
112 changes: 112 additions & 0 deletions Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,116 @@ final class SnippetBasedReferenceTests: XCTestCase {
)
}

func testComponentsSchemasFrozenEnum_accessModifier_public() throws {
try self.assertSchemasTranslation(
"""
schemas:
MyEnum:
type: string
enum:
- one
- two
""",
"""
public enum Schemas {
@frozen public enum MyEnum: String, Codable, Hashable, Sendable, CaseIterable {
case one = "one"
case two = "two"
}
}
""",
accessModifier: .public
)
}

func testComponentsSchemasFrozenEnum_accessModifier_package() throws {
try self.assertSchemasTranslation(
"""
schemas:
MyEnum:
type: string
enum:
- one
- two
""",
"""
package enum Schemas {
@frozen package enum MyEnum: String, Codable, Hashable, Sendable, CaseIterable {
case one = "one"
case two = "two"
}
}
""",
accessModifier: .package
)
}

func testComponentsSchemasFrozenEnum_accessModifier_internal() throws {
try self.assertSchemasTranslation(
"""
schemas:
MyEnum:
type: string
enum:
- one
- two
""",
"""
internal enum Schemas {
internal enum MyEnum: String, Codable, Hashable, Sendable, CaseIterable {
case one = "one"
case two = "two"
}
}
""",
accessModifier: .internal
)
}

func testComponentsSchemasFrozenEnum_accessModifier_fileprivate() throws {
try self.assertSchemasTranslation(
"""
schemas:
MyEnum:
type: string
enum:
- one
- two
""",
"""
fileprivate enum Schemas {
fileprivate enum MyEnum: String, Codable, Hashable, Sendable, CaseIterable {
case one = "one"
case two = "two"
}
}
""",
accessModifier: .fileprivate
)
}

func testComponentsSchemasFrozenEnum_accessModifier_private() throws {
try self.assertSchemasTranslation(
"""
schemas:
MyEnum:
type: string
enum:
- one
- two
""",
"""
private enum Schemas {
private enum MyEnum: String, Codable, Hashable, Sendable, CaseIterable {
case one = "one"
case two = "two"
}
}
""",
accessModifier: .private
)
}

func testComponentsSchemasString() throws {
try self.assertSchemasTranslation(
"""
Expand Down Expand Up @@ -5074,10 +5184,12 @@ extension SnippetBasedReferenceTests {
ignoredDiagnosticMessages: Set<String> = [],
_ componentsYAML: String,
_ expectedSwift: String,
accessModifier: AccessModifier = .public,
file: StaticString = #filePath,
line: UInt = #line
) throws {
let translator = try makeTypesTranslator(
accessModifier: accessModifier,
featureFlags: featureFlags,
ignoredDiagnosticMessages: ignoredDiagnosticMessages,
componentsYAML: componentsYAML
Expand Down
2 changes: 1 addition & 1 deletion docker/docker-compose.2204.59.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ services:
test:
image: *image
environment:
- WARN_AS_ERROR_ARG=-Xswiftc -warnings-as-errors
# - WARN_AS_ERROR_ARG=-Xswiftc -warnings-as-errors
- IMPORT_CHECK_ARG=--explicit-target-dependency-import-check error
- STRICT_CONCURRENCY_ARG=-Xswiftc -strict-concurrency=complete
shell:
Expand Down
2 changes: 1 addition & 1 deletion docker/docker-compose.2204.590.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ services:
test:
image: *image
environment:
- WARN_AS_ERROR_ARG=-Xswiftc -warnings-as-errors
# - WARN_AS_ERROR_ARG=-Xswiftc -warnings-as-errors
- IMPORT_CHECK_ARG=--explicit-target-dependency-import-check error
- STRICT_CONCURRENCY_ARG=-Xswiftc -strict-concurrency=complete
shell:
Expand Down