Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(api)!: Model .fromJson() Refactor #3032

Closed

Conversation

Equartey
Copy link
Member

Issue #, if available:
#816

Description of changes:
Flatens the nested data structure of serializedData across the method channel and the .fromJson() method found on codegen models. This PR is to be paired with changes in aws-amplify/amplify-codegen#593

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

packages/amplify_datastore/example/lib/main.dart Outdated Show resolved Hide resolved
@@ -5,11 +5,12 @@ library sample_app;

import 'dart:async';

import 'package:amplify_core/amplify_core.dart';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should import from amplify_flutter to ensure platform stuff can be correctly configured.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's required. amplify_core should only be imported directly in Dart-only environments.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't mean to include this file in this PR, will be removed 👍

Copy link
Contributor

@dnys1 dnys1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few comments - overall looking good

.map((e) => Post.fromJson(
Map<String, dynamic>.from(e['serializedData'])))
_posts = json['posts'] != null
? (json['posts']['items'] as List)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is items guaranteed to be there if posts is non-null?

You could add a safeguard if not:

Suggested change
? (json['posts']['items'] as List)
? (json['posts']['items'] as List? ?? const [])

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppSync will pass an empty items array when posts is specified in the document. When posts is not in the document and therefore the response too, decoding returns null for posts.

@@ -5,11 +5,12 @@ library sample_app;

import 'dart:async';

import 'package:amplify_core/amplify_core.dart';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's required. amplify_core should only be imported directly in Dart-only environments.

result[key] = [
"modelName": nextModelName,
"serializedData": try generateSerializedData(
let dataMap = try generateSerializedData(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment to Hui's. Instead of merging modelName into the model (which may conflict with already-present data), it may be better to return a wrapped value or use __typename instead which is part of the GraphQL spec.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was naming the field __modelName the resolution to this?

packages/amplify_datastore/pubspec.yaml Outdated Show resolved Hide resolved
@@ -15,10 +15,10 @@ import 'package:meta/meta.dart';

import 'ModelProvider.dart';

/** This is an auto generated class representing the Blog type in your schema. */
/// This is an auto generated class representing the Blog type in your schema.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are we testing with these "legacy" models?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is validating older versions of codegen that we still support since not all customers update their models/backends. @ragingsquirrel3 may have more context.

@Jordan-Nelson Jordan-Nelson changed the title chore(api): Model .fromJson() Refactor chore(api)!: Model .fromJson() Refactor May 23, 2023
Copy link
Member

@Jordan-Nelson Jordan-Nelson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a few questions, but overall lgtm.

@@ -180,7 +180,7 @@ name = value''';
});
test(r'Windows style line endings are supported.', () {
const configContents = r'''
[profile foo]
[profile foo]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentional?

@@ -75,7 +75,7 @@ struct FlutterSerializedModelData {
"postalCode": "94115"
]),
JSONValue.object([
"line1": "000 Somewhere close",
"line1": "111 Somewhere close",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

intentional?

{ "field": "created", "order": "descending" }
]
}
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the changes in this file?

result[key] = [
"modelName": nextModelName,
"serializedData": try generateSerializedData(
let dataMap = try generateSerializedData(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was naming the field __modelName the resolution to this?

@Equartey Equartey force-pushed the chore/api-from-json-refactor branch from 8e99a0c to 4c0d4ae Compare April 1, 2024 16:53
@Equartey
Copy link
Member Author

closing in favor of: #4665

@Equartey Equartey closed this Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants