From e612d3021dc28e169ea59d8a374230a32746f0c2 Mon Sep 17 00:00:00 2001 From: Elijah Quartey Date: Mon, 22 Apr 2024 10:24:32 -0600 Subject: [PATCH] refactor(amplify-codegen): refactor decoding logic to check types instead of value --- .../appsync-dart-visitor.test.ts.snap | 120 +++++++++++------- .../appsync-dart-visitor.test.ts.snap | 70 ++++++---- .../src/visitors/appsync-dart-visitor.ts | 10 +- .../__snapshots__/models.test.ts.snap | 20 +-- 4 files changed, 132 insertions(+), 88 deletions(-) diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-dart-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-dart-visitor.test.ts.snap index 0c4559b8e..836e494ae 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-dart-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/__snapshots__/appsync-dart-visitor.test.ts.snap @@ -160,17 +160,19 @@ class Post extends amplify_core.Model { _title = json['title'], _rating = (json['rating'] as num?)?.toInt(), _status = amplify_core.enumFromString(json['status'], PostStatus.values), - _comments = json['comments'] != null - ? json['comments'] is Map + _comments = json['comments'] is Map + ? (json['comments']['items'] is List ? (json['comments']['items'] as List) .where((e) => e != null) .map((e) => Comment.fromJson(new Map.from(e))) .toList() - : (json['comments'] as List) + : null) + : (json['comments'] is List + ? (json['comments'] as List) .where((e) => e?['serializedData'] != null) .map((e) => Comment.fromJson(new Map.from(e?['serializedData']))) .toList() - : null; + : null); Map toJson() => { 'id': id, 'title': _title, 'rating': _rating, 'status': amplify_core.enumToString(_status), 'comments': _comments?.map((Comment? e) => e?.toJson()).toList() @@ -560,17 +562,19 @@ class Post extends amplify_core.Model { _title = json['title'], _rating = (json['rating'] as num?)?.toInt(), _status = amplify_core.enumFromString(json['status'], PostStatus.values), - _comments = json['comments'] != null - ? json['comments'] is Map + _comments = json['comments'] is Map + ? (json['comments']['items'] is List ? (json['comments']['items'] as List) .where((e) => e != null) .map((e) => Comment.fromJson(new Map.from(e))) .toList() - : (json['comments'] as List) + : null) + : (json['comments'] is List + ? (json['comments'] as List) .where((e) => e?['serializedData'] != null) .map((e) => Comment.fromJson(new Map.from(e?['serializedData']))) .toList() - : null; + : null); Map toJson() => { 'id': id, 'title': _title, 'rating': _rating, 'status': amplify_core.enumToString(_status), 'comments': _comments?.map((Comment? e) => e?.toJson()).toList() @@ -2690,17 +2694,19 @@ class Post extends amplify_core.Model { : id = json['id'], _title = json['title'], _content = json['content'], - _tags = json['tags'] != null - ? json['tags'] is Map + _tags = json['tags'] is Map + ? (json['tags']['items'] is List ? (json['tags']['items'] as List) .where((e) => e != null) .map((e) => PostTags.fromJson(new Map.from(e))) .toList() - : (json['tags'] as List) + : null) + : (json['tags'] is List + ? (json['tags'] as List) .where((e) => e?['serializedData'] != null) .map((e) => PostTags.fromJson(new Map.from(e?['serializedData']))) .toList() - : null; + : null); Map toJson() => { 'id': id, 'title': _title, 'content': _content, 'tags': _tags?.map((PostTags? e) => e?.toJson()).toList() @@ -2851,17 +2857,19 @@ class Tag extends amplify_core.Model { Tag.fromJson(Map json) : id = json['id'], _label = json['label'], - _posts = json['posts'] != null - ? json['posts'] is Map + _posts = json['posts'] is Map + ? (json['posts']['items'] is List ? (json['posts']['items'] as List) .where((e) => e != null) .map((e) => PostTags.fromJson(new Map.from(e))) .toList() - : (json['posts'] as List) + : null) + : (json['posts'] is List + ? (json['posts'] as List) .where((e) => e?['serializedData'] != null) .map((e) => PostTags.fromJson(new Map.from(e?['serializedData']))) .toList() - : null; + : null); Map toJson() => { 'id': id, 'label': _label, 'posts': _posts?.map((PostTags? e) => e?.toJson()).toList() @@ -5481,17 +5489,19 @@ class Todo extends amplify_core.Model { Todo.fromJson(Map json) : id = json['id'], - _tasks = json['tasks'] != null - ? json['tasks'] is Map + _tasks = json['tasks'] is Map + ? (json['tasks']['items'] is List ? (json['tasks']['items'] as List) .where((e) => e != null) .map((e) => Task.fromJson(new Map.from(e))) .toList() - : (json['tasks'] as List) + : null) + : (json['tasks'] is List + ? (json['tasks'] as List) .where((e) => e?['serializedData'] != null) .map((e) => Task.fromJson(new Map.from(e?['serializedData']))) .toList() - : null; + : null); Map toJson() => { 'id': id, 'tasks': _tasks?.map((Task? e) => e?.toJson()).toList() @@ -5811,17 +5821,19 @@ class Blog extends amplify_core.Model { Blog.fromJson(Map json) : id = json['id'], _name = json['name'], - _posts = json['posts'] != null - ? json['posts'] is Map + _posts = json['posts'] is Map + ? (json['posts']['items'] is List ? (json['posts']['items'] as List) .where((e) => e != null) .map((e) => Post.fromJson(new Map.from(e))) .toList() - : (json['posts'] as List) + : null) + : (json['posts'] is List + ? (json['posts'] as List) .where((e) => e?['serializedData'] != null) .map((e) => Post.fromJson(new Map.from(e?['serializedData']))) .toList() - : null, + : null), _test = json['test']?.cast(); Map toJson() => { @@ -6197,17 +6209,19 @@ class Post extends amplify_core.Model { ? Blog.fromJson(new Map.from(json['blog']['serializedData'])) : Blog.fromJson(new Map.from(json['blog'])) : null, - _comments = json['comments'] != null - ? json['comments'] is Map + _comments = json['comments'] is Map + ? (json['comments']['items'] is List ? (json['comments']['items'] as List) .where((e) => e != null) .map((e) => Comment.fromJson(new Map.from(e))) .toList() - : (json['comments'] as List) + : null) + : (json['comments'] is List + ? (json['comments'] as List) .where((e) => e?['serializedData'] != null) .map((e) => Comment.fromJson(new Map.from(e?['serializedData']))) .toList() - : null; + : null); Map toJson() => { 'id': id, 'title': _title, 'blog': _blog?.toJson(), 'comments': _comments?.map((Comment? e) => e?.toJson()).toList() @@ -6734,17 +6748,19 @@ class Blog extends amplify_core.Model { Blog.fromJson(Map json) : id = json['id'], _name = json['name'], - _posts = json['posts'] != null - ? json['posts'] is Map + _posts = json['posts'] is Map + ? (json['posts']['items'] is List ? (json['posts']['items'] as List) .where((e) => e != null) .map((e) => Post.fromJson(new Map.from(e))) .toList() - : (json['posts'] as List) + : null) + : (json['posts'] is List + ? (json['posts'] as List) .where((e) => e?['serializedData'] != null) .map((e) => Post.fromJson(new Map.from(e?['serializedData']))) .toList() - : null; + : null); Map toJson() => { 'id': id, 'name': _name, 'posts': _posts?.map((Post? e) => e?.toJson()).toList() @@ -7110,17 +7126,19 @@ class Post extends amplify_core.Model { ? Blog.fromJson(new Map.from(json['blog']['serializedData'])) : Blog.fromJson(new Map.from(json['blog'])) : null, - _comments = json['comments'] != null - ? json['comments'] is Map + _comments = json['comments'] is Map + ? (json['comments']['items'] is List ? (json['comments']['items'] as List) .where((e) => e != null) .map((e) => Comment.fromJson(new Map.from(e))) .toList() - : (json['comments'] as List) + : null) + : (json['comments'] is List + ? (json['comments'] as List) .where((e) => e?['serializedData'] != null) .map((e) => Comment.fromJson(new Map.from(e?['serializedData']))) .toList() - : null; + : null); Map toJson() => { 'id': id, 'title': _title, 'blog': _blog?.toJson(), 'comments': _comments?.map((Comment? e) => e?.toJson()).toList() @@ -7295,28 +7313,32 @@ class TestModel extends amplify_core.Model { TestModel.fromJson(Map json) : id = json['id'], - _listOfModels = json['listOfModels'] != null - ? json['listOfModels'] is Map + _listOfModels = json['listOfModels'] is Map + ? (json['listOfModels']['items'] is List ? (json['listOfModels']['items'] as List) .where((e) => e != null) .map((e) => ListItem.fromJson(new Map.from(e))) .toList() - : (json['listOfModels'] as List) + : null) + : (json['listOfModels'] is List + ? (json['listOfModels'] as List) .where((e) => e?['serializedData'] != null) .map((e) => ListItem.fromJson(new Map.from(e?['serializedData']))) .toList() - : null, - _nullableListOfModels = json['nullableListOfModels'] != null - ? json['nullableListOfModels'] is Map + : null), + _nullableListOfModels = json['nullableListOfModels'] is Map + ? (json['nullableListOfModels']['items'] is List ? (json['nullableListOfModels']['items'] as List) .where((e) => e != null) .map((e) => ListItem.fromJson(new Map.from(e))) .toList() - : (json['nullableListOfModels'] as List) + : null) + : (json['nullableListOfModels'] is List + ? (json['nullableListOfModels'] as List) .where((e) => e?['serializedData'] != null) .map((e) => ListItem.fromJson(new Map.from(e?['serializedData']))) .toList() - : null; + : null); Map toJson() => { 'id': id, 'listOfModels': _listOfModels?.map((ListItem? e) => e?.toJson()).toList(), 'nullableListOfModels': _nullableListOfModels?.map((ListItem? e) => e?.toJson()).toList() @@ -10052,17 +10074,19 @@ class Post extends amplify_core.Model { Post.fromJson(Map json) : id = json['id'], _title = json['title'], - _comments = json['comments'] != null - ? json['comments'] is Map + _comments = json['comments'] is Map + ? (json['comments']['items'] is List ? (json['comments']['items'] as List) .where((e) => e != null) .map((e) => Comment.fromJson(new Map.from(e))) .toList() - : (json['comments'] as List) + : null) + : (json['comments'] is List + ? (json['comments'] as List) .where((e) => e?['serializedData'] != null) .map((e) => Comment.fromJson(new Map.from(e?['serializedData']))) .toList() - : null, + : null), _createdAt = json['createdAt'] != null ? amplify_core.TemporalDateTime.fromString(json['createdAt']) : null, _updatedAt = json['updatedAt'] != null ? amplify_core.TemporalDateTime.fromString(json['updatedAt']) : null; diff --git a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-dart-visitor.test.ts.snap b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-dart-visitor.test.ts.snap index e89b5a911..4764b7771 100644 --- a/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-dart-visitor.test.ts.snap +++ b/packages/appsync-modelgen-plugin/src/__tests__/visitors/gqlv2-regression-tests/__snapshots__/appsync-dart-visitor.test.ts.snap @@ -118,17 +118,19 @@ class Post extends amplify_core.Model { Post.fromJson(Map json) : id = json['id'], _title = json['title'], - _comments = json['comments'] != null - ? json['comments'] is Map + _comments = json['comments'] is Map + ? (json['comments']['items'] is List ? (json['comments']['items'] as List) .where((e) => e != null) .map((e) => Comment.fromJson(new Map.from(e))) .toList() - : (json['comments'] as List) + : null) + : (json['comments'] is List + ? (json['comments'] as List) .where((e) => e?['serializedData'] != null) .map((e) => Comment.fromJson(new Map.from(e?['serializedData']))) .toList() - : null; + : null); Map toJson() => { 'id': id, 'title': _title, 'comments': _comments?.map((Comment? e) => e?.toJson()).toList() @@ -838,17 +840,19 @@ class Blog7V2 extends amplify_core.Model { Blog7V2.fromJson(Map json) : id = json['id'], _name = json['name'], - _posts = json['posts'] != null - ? json['posts'] is Map + _posts = json['posts'] is Map + ? (json['posts']['items'] is List ? (json['posts']['items'] as List) .where((e) => e != null) .map((e) => Post7V2.fromJson(new Map.from(e))) .toList() - : (json['posts'] as List) + : null) + : (json['posts'] is List + ? (json['posts'] as List) .where((e) => e?['serializedData'] != null) .map((e) => Post7V2.fromJson(new Map.from(e?['serializedData']))) .toList() - : null; + : null); Map toJson() => { 'id': id, 'name': _name, 'posts': _posts?.map((Post7V2? e) => e?.toJson()).toList() @@ -1035,17 +1039,19 @@ class Post7V2 extends amplify_core.Model { ? Blog7V2.fromJson(new Map.from(json['blog']['serializedData'])) : Blog7V2.fromJson(new Map.from(json['blog'])) : null, - _comments = json['comments'] != null - ? json['comments'] is Map + _comments = json['comments'] is Map + ? (json['comments']['items'] is List ? (json['comments']['items'] as List) .where((e) => e != null) .map((e) => Comment7V2.fromJson(new Map.from(e))) .toList() - : (json['comments'] as List) + : null) + : (json['comments'] is List + ? (json['comments'] as List) .where((e) => e?['serializedData'] != null) .map((e) => Comment7V2.fromJson(new Map.from(e?['serializedData']))) .toList() - : null; + : null); Map toJson() => { 'id': id, 'title': _title, 'blog': _blog?.toJson(), 'comments': _comments?.map((Comment7V2? e) => e?.toJson()).toList() @@ -1765,17 +1771,19 @@ class Post extends amplify_core.Model { : id = json['id'], _title = json['title'], _content = json['content'], - _tags = json['tags'] != null - ? json['tags'] is Map + _tags = json['tags'] is Map + ? (json['tags']['items'] is List ? (json['tags']['items'] as List) .where((e) => e != null) .map((e) => PostTags.fromJson(new Map.from(e))) .toList() - : (json['tags'] as List) + : null) + : (json['tags'] is List + ? (json['tags'] as List) .where((e) => e?['serializedData'] != null) .map((e) => PostTags.fromJson(new Map.from(e?['serializedData']))) .toList() - : null; + : null); Map toJson() => { 'id': id, 'title': _title, 'content': _content, 'tags': _tags?.map((PostTags? e) => e?.toJson()).toList() @@ -1954,17 +1962,19 @@ class Tag extends amplify_core.Model { Tag.fromJson(Map json) : id = json['id'], _label = json['label'], - _posts = json['posts'] != null - ? json['posts'] is Map + _posts = json['posts'] is Map + ? (json['posts']['items'] is List ? (json['posts']['items'] as List) .where((e) => e != null) .map((e) => PostTags.fromJson(new Map.from(e))) .toList() - : (json['posts'] as List) + : null) + : (json['posts'] is List + ? (json['posts'] as List) .where((e) => e?['serializedData'] != null) .map((e) => PostTags.fromJson(new Map.from(e?['serializedData']))) .toList() - : null; + : null); Map toJson() => { 'id': id, 'label': _label, 'posts': _posts?.map((PostTags? e) => e?.toJson()).toList() @@ -2274,17 +2284,19 @@ class Post2 extends amplify_core.Model { Post2.fromJson(Map json) : id = json['id'], _title = json['title'], - _comments = json['comments'] != null - ? json['comments'] is Map + _comments = json['comments'] is Map + ? (json['comments']['items'] is List ? (json['comments']['items'] as List) .where((e) => e != null) .map((e) => Comment2.fromJson(new Map.from(e))) .toList() - : (json['comments'] as List) + : null) + : (json['comments'] is List + ? (json['comments'] as List) .where((e) => e?['serializedData'] != null) .map((e) => Comment2.fromJson(new Map.from(e?['serializedData']))) .toList() - : null; + : null); Map toJson() => { 'id': id, 'title': _title, 'comments': _comments?.map((Comment2? e) => e?.toJson()).toList() @@ -2969,17 +2981,19 @@ class Post extends amplify_core.Model { Post.fromJson(Map json) : id = json['id'], _title = json['title'], - _comments = json['comments'] != null - ? json['comments'] is Map + _comments = json['comments'] is Map + ? (json['comments']['items'] is List ? (json['comments']['items'] as List) .where((e) => e != null) .map((e) => Comment.fromJson(new Map.from(e))) .toList() - : (json['comments'] as List) + : null) + : (json['comments'] is List + ? (json['comments'] as List) .where((e) => e?['serializedData'] != null) .map((e) => Comment.fromJson(new Map.from(e?['serializedData']))) .toList() - : null; + : null); Map toJson() => { 'id': id, 'title': _title, 'comments': _comments?.map((Comment? e) => e?.toJson()).toList() diff --git a/packages/appsync-modelgen-plugin/src/visitors/appsync-dart-visitor.ts b/packages/appsync-modelgen-plugin/src/visitors/appsync-dart-visitor.ts index 875f86eaa..11807a379 100644 --- a/packages/appsync-modelgen-plugin/src/visitors/appsync-dart-visitor.ts +++ b/packages/appsync-modelgen-plugin/src/visitors/appsync-dart-visitor.ts @@ -698,17 +698,19 @@ export class AppSyncModelDartVisitor< if (this.isModelType(field)) { if (field.isList) { return [ - `${fieldName} = json['${varName}'] != null`, - indent(`? json['${varName}'] is Map`), + `${fieldName} = json['${varName}'] is Map`, + indent(`? (json['${varName}']['items'] is List`), indent(`? (json['${varName}']['items'] as List)`, 2), indent(`.where((e) => e != null)`, 4), indent(`.map((e) => ${this.getNativeType({ ...field, isList: false })}.fromJson(new Map.from(e)))`, 4), indent(`.toList()`, 4), - indent(`: (json['${varName}'] as List)`), + indent(`: null)`, 2), + indent(`: (json['${varName}'] is List`), + indent(`? (json['${varName}'] as List)`, 2), indent(`.where((e) => e?['serializedData'] != null)`, 4), indent(`.map((e) => ${this.getNativeType({ ...field, isList: false })}.fromJson(new Map.from(e?['serializedData'])))`, 4), indent(`.toList()`, 4), - indent(`: null`), + indent(`: null)`, 2), ] .filter(e => e !== undefined) .join('\n'); diff --git a/packages/graphql-generator/src/__tests__/__snapshots__/models.test.ts.snap b/packages/graphql-generator/src/__tests__/__snapshots__/models.test.ts.snap index ff8d0dad1..703230ce5 100644 --- a/packages/graphql-generator/src/__tests__/__snapshots__/models.test.ts.snap +++ b/packages/graphql-generator/src/__tests__/__snapshots__/models.test.ts.snap @@ -166,17 +166,19 @@ class Blog extends amplify_core.Model { Blog.fromJson(Map json) : id = json['id'], _name = json['name'], - _posts = json['posts'] != null - ? json['posts'] is Map + _posts = json['posts'] is Map + ? (json['posts']['items'] is List ? (json['posts']['items'] as List) .where((e) => e != null) .map((e) => Post.fromJson(new Map.from(e))) .toList() - : (json['posts'] as List) + : null) + : (json['posts'] is List + ? (json['posts'] as List) .where((e) => e?['serializedData'] != null) .map((e) => Post.fromJson(new Map.from(e?['serializedData']))) .toList() - : null, + : null), _createdAt = json['createdAt'] != null ? amplify_core.TemporalDateTime.fromString(json['createdAt']) : null, _updatedAt = json['updatedAt'] != null ? amplify_core.TemporalDateTime.fromString(json['updatedAt']) : null; @@ -750,17 +752,19 @@ class Post extends amplify_core.Model { ? Blog.fromJson(new Map.from(json['blog']['serializedData'])) : Blog.fromJson(new Map.from(json['blog'])) : null, - _comments = json['comments'] != null - ? json['comments'] is Map + _comments = json['comments'] is Map + ? (json['comments']['items'] is List ? (json['comments']['items'] as List) .where((e) => e != null) .map((e) => Comment.fromJson(new Map.from(e))) .toList() - : (json['comments'] as List) + : null) + : (json['comments'] is List + ? (json['comments'] as List) .where((e) => e?['serializedData'] != null) .map((e) => Comment.fromJson(new Map.from(e?['serializedData']))) .toList() - : null, + : null), _createdAt = json['createdAt'] != null ? amplify_core.TemporalDateTime.fromString(json['createdAt']) : null, _updatedAt = json['updatedAt'] != null ? amplify_core.TemporalDateTime.fromString(json['updatedAt']) : null;