amplify-codegen icon indicating copy to clipboard operation
amplify-codegen copied to clipboard

Generated swift model for a hasOne relation uses incorrect arguments

Open concavegit opened this issue 1 year ago • 0 comments

Environment information

System:
  OS: macOS 15.0
  CPU: (8) arm64 Apple M2
  Memory: 129.42 MB / 24.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 22.9.0 - /opt/homebrew/bin/node
  Yarn: undefined - undefined
  npm: 10.8.3 - /opt/homebrew/bin/npm
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/auth-construct: 1.3.1
  @aws-amplify/backend: 1.3.0
  @aws-amplify/backend-auth: 1.2.0
  @aws-amplify/backend-cli: 1.2.8
  @aws-amplify/backend-data: 1.1.4
  @aws-amplify/backend-deployer: 1.1.3
  @aws-amplify/backend-function: 1.5.0
  @aws-amplify/backend-output-schemas: 1.2.0
  @aws-amplify/backend-output-storage: 1.1.2
  @aws-amplify/backend-secret: 1.1.2
  @aws-amplify/backend-storage: 1.2.0
  @aws-amplify/cli-core: 1.1.3
  @aws-amplify/client-config: 1.3.1
  @aws-amplify/deployed-backend-client: 1.4.1
  @aws-amplify/form-generator: 1.0.3
  @aws-amplify/model-generator: 1.0.8
  @aws-amplify/platform-core: 1.1.0
  @aws-amplify/plugin-types: 1.3.0
  @aws-amplify/sandbox: 1.2.2
  @aws-amplify/schema-generator: 1.2.4
  aws-amplify: 6.6.2
  aws-cdk: 2.159.1
  aws-cdk-lib: 2.159.1
  typescript: 5.6.2
AWS environment variables:
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Codegen packages

[email protected] /Users/concaveusr/Projects/KanaiAmplify
└─┬ @aws-amplify/[email protected]
  └─┬ @aws-amplify/[email protected]
    └── @aws-amplify/[email protected]

Description

hasOne relations generate incorrect Swift code.

If I define a hasOne model like this:

const schema = a.schema({
  Cart: a.model({
    customerId: a.id(),
    customer: a.belongsTo('Customer', 'customerId'),
  }),
  Customer: a.model({
    activeCart: a.hasOne('Cart', 'customerId')
  }),
});

Then the Swift generated code has a line like

    model.fields(
...
      .hasOne(customer.activeCart, is: .optional, ofType: Cart.self, associatedFields: [Cart.customerId])
...
    )

This throws the following errors:

Argument type '[Cart.CodingKeys]' does not conform to expected type 'CodingKey'
Incorrect argument label in call (have '_:is:ofType:associatedFields:', expected '_:is:ofType:associatedWith:')

I can silence these errors by using the correct function label associatedWith and by removing the square brackets:

      .hasOne(customer.activeCart, is: .optional, ofType: Cart.self, associatedWith: Cart.customerId)

concavegit avatar Sep 23 '24 18:09 concavegit