Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('Javascript visitor', () => {
id: ID!
name: String
bar: String
foo: [Bar!] @connection
}
enum SimpleEnum {
enumVal1
Expand All @@ -36,6 +37,10 @@ describe('Javascript visitor', () => {
id: ID!
names: [String]
}

type Bar @model {
id: ID!
}
`;
let visitor: AppSyncModelJavascriptVisitor;
beforeEach(() => {
Expand Down Expand Up @@ -100,16 +105,24 @@ export enum SimpleEnum {

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.

constructor(init: ModelInit<SimpleNonModelType>);
}

export declare class SimpleModel {
readonly id: string;
readonly name?: string;
readonly bar?: string;
readonly foo?: Bar[];
constructor(init: ModelInit<SimpleModel>);
static copyOf(source: SimpleModel, mutator: (draft: MutableModel<SimpleModel>) => MutableModel<SimpleModel> | void): SimpleModel;
}

export declare class Bar {
readonly id: string;
readonly simpleModelFooId?: string;
constructor(init: ModelInit<Bar>);
static copyOf(source: Bar, mutator: (draft: MutableModel<Bar>) => MutableModel<Bar> | void): Bar;
}"
`);
expect(generateImportSpy).toBeCalledTimes(1);
Expand All @@ -118,9 +131,10 @@ export declare class SimpleModel {
expect(generateEnumDeclarationsSpy).toBeCalledTimes(1);
expect(generateEnumDeclarationsSpy).toBeCalledWith((declarationVisitor as any).enumMap['SimpleEnum'], true);

expect(generateModelDeclarationSpy).toBeCalledTimes(2);
expect(generateModelDeclarationSpy).toBeCalledTimes(3);
expect(generateModelDeclarationSpy).toHaveBeenNthCalledWith(1, (declarationVisitor as any).modelMap['SimpleModel'], true);
expect(generateModelDeclarationSpy).toHaveBeenNthCalledWith(2, (declarationVisitor as any).nonModelMap['SimpleNonModelType'], true);
expect(generateModelDeclarationSpy).toHaveBeenNthCalledWith(2, (declarationVisitor as any).modelMap['Bar'], true);
expect(generateModelDeclarationSpy).toHaveBeenNthCalledWith(3, (declarationVisitor as any).nonModelMap['SimpleNonModelType'], true);
});
});

Expand All @@ -141,10 +155,11 @@ const SimpleEnum = {
\\"ENUM_VAL2\\": \\"enumVal2\\"
};

const { SimpleModel, SimpleNonModelType } = initSchema(schema);
const { SimpleModel, Bar, SimpleNonModelType } = initSchema(schema);

export {
SimpleModel,
Bar,
SimpleEnum,
SimpleNonModelType
};"
Expand All @@ -156,7 +171,11 @@ export {

expect(generateModelInitializationSpy).toHaveBeenCalledTimes(1);
expect(generateModelInitializationSpy).toHaveBeenCalledWith(
[(jsVisitor as any).modelMap['SimpleModel'], (jsVisitor as any).nonModelMap['SimpleNonModelType']],
[
(jsVisitor as any).modelMap['SimpleModel'],
(jsVisitor as any).modelMap['Bar'],
(jsVisitor as any).nonModelMap['SimpleNonModelType'],
],
false,
);
});
Expand Down Expand Up @@ -206,7 +225,7 @@ export enum SimpleEnum {

export declare class SimpleNonModelType {
readonly id: string;
readonly names?: string[];
readonly names?: (string | null)[];
constructor(init: ModelInit<SimpleNonModelType>);
}

Expand Down Expand Up @@ -276,7 +295,7 @@ export enum SimpleEnum {

export declare class SimpleNonModelType {
readonly id: string;
readonly names?: string[];
readonly names?: (string | null)[];
constructor(init: ModelInit<SimpleNonModelType>);
}

Expand Down Expand Up @@ -348,7 +367,7 @@ export enum SimpleEnum {

export declare class SimpleNonModelType {
readonly id: string;
readonly names?: string[];
readonly names?: (string | null)[];
constructor(init: ModelInit<SimpleNonModelType>);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class AppSyncModelTypeScriptVisitor<
modelObj.fields.forEach((field: CodeGenField) => {
modelDeclarations.addProperty(this.getFieldName(field), this.getNativeType(field), undefined, 'DEFAULT', {
readonly: true,
optional: field.isNullable,
optional: field.isList ? field.isListNullable : field.isNullable,
});
});

Expand Down Expand Up @@ -202,7 +202,11 @@ export class AppSyncModelTypeScriptVisitor<
}

protected getListType(typeStr: string, field: CodeGenField): string {
return `${typeStr}[]`;
let type: string = typeStr;
if (field.isNullable) {
type = `(${type} | null)`;
}
return `${type}[]`;
}

protected getNativeType(field: CodeGenField): string {
Expand Down
7 changes: 2 additions & 5 deletions packages/amplify-e2e-tests/src/__tests__/function_3.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { initJSProjectWithProfile, deleteProject, amplifyPushAuth } from 'amplify-e2e-core';
import { addFunction, functionMockAssert, functionCloudInvoke } from 'amplify-e2e-core';
import {
createNewProjectDir,
deleteProjectDir,
} from 'amplify-e2e-core';
import { createNewProjectDir, deleteProjectDir } from 'amplify-e2e-core';

describe('go function tests', () => {
const helloWorldSuccessOutput = 'Hello Amplify!';
Expand Down Expand Up @@ -137,4 +134,4 @@ describe('dotnet function tests', () => {
const response = await functionCloudInvoke(projRoot, { friendlyName, funcName, payload });
expect(JSON.parse(response.Payload.toString())).toEqual(JSON.parse(helloWorldSuccessOutput));
});
});
});