Skip to content

Commit dc631a7

Browse files
committed
fix(amplify-codegen): fix for non-model decoding in flutter v1
aws-amplify/amplify-flutter#4872
1 parent 0f3bbe0 commit dc631a7

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-dart-visitor.test.ts.snap

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,12 +1142,14 @@ class Contact {
11421142
Contact.fromJson(Map<String, dynamic> json)
11431143
: _contactName = json['contactName'],
11441144
_phone = json['phone'] != null
1145-
? Phone.fromJson(new Map<String, dynamic>.from(json['phone']))
1145+
? json['phone']['serializedData'] != null
1146+
? Phone.fromJson(new Map<String, dynamic>.from(json['phone']['serializedData']))
1147+
: Phone.fromJson(new Map<String, dynamic>.from(json['phone']))
11461148
: null,
11471149
_mailingAddresses = json['mailingAddresses'] is List
11481150
? (json['mailingAddresses'] as List)
11491151
.where((e) => e != null)
1150-
.map((e) => Address.fromJson(new Map<String, dynamic>.from(e)))
1152+
.map((e) => Address.fromJson(new Map<String, dynamic>.from(e['serializedData'] ?? e)))
11511153
.toList()
11521154
: null;
11531155

@@ -1390,12 +1392,14 @@ class Person extends amplify_core.Model {
13901392
: id = json['id'],
13911393
_name = json['name'],
13921394
_phone = json['phone'] != null
1393-
? Phone.fromJson(new Map<String, dynamic>.from(json['phone']))
1395+
? json['phone']['serializedData'] != null
1396+
? Phone.fromJson(new Map<String, dynamic>.from(json['phone']['serializedData']))
1397+
: Phone.fromJson(new Map<String, dynamic>.from(json['phone']))
13941398
: null,
13951399
_mailingAddresses = json['mailingAddresses'] is List
13961400
? (json['mailingAddresses'] as List)
13971401
.where((e) => e != null)
1398-
.map((e) => Address.fromJson(new Map<String, dynamic>.from(e)))
1402+
.map((e) => Address.fromJson(new Map<String, dynamic>.from(e['serializedData'] ?? e)))
13991403
.toList()
14001404
: null;
14011405

packages/appsync-modelgen-plugin/src/visitors/appsync-dart-visitor.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,9 @@ export class AppSyncModelDartVisitor<
746746
`${fieldName} = json['${varName}'] is List`,
747747
indent(`? (json['${varName}'] as List)`),
748748
indent(`.where((e) => e != null)`, 2),
749-
indent(`.map((e) => ${this.getNativeType({ ...field, isList: false })}.fromJson(new Map<String, dynamic>.from(e)))`, 2),
749+
indent(`.map((e) => ${this.getNativeType({ ...field, isList: false })}.fromJson(new Map<String, dynamic>.from(${
750+
this.isNonModelType(field) ? "e['serializedData'] ?? e" : 'e'
751+
})))`, 2),
750752
indent(`.toList()`, 2),
751753
indent(`: null`),
752754
]
@@ -756,8 +758,10 @@ export class AppSyncModelDartVisitor<
756758
// single non-model i.e. embedded
757759
return [
758760
`${fieldName} = json['${varName}'] != null`,
759-
indent(`? ${this.getNativeType(field)}.fromJson(new Map<String, dynamic>.from(json['${varName}']))`),
760-
indent(`: null`),
761+
indent(`? json['${varName}']['serializedData'] != null`, 2),
762+
indent(`? ${this.getNativeType(field)}.fromJson(new Map<String, dynamic>.from(json['${varName}']['serializedData']))`, 4),
763+
indent(`: ${this.getNativeType(field)}.fromJson(new Map<String, dynamic>.from(json['${varName}']))`, 4),
764+
indent(`: null`,),
761765
].join('\n');
762766
}
763767
//regular type

0 commit comments

Comments
 (0)