From 0eaa8fef92301843d8240284b1fe9432907575df Mon Sep 17 00:00:00 2001 From: Dane Pilcher Date: Fri, 4 Oct 2024 10:20:22 -0600 Subject: [PATCH 1/3] test: add snapshot for list decode optionality --- .../appsync-swift-visitor.test.ts.snap | 320 ++++++++++++++++++ .../visitors/appsync-swift-visitor.test.ts | 113 ++++++- 2 files changed, 426 insertions(+), 7 deletions(-) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap index 9d94969ad..fa77399d9 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -2904,3 +2904,323 @@ extension ModelPath where ModelType == SqlRelated { } }" `; + +exports[`AppSyncSwiftVisitor lists decode optional list with nullable elements 1`] = ` +"// swiftlint:disable all +import Amplify +import Foundation + +public struct MyModel: Model { + public let id: String + public var context: String? + public var myCustomTypes: [MyCustomType?]? + public var scalarArray: [String?]? + public var myOtherModelId: String + internal var _myOtherModel: LazyReference + public var myOtherModel: MyOtherModel? { + get async throws { + try await _myOtherModel.get() + } + } + public var createdAt: Temporal.DateTime? + public var updatedAt: Temporal.DateTime? + + public init(id: String = UUID().uuidString, + context: String? = nil, + myCustomTypes: [MyCustomType?]? = nil, + scalarArray: [String?]? = nil, + myOtherModelId: String, + myOtherModel: MyOtherModel? = nil) { + self.init(id: id, + context: context, + myCustomTypes: myCustomTypes, + scalarArray: scalarArray, + myOtherModelId: myOtherModelId, + myOtherModel: myOtherModel, + createdAt: nil, + updatedAt: nil) + } + internal init(id: String = UUID().uuidString, + context: String? = nil, + myCustomTypes: [MyCustomType?]? = nil, + scalarArray: [String?]? = nil, + myOtherModelId: String, + myOtherModel: MyOtherModel? = nil, + createdAt: Temporal.DateTime? = nil, + updatedAt: Temporal.DateTime? = nil) { + self.id = id + self.context = context + self.myCustomTypes = myCustomTypes + self.scalarArray = scalarArray + self.myOtherModelId = myOtherModelId + self._myOtherModel = LazyReference(myOtherModel) + self.createdAt = createdAt + self.updatedAt = updatedAt + } + public mutating func setMyOtherModel(_ myOtherModel: MyOtherModel? = nil) { + self._myOtherModel = LazyReference(myOtherModel) + } + public init(from decoder: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, forKey: .id) + context = try? values.decode(String?.self, forKey: .context) + myCustomTypes = try? values.decode([MyCustomType].self, forKey: .myCustomTypes) + scalarArray = try? values.decode([String].self, forKey: .scalarArray) + myOtherModelId = try values.decode(String.self, forKey: .myOtherModelId) + _myOtherModel = try values.decodeIfPresent(LazyReference.self, forKey: .myOtherModel) ?? LazyReference(identifiers: nil) + createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(context, forKey: .context) + try container.encode(myCustomTypes, forKey: .myCustomTypes) + try container.encode(scalarArray, forKey: .scalarArray) + try container.encode(myOtherModelId, forKey: .myOtherModelId) + try container.encode(_myOtherModel, forKey: .myOtherModel) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } +}" +`; + +exports[`AppSyncSwiftVisitor lists decode optional list with required elements 1`] = ` +"// swiftlint:disable all +import Amplify +import Foundation + +public struct MyModel: Model { + public let id: String + public var context: String? + public var myCustomTypes: [MyCustomType]? + public var scalarArray: [String]? + public var myOtherModelId: String + internal var _myOtherModel: LazyReference + public var myOtherModel: MyOtherModel? { + get async throws { + try await _myOtherModel.get() + } + } + public var createdAt: Temporal.DateTime? + public var updatedAt: Temporal.DateTime? + + public init(id: String = UUID().uuidString, + context: String? = nil, + myCustomTypes: [MyCustomType]? = nil, + scalarArray: [String]? = nil, + myOtherModelId: String, + myOtherModel: MyOtherModel? = nil) { + self.init(id: id, + context: context, + myCustomTypes: myCustomTypes, + scalarArray: scalarArray, + myOtherModelId: myOtherModelId, + myOtherModel: myOtherModel, + createdAt: nil, + updatedAt: nil) + } + internal init(id: String = UUID().uuidString, + context: String? = nil, + myCustomTypes: [MyCustomType]? = nil, + scalarArray: [String]? = nil, + myOtherModelId: String, + myOtherModel: MyOtherModel? = nil, + createdAt: Temporal.DateTime? = nil, + updatedAt: Temporal.DateTime? = nil) { + self.id = id + self.context = context + self.myCustomTypes = myCustomTypes + self.scalarArray = scalarArray + self.myOtherModelId = myOtherModelId + self._myOtherModel = LazyReference(myOtherModel) + self.createdAt = createdAt + self.updatedAt = updatedAt + } + public mutating func setMyOtherModel(_ myOtherModel: MyOtherModel? = nil) { + self._myOtherModel = LazyReference(myOtherModel) + } + public init(from decoder: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, forKey: .id) + context = try? values.decode(String?.self, forKey: .context) + myCustomTypes = try values.decode([MyCustomType].self, forKey: .myCustomTypes) + scalarArray = try values.decode([String].self, forKey: .scalarArray) + myOtherModelId = try values.decode(String.self, forKey: .myOtherModelId) + _myOtherModel = try values.decodeIfPresent(LazyReference.self, forKey: .myOtherModel) ?? LazyReference(identifiers: nil) + createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(context, forKey: .context) + try container.encode(myCustomTypes, forKey: .myCustomTypes) + try container.encode(scalarArray, forKey: .scalarArray) + try container.encode(myOtherModelId, forKey: .myOtherModelId) + try container.encode(_myOtherModel, forKey: .myOtherModel) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } +}" +`; + +exports[`AppSyncSwiftVisitor lists decode required list with nullable elements 1`] = ` +"// swiftlint:disable all +import Amplify +import Foundation + +public struct MyModel: Model { + public let id: String + public var context: String? + public var myCustomTypes: [MyCustomType?] + public var scalarArray: [String?] + public var myOtherModelId: String + internal var _myOtherModel: LazyReference + public var myOtherModel: MyOtherModel? { + get async throws { + try await _myOtherModel.get() + } + } + public var createdAt: Temporal.DateTime? + public var updatedAt: Temporal.DateTime? + + public init(id: String = UUID().uuidString, + context: String? = nil, + myCustomTypes: [MyCustomType?] = [], + scalarArray: [String?] = [], + myOtherModelId: String, + myOtherModel: MyOtherModel? = nil) { + self.init(id: id, + context: context, + myCustomTypes: myCustomTypes, + scalarArray: scalarArray, + myOtherModelId: myOtherModelId, + myOtherModel: myOtherModel, + createdAt: nil, + updatedAt: nil) + } + internal init(id: String = UUID().uuidString, + context: String? = nil, + myCustomTypes: [MyCustomType?] = [], + scalarArray: [String?] = [], + myOtherModelId: String, + myOtherModel: MyOtherModel? = nil, + createdAt: Temporal.DateTime? = nil, + updatedAt: Temporal.DateTime? = nil) { + self.id = id + self.context = context + self.myCustomTypes = myCustomTypes + self.scalarArray = scalarArray + self.myOtherModelId = myOtherModelId + self._myOtherModel = LazyReference(myOtherModel) + self.createdAt = createdAt + self.updatedAt = updatedAt + } + public mutating func setMyOtherModel(_ myOtherModel: MyOtherModel? = nil) { + self._myOtherModel = LazyReference(myOtherModel) + } + public init(from decoder: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, forKey: .id) + context = try? values.decode(String?.self, forKey: .context) + myCustomTypes = try? values.decode([MyCustomType].self, forKey: .myCustomTypes) + scalarArray = try? values.decode([String].self, forKey: .scalarArray) + myOtherModelId = try values.decode(String.self, forKey: .myOtherModelId) + _myOtherModel = try values.decodeIfPresent(LazyReference.self, forKey: .myOtherModel) ?? LazyReference(identifiers: nil) + createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(context, forKey: .context) + try container.encode(myCustomTypes, forKey: .myCustomTypes) + try container.encode(scalarArray, forKey: .scalarArray) + try container.encode(myOtherModelId, forKey: .myOtherModelId) + try container.encode(_myOtherModel, forKey: .myOtherModel) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } +}" +`; + +exports[`AppSyncSwiftVisitor lists decode required list with required elements 1`] = ` +"// swiftlint:disable all +import Amplify +import Foundation + +public struct MyModel: Model { + public let id: String + public var context: String? + public var myCustomTypes: [MyCustomType] + public var scalarArray: [String] + public var myOtherModelId: String + internal var _myOtherModel: LazyReference + public var myOtherModel: MyOtherModel? { + get async throws { + try await _myOtherModel.get() + } + } + public var createdAt: Temporal.DateTime? + public var updatedAt: Temporal.DateTime? + + public init(id: String = UUID().uuidString, + context: String? = nil, + myCustomTypes: [MyCustomType] = [], + scalarArray: [String] = [], + myOtherModelId: String, + myOtherModel: MyOtherModel? = nil) { + self.init(id: id, + context: context, + myCustomTypes: myCustomTypes, + scalarArray: scalarArray, + myOtherModelId: myOtherModelId, + myOtherModel: myOtherModel, + createdAt: nil, + updatedAt: nil) + } + internal init(id: String = UUID().uuidString, + context: String? = nil, + myCustomTypes: [MyCustomType] = [], + scalarArray: [String] = [], + myOtherModelId: String, + myOtherModel: MyOtherModel? = nil, + createdAt: Temporal.DateTime? = nil, + updatedAt: Temporal.DateTime? = nil) { + self.id = id + self.context = context + self.myCustomTypes = myCustomTypes + self.scalarArray = scalarArray + self.myOtherModelId = myOtherModelId + self._myOtherModel = LazyReference(myOtherModel) + self.createdAt = createdAt + self.updatedAt = updatedAt + } + public mutating func setMyOtherModel(_ myOtherModel: MyOtherModel? = nil) { + self._myOtherModel = LazyReference(myOtherModel) + } + public init(from decoder: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + id = try values.decode(String.self, forKey: .id) + context = try? values.decode(String?.self, forKey: .context) + myCustomTypes = try values.decode([MyCustomType].self, forKey: .myCustomTypes) + scalarArray = try values.decode([String].self, forKey: .scalarArray) + myOtherModelId = try values.decode(String.self, forKey: .myOtherModelId) + _myOtherModel = try values.decodeIfPresent(LazyReference.self, forKey: .myOtherModel) ?? LazyReference(identifiers: nil) + createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) + updatedAt = try? values.decode(Temporal.DateTime?.self, forKey: .updatedAt) + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(context, forKey: .context) + try container.encode(myCustomTypes, forKey: .myCustomTypes) + try container.encode(scalarArray, forKey: .scalarArray) + try container.encode(myOtherModelId, forKey: .myOtherModelId) + try container.encode(_myOtherModel, forKey: .myOtherModel) + try container.encode(createdAt, forKey: .createdAt) + try container.encode(updatedAt, forKey: .updatedAt) + } +}" +`; diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts index 2ee5581ff..ebddff3f9 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts @@ -3006,7 +3006,7 @@ describe('AppSyncSwiftVisitor', () => { respectPrimaryKeyAttributesOnConnectionField: true, }).generate()).toMatchSnapshot(); }); - + test('sets the association to the references field for hasOne/belongsTo', () => { const schema = /* GraphQL */ ` type SqlPrimary @refersTo(name: "sql_primary") @model { @@ -3035,7 +3035,7 @@ describe('AppSyncSwiftVisitor', () => { respectPrimaryKeyAttributesOnConnectionField: true, }).generate()).toMatchSnapshot(); }); - + test('sets the association to the references field for hasOne and hasMany', () => { const schema = /* GraphQL */ ` type Primary @model { @@ -3075,7 +3075,7 @@ describe('AppSyncSwiftVisitor', () => { respectPrimaryKeyAttributesOnConnectionField: true, }).generate()).toMatchSnapshot(); }); - + test('double linked references', () => { const schema = /* GraphQL */ ` type Foo @model { @@ -3105,7 +3105,7 @@ describe('AppSyncSwiftVisitor', () => { respectPrimaryKeyAttributesOnConnectionField: true, }).generate()).toMatchSnapshot(); }); - + test('hasMany with sortKeyFields on primary key', () => { const schema = /* GraphQL */ ` type Primary @model { @@ -3124,7 +3124,7 @@ describe('AppSyncSwiftVisitor', () => { primary: Primary @belongsTo(references: ["primaryTenantId", "primaryInstanceId", "primaryRecordId"]) } `; - + expect(getVisitorPipelinedTransformer(schema, 'Primary', CodeGenGenerateEnum.code, { respectPrimaryKeyAttributesOnConnectionField: true, }).generate()).toMatchSnapshot(); @@ -3138,7 +3138,7 @@ describe('AppSyncSwiftVisitor', () => { respectPrimaryKeyAttributesOnConnectionField: true, }).generate()).toMatchSnapshot(); }); - + test('hasOne with sortKeyFields on primary key', () => { const schema = /* GraphQL */ ` type Primary @model { @@ -3157,7 +3157,7 @@ describe('AppSyncSwiftVisitor', () => { primary: Primary @belongsTo(references: ["primaryTenantId", "primaryInstanceId", "primaryRecordId"]) } `; - + expect(getVisitorPipelinedTransformer(schema, 'Primary', CodeGenGenerateEnum.code, { respectPrimaryKeyAttributesOnConnectionField: true, }).generate()).toMatchSnapshot(); @@ -3172,4 +3172,103 @@ describe('AppSyncSwiftVisitor', () => { }).generate()).toMatchSnapshot(); }); }); + describe('lists', () => { + test('decode required list with nullable elements', () => { + const schema = /* GraphQL */ ` + type MyModel @model + { + context: String + myCustomTypes: [MyCustomType]! + scalarArray: [String]! + myOtherModelId: ID! + myOtherModel: MyOtherModel @belongsTo(references: ["myOtherModelId"]) + } + + type MyOtherModel @model + { + context: String + myModel: MyModel @hasOne(references: ["myOtherModelId"]) + } + + type MyCustomType + { + context: String + } + `; + expect(getVisitorPipelinedTransformer(schema, 'MyModel', CodeGenGenerateEnum.code, {}).generate()).toMatchSnapshot(); + }); + test('decode optional list with required elements', () => { + const schema = /* GraphQL */ ` + type MyModel @model + { + context: String + myCustomTypes: [MyCustomType!] + scalarArray: [String!] + myOtherModelId: ID! + myOtherModel: MyOtherModel @belongsTo(references: ["myOtherModelId"]) + } + + type MyOtherModel @model + { + context: String + myModel: MyModel @hasOne(references: ["myOtherModelId"]) + } + + type MyCustomType + { + context: String + } + `; + expect(getVisitorPipelinedTransformer(schema, 'MyModel', CodeGenGenerateEnum.code, {}).generate()).toMatchSnapshot(); + }); + test('decode required list with required elements', () => { + const schema = /* GraphQL */ ` + type MyModel @model + { + context: String + myCustomTypes: [MyCustomType!]! + scalarArray: [String!]! + myOtherModelId: ID! + myOtherModel: MyOtherModel @belongsTo(references: ["myOtherModelId"]) + } + + type MyOtherModel @model + { + context: String + myModel: MyModel @hasOne(references: ["myOtherModelId"]) + } + + type MyCustomType + { + context: String + } + `; + expect(getVisitorPipelinedTransformer(schema, 'MyModel', CodeGenGenerateEnum.code, {}).generate()).toMatchSnapshot(); + }); + + test('decode optional list with nullable elements', () => { + const schema = /* GraphQL */ ` + type MyModel @model + { + context: String + myCustomTypes: [MyCustomType] + scalarArray: [String] + myOtherModelId: ID! + myOtherModel: MyOtherModel @belongsTo(references: ["myOtherModelId"]) + } + + type MyOtherModel @model + { + context: String + myModel: MyModel @hasOne(references: ["myOtherModelId"]) + } + + type MyCustomType + { + context: String + } + `; + expect(getVisitorPipelinedTransformer(schema, 'MyModel', CodeGenGenerateEnum.code, {}).generate()).toMatchSnapshot(); + }); + }); }); From 6ecafeb539c63d6edbede93b7bf35cc3c2cd2d1f Mon Sep 17 00:00:00 2001 From: Dane Pilcher Date: Fri, 4 Oct 2024 10:21:31 -0600 Subject: [PATCH 2/3] fix: set correct optionality when decoding list type --- .../__snapshots__/appsync-swift-visitor.test.ts.snap | 12 ++++++------ .../src/visitors/appsync-swift-visitor.ts | 10 ++++++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap index fa77399d9..12317d415 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-swift-visitor.test.ts.snap @@ -2964,8 +2964,8 @@ public struct MyModel: Model { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, forKey: .id) context = try? values.decode(String?.self, forKey: .context) - myCustomTypes = try? values.decode([MyCustomType].self, forKey: .myCustomTypes) - scalarArray = try? values.decode([String].self, forKey: .scalarArray) + myCustomTypes = try? values.decode([MyCustomType?].self, forKey: .myCustomTypes) + scalarArray = try? values.decode([String?].self, forKey: .scalarArray) myOtherModelId = try values.decode(String.self, forKey: .myOtherModelId) _myOtherModel = try values.decodeIfPresent(LazyReference.self, forKey: .myOtherModel) ?? LazyReference(identifiers: nil) createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) @@ -3044,8 +3044,8 @@ public struct MyModel: Model { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, forKey: .id) context = try? values.decode(String?.self, forKey: .context) - myCustomTypes = try values.decode([MyCustomType].self, forKey: .myCustomTypes) - scalarArray = try values.decode([String].self, forKey: .scalarArray) + myCustomTypes = try? values.decode([MyCustomType].self, forKey: .myCustomTypes) + scalarArray = try? values.decode([String].self, forKey: .scalarArray) myOtherModelId = try values.decode(String.self, forKey: .myOtherModelId) _myOtherModel = try values.decodeIfPresent(LazyReference.self, forKey: .myOtherModel) ?? LazyReference(identifiers: nil) createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) @@ -3124,8 +3124,8 @@ public struct MyModel: Model { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, forKey: .id) context = try? values.decode(String?.self, forKey: .context) - myCustomTypes = try? values.decode([MyCustomType].self, forKey: .myCustomTypes) - scalarArray = try? values.decode([String].self, forKey: .scalarArray) + myCustomTypes = try values.decode([MyCustomType?].self, forKey: .myCustomTypes) + scalarArray = try values.decode([String?].self, forKey: .scalarArray) myOtherModelId = try values.decode(String.self, forKey: .myOtherModelId) _myOtherModel = try values.decodeIfPresent(LazyReference.self, forKey: .myOtherModel) ?? LazyReference(identifiers: nil) createdAt = try? values.decode(Temporal.DateTime?.self, forKey: .createdAt) diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts index 4fc429ff8..0d6900074 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts @@ -557,7 +557,7 @@ export class AppSyncSwiftVisitor< const decodeMethod = field.connectionInfo ? 'decodeIfPresent' : 'decode'; const defaultLazyReference = connectionHasOneOrBelongsTo ? ' ?? LazyReference(identifiers: nil)' : ''; const defaultListReference = this.isHasManyConnectionField(field) ? ' ?? .init()' : ''; - const optionalTry = !this.isFieldRequired(field) && !field.connectionInfo ? '?' : ''; + const optionalTry = !field.connectionInfo && (field.isList ? field.isListNullable : !this.isFieldRequired(field)) ? '?' : ''; result.push( indent( `${assignedFieldName} = try${optionalTry} values.${decodeMethod}(${fieldType}.self, forKey: .${escapedFieldName})${defaultLazyReference}${defaultListReference}`, @@ -567,6 +567,12 @@ export class AppSyncSwiftVisitor< return result.join('\n'); } + /* + [string!]! try [string] + [String]! try [String?] + [String!] try? [String] + [string] try [string] + */ private getDecoderBodyFieldType(field: CodeGenField): string { const nativeType = this.getNativeType(field); @@ -579,7 +585,7 @@ export class AppSyncSwiftVisitor< if (field.connectionInfo) { return `List<${nativeType}>${optionality}`; } - return `[${nativeType}]`; + return `[${nativeType}${optionality}]`; } return `${nativeType}${optionality}`; } From 3849ed1360ecae5d62238abc8275d6e106c8afe6 Mon Sep 17 00:00:00 2001 From: Dane Pilcher Date: Fri, 4 Oct 2024 10:26:06 -0600 Subject: [PATCH 3/3] test: add additional assertions --- .../visitors/appsync-swift-visitor.test.ts | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts index ebddff3f9..8ed5de56f 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-swift-visitor.test.ts @@ -3195,7 +3195,10 @@ describe('AppSyncSwiftVisitor', () => { context: String } `; - expect(getVisitorPipelinedTransformer(schema, 'MyModel', CodeGenGenerateEnum.code, {}).generate()).toMatchSnapshot(); + const result = getVisitorPipelinedTransformer(schema, 'MyModel', CodeGenGenerateEnum.code, {}).generate(); + expect(result).toContain('myCustomTypes = try values.decode([MyCustomType?].self, forKey: .myCustomTypes)'); + expect(result).toContain('scalarArray = try values.decode([String?].self, forKey: .scalarArray)'); + expect(result).toMatchSnapshot(); }); test('decode optional list with required elements', () => { const schema = /* GraphQL */ ` @@ -3219,7 +3222,10 @@ describe('AppSyncSwiftVisitor', () => { context: String } `; - expect(getVisitorPipelinedTransformer(schema, 'MyModel', CodeGenGenerateEnum.code, {}).generate()).toMatchSnapshot(); + const result = getVisitorPipelinedTransformer(schema, 'MyModel', CodeGenGenerateEnum.code, {}).generate(); + expect(result).toContain('myCustomTypes = try? values.decode([MyCustomType].self, forKey: .myCustomTypes)'); + expect(result).toContain('scalarArray = try? values.decode([String].self, forKey: .scalarArray)'); + expect(result).toMatchSnapshot(); }); test('decode required list with required elements', () => { const schema = /* GraphQL */ ` @@ -3243,7 +3249,10 @@ describe('AppSyncSwiftVisitor', () => { context: String } `; - expect(getVisitorPipelinedTransformer(schema, 'MyModel', CodeGenGenerateEnum.code, {}).generate()).toMatchSnapshot(); + const result = getVisitorPipelinedTransformer(schema, 'MyModel', CodeGenGenerateEnum.code, {}).generate(); + expect(result).toContain('myCustomTypes = try values.decode([MyCustomType].self, forKey: .myCustomTypes)'); + expect(result).toContain('scalarArray = try values.decode([String].self, forKey: .scalarArray)'); + expect(result).toMatchSnapshot(); }); test('decode optional list with nullable elements', () => { @@ -3268,7 +3277,10 @@ describe('AppSyncSwiftVisitor', () => { context: String } `; - expect(getVisitorPipelinedTransformer(schema, 'MyModel', CodeGenGenerateEnum.code, {}).generate()).toMatchSnapshot(); + const result = getVisitorPipelinedTransformer(schema, 'MyModel', CodeGenGenerateEnum.code, {}).generate(); + expect(result).toContain('myCustomTypes = try? values.decode([MyCustomType?].self, forKey: .myCustomTypes)'); + expect(result).toContain('scalarArray = try? values.decode([String?].self, forKey: .scalarArray)'); + expect(result).toMatchSnapshot(); }); }); });