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

[Swift] Fix escaping of parameter and class names #1452

Merged
merged 6 commits into from
Jul 30, 2019
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
- `apollo-codegen-scala`
- <First `apollo-codegen-scala` related entry goes here>
- `apollo-codegen-swift`
- <First `apollo-codegen-swift` related entry goes here>
- Fix issue where type names were not being properly escaped [iOS 193](https://github.com/apollographql/apollo-ios/issues/193)
- `apollo-codegen-typescript`
- <First `apollo-codegen-typescript` related entry goes here>
- `apollo-env`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,175 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Swift code generation #classDeclarationForOperation() should correctly escape a mutli-line string literal 1`] = `
"public final class CreateReviewMutation: GraphQLMutation {
public let operationDefinition =
\\"mutation CreateReview($episode: Episode) {\\\\n createReview(episode: $episode, review: {stars: 5, commentary: \\\\\\"\\\\\\"\\\\\\"\\\\n Wow!\\\\n \\\\n This movie ROCKED!\\\\n \\\\\\"\\\\\\"\\\\\\"}) {\\\\n stars\\\\n commentary\\\\n }\\\\n}\\"

public let operationName = \\"CreateReview\\"

public var episode: Episode?

public init(episode: Episode? = nil) {
self.episode = episode
}

public var variables: GraphQLMap? {
return [\\"episode\\": episode]
}

public struct Data: GraphQLSelectionSet {
public static let possibleTypes = [\\"Mutation\\"]

public static let selections: [GraphQLSelection] = [
GraphQLField(\\"createReview\\", arguments: [\\"episode\\": GraphQLVariable(\\"episode\\"), \\"review\\": [\\"stars\\": 5, \\"commentary\\": \\"Wow!\\\\n\\\\n This movie ROCKED!\\"]], type: .object(CreateReview.selections)),
]

public private(set) var resultMap: ResultMap

public init(unsafeResultMap: ResultMap) {
self.resultMap = unsafeResultMap
}

public init(createReview: CreateReview? = nil) {
self.init(unsafeResultMap: [\\"__typename\\": \\"Mutation\\", \\"createReview\\": createReview.flatMap { (value: CreateReview) -> ResultMap in value.resultMap }])
}

public var createReview: CreateReview? {
get {
return (resultMap[\\"createReview\\"] as? ResultMap).flatMap { CreateReview(unsafeResultMap: $0) }
}
set {
resultMap.updateValue(newValue?.resultMap, forKey: \\"createReview\\")
}
}

public struct CreateReview: GraphQLSelectionSet {
public static let possibleTypes = [\\"Review\\"]

public static let selections: [GraphQLSelection] = [
GraphQLField(\\"stars\\", type: .nonNull(.scalar(Int.self))),
GraphQLField(\\"commentary\\", type: .scalar(String.self)),
]

public private(set) var resultMap: ResultMap

public init(unsafeResultMap: ResultMap) {
self.resultMap = unsafeResultMap
}

public init(stars: Int, commentary: String? = nil) {
self.init(unsafeResultMap: [\\"__typename\\": \\"Review\\", \\"stars\\": stars, \\"commentary\\": commentary])
}

/// The number of stars this review gave, 1-5
public var stars: Int {
get {
return resultMap[\\"stars\\"]! as! Int
}
set {
resultMap.updateValue(newValue, forKey: \\"stars\\")
}
}

/// Comment about the movie
public var commentary: String? {
get {
return resultMap[\\"commentary\\"] as? String
}
set {
resultMap.updateValue(newValue, forKey: \\"commentary\\")
}
}
}
}
}"
`;

exports[`Swift code generation #classDeclarationForOperation() should correctly escape a mutli-line string literal with backslashes 1`] = `
"public final class CreateReviewMutation: GraphQLMutation {
public let operationDefinition =
\\"mutation CreateReview($episode: Episode) {\\\\n createReview(episode: $episode, review: {stars: 5, commentary: \\\\\\"\\\\\\"\\\\\\"\\\\n Wow!\\\\n \\\\n This movie \\\\ ROCKED!\\\\n \\\\\\"\\\\\\"\\\\\\"}) {\\\\n stars\\\\n commentary\\\\n }\\\\n}\\"

public let operationName = \\"CreateReview\\"

public var episode: Episode?

public init(episode: Episode? = nil) {
self.episode = episode
}

public var variables: GraphQLMap? {
return [\\"episode\\": episode]
}

public struct Data: GraphQLSelectionSet {
public static let possibleTypes = [\\"Mutation\\"]

public static let selections: [GraphQLSelection] = [
GraphQLField(\\"createReview\\", arguments: [\\"episode\\": GraphQLVariable(\\"episode\\"), \\"review\\": [\\"stars\\": 5, \\"commentary\\": \\"Wow!\\\\n\\\\n This movie \\\\\\\\ ROCKED!\\"]], type: .object(CreateReview.selections)),
]

public private(set) var resultMap: ResultMap

public init(unsafeResultMap: ResultMap) {
self.resultMap = unsafeResultMap
}

public init(createReview: CreateReview? = nil) {
self.init(unsafeResultMap: [\\"__typename\\": \\"Mutation\\", \\"createReview\\": createReview.flatMap { (value: CreateReview) -> ResultMap in value.resultMap }])
}

public var createReview: CreateReview? {
get {
return (resultMap[\\"createReview\\"] as? ResultMap).flatMap { CreateReview(unsafeResultMap: $0) }
}
set {
resultMap.updateValue(newValue?.resultMap, forKey: \\"createReview\\")
}
}

public struct CreateReview: GraphQLSelectionSet {
public static let possibleTypes = [\\"Review\\"]

public static let selections: [GraphQLSelection] = [
GraphQLField(\\"stars\\", type: .nonNull(.scalar(Int.self))),
GraphQLField(\\"commentary\\", type: .scalar(String.self)),
]

public private(set) var resultMap: ResultMap

public init(unsafeResultMap: ResultMap) {
self.resultMap = unsafeResultMap
}

public init(stars: Int, commentary: String? = nil) {
self.init(unsafeResultMap: [\\"__typename\\": \\"Review\\", \\"stars\\": stars, \\"commentary\\": commentary])
}

/// The number of stars this review gave, 1-5
public var stars: Int {
get {
return resultMap[\\"stars\\"]! as! Int
}
set {
resultMap.updateValue(newValue, forKey: \\"stars\\")
}
}

/// Comment about the movie
public var commentary: String? {
get {
return resultMap[\\"commentary\\"] as? String
}
set {
resultMap.updateValue(newValue, forKey: \\"commentary\\")
}
}
}
}
}"
`;

exports[`Swift code generation #classDeclarationForOperation() should generate a class declaration for a mutation with variables 1`] = `
"public final class CreateReviewMutation: GraphQLMutation {
public let operationDefinition =
Expand Down
42 changes: 42 additions & 0 deletions packages/apollo-codegen-swift/src/__tests__/codeGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,48 @@ describe("Swift code generation", () => {
expect(generator.output).toMatchSnapshot();
});

it("should correctly escape a mutli-line string literal", () => {
const { operations } = compile(`
mutation CreateReview($episode: Episode) {
createReview(episode: $episode, review: {stars: 5, commentary:
"""
Wow!

This movie ROCKED!
"""
}) {
stars
commentary
}
}
`);

generator.classDeclarationForOperation(operations["CreateReview"]);

expect(generator.output).toMatchSnapshot();
});

it("should correctly escape a mutli-line string literal with backslashes", () => {
const { operations } = compile(`
mutation CreateReview($episode: Episode) {
createReview(episode: $episode, review: {stars: 5, commentary:
"""
Wow!

This movie \\ ROCKED!
"""
}) {
stars
commentary
}
}
`);

generator.classDeclarationForOperation(operations["CreateReview"]);

expect(generator.output).toMatchSnapshot();
});

it(`should generate a class declaration for a query with a fragment spread nested in an inline fragment`, () => {
const { operations } = compile(`
query Hero {
Expand Down
38 changes: 38 additions & 0 deletions packages/apollo-codegen-swift/src/__tests__/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ describe("Swift code generation: Basic language constructs", () => {
`);
});

it(`should generate a class declaration with proper escaping`, () => {
generator.classDeclaration(
{ className: "Type", modifiers: ["public", "final"] },
() => {
generator.propertyDeclaration({
propertyName: "name",
typeName: "String"
});
generator.propertyDeclaration({
propertyName: "age",
typeName: "Int"
});
generator.propertyDeclaration({
propertyName: "self",
typeName: "Self"
});
}
);

expect(generator.output).toBe(stripIndent`
public final class \`Type\` {
public var name: String
public var age: Int
public var \`self\`: \`Self\`
}
`);
});

it(`should generate a struct declaration`, () => {
generator.structDeclaration({ structName: "Hero" }, () => {
generator.propertyDeclaration({
Expand Down Expand Up @@ -100,12 +128,17 @@ describe("Swift code generation: Basic language constructs", () => {
propertyName: "yearOfBirth",
typeName: "Int"
});
generator.propertyDeclaration({
propertyName: "self",
typeName: "Self"
});
});

expect(generator.output).toBe(stripIndent`
public struct \`Type\` {
public var name: String
public var yearOfBirth: Int
public var \`self\`: \`Self\`
}
`);
});
Expand Down Expand Up @@ -153,13 +186,18 @@ describe("Swift code generation: Basic language constructs", () => {
propertyName: "age",
typeName: "Int"
});
generator.protocolPropertyDeclaration({
propertyName: "default",
typeName: "Boolean"
});
}
);

expect(generator.output).toBe(stripIndent`
public protocol HeroDetails: HasName {
var name: String { get }
var age: Int { get }
var \`default\`: Boolean { get }
}
`);
});
Expand Down
11 changes: 8 additions & 3 deletions packages/apollo-codegen-swift/src/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ export class SwiftGenerator<Context> extends CodeGenerator<
) {
this.printNewlineIfNeeded();
this.printOnNewline(
wrap("", join(modifiers, " "), " ") + `class ${className}`
wrap("", join(modifiers, " "), " ") +
`class ${escapeIdentifierIfNeeded(className)}`
);
this.print(wrap(": ", join([superClass, ...adoptedProtocols], ", ")));
this.pushScope({ typeName: className });
Expand Down Expand Up @@ -164,7 +165,9 @@ export class SwiftGenerator<Context> extends CodeGenerator<
propertyDeclaration({ propertyName, typeName, description }: Property) {
this.comment(description);
this.printOnNewline(
`public var ${escapeIdentifierIfNeeded(propertyName)}: ${typeName}`
`public var ${escapeIdentifierIfNeeded(
propertyName
)}: ${escapeIdentifierIfNeeded(typeName)}`
);
}

Expand All @@ -186,7 +189,9 @@ export class SwiftGenerator<Context> extends CodeGenerator<
}

protocolPropertyDeclaration({ propertyName, typeName }: Property) {
this.printOnNewline(`var ${propertyName}: ${typeName} { get }`);
this.printOnNewline(
`var ${escapeIdentifierIfNeeded(propertyName)}: ${typeName} { get }`
);
}

protocolPropertyDeclarations(properties: Property[]) {
Expand Down