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

fix: Disallow certain targetNames in code generation (#2958) #2972

Merged
merged 1 commit into from
Apr 20, 2023
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: 11 additions & 0 deletions Sources/ApolloCodegenLib/ApolloCodegen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class ApolloCodegen {
case cannotLoadOperations
case invalidConfiguration(message: String)
case invalidSchemaName(_ name: String, message: String)
case targetNameConflict(name: String)

public var errorDescription: String? {
switch self {
Expand Down Expand Up @@ -50,6 +51,11 @@ public class ApolloCodegen {
return "The codegen configuration has conflicting values: \(message)"
case let .invalidSchemaName(name, message):
return "The schema namespace `\(name)` is invalid: \(message)"
case let .targetNameConflict(name):
return """
Target name '\(name)' conflicts with a reserved library name. Please choose a different \
target name.
"""
}
}
}
Expand Down Expand Up @@ -172,6 +178,11 @@ public class ApolloCodegen {
value to 'false', or choose a different module type, to resolve the conflict.
""")
}

if case let .embeddedInTarget(targetName) = context.output.schemaTypes.moduleType,
SwiftKeywords.DisallowedEmbeddedTargetNames.contains(targetName.lowercased()) {
throw Error.targetNameConflict(name: targetName)
}

for searchPath in context.input.schemaSearchPaths {
try validate(inputSearchPath: searchPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ enum SwiftKeywords {
static let DisallowedSchemaNamespaceNames: Set<String> = [
"schema", "apolloapi"
]

static let DisallowedEmbeddedTargetNames: Set<String> = [
"apollo", "apolloapi"
]

static let SelectionSetTypeNamesToSuffix: Set<String> = [
"Any",
Expand Down
18 changes: 18 additions & 0 deletions Tests/ApolloCodegenTests/ApolloCodegenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2157,6 +2157,24 @@ class ApolloCodegenTests: XCTestCase {
.to(throwError(ApolloCodegen.Error.schemaNameConflict(name: config.schemaNamespace)))
}
}

func test__validation__givenTargetName_matchingDisallowedTargetName_shouldThrow() throws {
// given
let disallowedNames = ["apollo", "Apollo", "apolloapi", "ApolloAPI"]

// when
for name in disallowedNames {
let config = ApolloCodegenConfiguration.mock(
output: .mock(
moduleType: .embeddedInTarget(name: name)
)
)

// then
expect(try ApolloCodegen._validate(config: config))
.to(throwError(ApolloCodegen.Error.targetNameConflict(name: name)))
}
}

func test__validation__givenEmptySchemaName_shouldThrow() throws {
let config = ApolloCodegenConfiguration.mock(schemaNamespace: "")
Expand Down