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

Adds disallowed names to schema namespace validation #2755

Merged
merged 2 commits into from
Jan 6, 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
5 changes: 3 additions & 2 deletions Sources/ApolloCodegenLib/ApolloCodegen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public class ApolloCodegen {
"""
case let .schemaNameConflict(name):
return """
Schema name \(name) conflicts with name of a type in your GraphQL schema. Please \
choose a different schema name. Suggestions: \(name)Schema, \(name)GraphQL, \(name)API
Schema name '\(name)' conflicts with name of a type in the generated code. Please choose \
a different schema name. Suggestions: \(name)Schema, \(name)GraphQL, \(name)API.
"""
case .cannotLoadSchema:
return "A GraphQL schema could not be found. Please verify the schema search paths."
Expand Down Expand Up @@ -162,6 +162,7 @@ public class ApolloCodegen {

static func validate(schemaName: String, compilationResult: CompilationResult) throws {
guard
!SwiftKeywords.DisallowedSchemaNamespaceNames.contains(schemaName.lowercased()),
!compilationResult.referencedTypes.contains(where: { namedType in
namedType.swiftName == schemaName.firstUppercased
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ enum SwiftKeywords {
"self", "_"
]

static let DisallowedSchemaNamespaceNames: Set<String> = [
"schema", "apolloapi"
]

static let SelectionSetTypeNamesToSuffix: Set<String> = [
"Any",
"DataDict",
Expand Down
18 changes: 18 additions & 0 deletions Tests/ApolloCodegenTests/ApolloCodegenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2158,6 +2158,24 @@ class ApolloCodegenTests: XCTestCase {
}
}

func test__validation__givenSchemaName_matchingDisallowedSchemaNamespaceName_shouldThrow() throws {
// given
let disallowedNames = ["schema", "Schema", "ApolloAPI", "apolloapi"]

// when
for name in disallowedNames {
let configContext = ApolloCodegen.ConfigurationContext(config: .mock(
schemaName: name
), rootURL: nil)

// then
expect(try ApolloCodegen.validate(
schemaName: configContext.schemaName,
compilationResult: CompilationResult.mock()))
.to(throwError(ApolloCodegen.Error.schemaNameConflict(name: configContext.schemaName)))
}
}

func test__validation__givenUniqueSchemaName_shouldNotThrow() throws {
// given
let object = GraphQLObjectType.mock("MockObject")
Expand Down