aws-amplify / amplify-codegen

Amplify Codegen is a JavaScript toolkit library for frontend and mobile developers building Amplify applications.
Apache License 2.0
58 stars 59 forks source link

“Break” embedded type model fails to compile in Xcode #495

Open manaswi223 opened 1 year ago

manaswi223 commented 1 year ago

Describe the bug

An embedded type model named Break fails to compile on Xcode complaining about reserved keywords although the codegen generates code & compiles it successfully.

The build fail error: Keyword 'break' cannot be used as an identifier here

Screen Shot 2022-10-17 at 11 26 25 AM

As a fix, tried to add a backtick to the break keyword in the generated files, but still fails complaining about missing parameter:

Screen Shot 2022-10-17 at 11 29 12 AM

Steps To Reproduce

Steps to reproduce the behavior:
1. Create an embedded type model named "Break", like:
`type Break {
  break_id: Int
  payment_mode: String
}`
2. generate models through `amplify codegen models` & observe logs that indicate successful codegenearation 
3. Now, try to build the app on Xcode & see how it fails
4. Try adding `backtick` as a workaround but still fails complaining about the missing parameter

Expected behavior

scenario 1: amplify codegen models successfully generates codegenerated files & code builds on Xcode (after a potential bug-fix)

scenario 2: amplify codegen models throws error on the CLI to consider renaming embedded types in their schemas to avoid usage of the reserved keywords on Xcode

Amplify Framework Version

main

Amplify Categories

DataStore

Dependency manager

Cocoapods

Swift version

5.0

CLI version

9.1.0

Xcode version

14.0.1

Relevant log output

<details>
<summary>Log Messages</summary>

INSERT LOG MESSAGES HERE



### Is this a regression?

No

### Regression additional context

_No response_

### Device

iPhone 14 pro

### iOS Version

iOS 16

### Specific to simulators

_No response_

### Additional context

_No response_
lawmicha commented 1 year ago

Hi @manaswi223, your fix sounds like the right approach, and this appears to compile for me

// swiftlint:disable all
import Amplify
import Foundation

public struct Break: Embeddable {
  var break_id: Int?
  var payment_mode: String?
}

extension Break {
  // MARK: - CodingKeys
   public enum CodingKeys: String, ModelKey {
    case break_id
    case payment_mode
  }

  public static let keys = CodingKeys.self
  //  MARK: - ModelSchema

  public static let schema = defineSchema { model in
    let `break` = Break.keys

    model.pluralName = "Breaks"

    model.fields(
      .field(`break`.break_id, is: .optional, ofType: .int),
      .field(`break`.payment_mode, is: .optional, ofType: .string)
    )
    }
}

You probably missed the backticks at these two lines:

image

Considering "break" is a special keyword in Swift, we should either globally denylist this word when compiling the GraphQL schema.graphql or generate the backticks with the word "break" for Swift. Does the schema break on other platforms such as Flutter, JS or Android?

alharris-at commented 1 year ago

Thank you for reporting @manaswi223 - we currently don't maintain a platform-specific denylist as far as I'm aware, but logging this as a feature request.