Skip to content

Commit

Permalink
fix: Disallow certain targetNames in code generation (#2958) (#2972)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobaFetters authored Apr 20, 2023
1 parent a2927b6 commit dab1c32
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
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

0 comments on commit dab1c32

Please sign in to comment.