Skip to content

Conversation

@seang96
Copy link
Contributor

@seang96 seang96 commented Oct 5, 2020

Fix types regarding nullable lists

Issue #, if available: Mentioned in PR #5327

Description of changes:
Fixes an issue with typing with newly added nullable array support for JavaScript and Typescript. Thank you @danrivett for discovering it.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Fix types regarding nullable lists
export declare class SimpleNonModelType {
readonly id: string;
readonly names?: string[];
readonly names?: (string | null)[];

Choose a reason for hiding this comment

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

I don't know if there's a unit test to make sure that:

foo: [Bar!]

Is generated correctly?

As in that case we would want the following generated since the elements themselves should be non-null, just the array nullable:

readyonly foo?: Bar[];

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@danrivett, I added a new Bar type to the tests and included it in the SimpleModel to cover this case.

@codecov
Copy link

codecov bot commented Oct 5, 2020

Codecov Report

Merging #5493 into master will increase coverage by 0.08%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5493      +/-   ##
==========================================
+ Coverage   59.08%   59.16%   +0.08%     
==========================================
  Files         402      402              
  Lines       18043    18072      +29     
  Branches     3388     3395       +7     
==========================================
+ Hits        10661    10693      +32     
+ Misses       6743     6741       -2     
+ Partials      639      638       -1     
Impacted Files Coverage Δ
...-plugin/src/visitors/appsync-typescript-visitor.ts 69.73% <100.00%> (+9.73%) ⬆️
...gen-appsync-model-plugin/src/utils/process-auth.ts 98.21% <0.00%> (-1.79%) ⬇️
...psync-model-plugin/src/visitors/appsync-visitor.ts 95.67% <0.00%> (+0.46%) ⬆️
...ugin/src/visitors/appsync-json-metadata-visitor.ts 95.89% <0.00%> (+0.57%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 952a92e...3250fdc. Read the comment docs.

Added test for nullable array having non-null types
protected getListType(typeStr: string, field: CodeGenField): string {
return `${typeStr}[]`;
let type: string = typeStr;
if (field.isList && field.isNullable) {

Choose a reason for hiding this comment

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

I'm not familiar with the codegen code so treat accordingly, but would:

Suggested change
if (field.isList && field.isNullable) {
if (field.isNullable) {

Be sufficient here?

Do we need to check the isList type since this function is always returning an array type regardless (line 223) and so it seems this if-block only cares if the list element type is nullable or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I imagine just isNullable is sufficient, I was originally placing this code elsewhere which actually needed it until I found getListType. I am also learning the library haha

Copy link

@danrivett danrivett left a comment

Choose a reason for hiding this comment

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

LGTM (as a layperson here at least).

@danrivett
Copy link

@yuth If you can review when you're available, since you reviewed the previous related PRs, it would be much appreciated. Thanks.

let ownerFieldName = getOwnerFieldName(rule);
if (ownerFieldName !== undefined && !hasOwnerField(modelObj.fields, ownerFieldName)) {
modelDeclarations.addProperty(ownerFieldName, "String", undefined, 'DEFAULT', {
modelDeclarations.addProperty(ownerFieldName, 'String', undefined, 'DEFAULT', {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is where the merging conflict is. I removed this part in my previous merged PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AaronZyLee , I did not notice the merge conflict... My editor must've refactored to single quotes. I am pushing the merge that resolves the conflict now.

@danrivett
Copy link

@yuth pinging again as this fixes a regression introduced in version 4.29.4 and until it's merged I cannot upgrade from 4.29.3.

Copy link
Contributor

@yuth yuth left a comment

Choose a reason for hiding this comment

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

Thank you for the PR

@yuth yuth merged commit 8b5043c into aws-amplify:master Oct 19, 2020
@seang96 seang96 deleted the fix-nullable-list branch October 19, 2020 20:14
r0zar pushed a commit to r0zar/amplify-cli that referenced this pull request Nov 19, 2020
…r list (aws-amplify#5493)

Fix types regarding nullable lists
Added test for nullable array having non-null types
Removed redundant isList check

Co-authored-by: Sean Greenawalt <sgreenawalt@dasherlawless.com>
@github-actions
Copy link

This pull request has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants