Skip to content

Commit f9dd802

Browse files
letsbelopezDavid Lopez
andauthored
fix: check if import name is a primitive and needs aliasing (#1142)
Co-authored-by: David Lopez <[email protected]>
1 parent e2e5f41 commit f9dd802

File tree

5 files changed

+137
-2
lines changed

5 files changed

+137
-2
lines changed

packages/codegen-ui-react/lib/__tests__/__utils__/amplify-renderer-generator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,15 @@ export const generateComponentOnlyWithAmplifyFormRenderer = (
7777
formJsonFile: string,
7878
dataSchemaJsonFile: string | undefined,
7979
renderConfig: ReactRenderConfig = defaultCLIRenderConfig,
80+
featureFlags?: FormFeatureFlags,
8081
) => {
8182
let dataSchema: GenericDataSchema | undefined;
8283
if (dataSchemaJsonFile) {
8384
const dataStoreSchema = loadSchemaFromJSONFile<DataStoreSchema | ModelIntrospectionSchema>(dataSchemaJsonFile);
8485
dataSchema = getGenericFromDataStore(dataStoreSchema);
8586
}
8687
const rendererFactory = new StudioTemplateRendererFactory(
87-
(component: StudioForm) => new AmplifyFormRenderer(component, dataSchema, renderConfig),
88+
(component: StudioForm) => new AmplifyFormRenderer(component, dataSchema, renderConfig, featureFlags),
8889
);
8990

9091
const renderer = rendererFactory.buildRenderer(loadSchemaFromJSONFile<StudioForm>(formJsonFile));

packages/codegen-ui-react/lib/__tests__/studio-ui-codegen-react-forms.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,18 @@ describe('amplify form renderer tests', () => {
383383
expect(importCollection).toBeDefined();
384384
});
385385

386+
it('should contain Text as alias map because its a primitive component name', () => {
387+
const { importCollection } = generateComponentOnlyWithAmplifyFormRenderer(
388+
'forms/games',
389+
'datastore/games',
390+
{ ...defaultCLIRenderConfig, dependencies: { 'aws-amplify': '^6.0.0' } },
391+
{ isNonModelSupported: true, isRelationshipSupported: true },
392+
);
393+
394+
const aliasMap = importCollection.getAliasMap();
395+
expect(aliasMap.model.Text).toBe('Text0');
396+
});
397+
386398
it('should 1:1 relationships without types file path - Create amplify js v6', () => {
387399
const { componentText, declaration } = generateWithAmplifyFormRenderer(
388400
'forms/owner-dog-create',

packages/codegen-ui-react/lib/imports/import-collection.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,12 @@ export class ImportCollection {
113113
const modelAlias = createUniqueName(
114114
importName,
115115
(input) =>
116-
this.importedNames.has(input) || (input.endsWith('Props') && isPrimitive(input.replace(/Props/g, ''))),
116+
// Testing if the name is not unique.
117+
// Props is for testing the primitive types e.g. "TextProps".
118+
// And test for a matching primitive name value e.g. "Text"
119+
this.importedNames.has(input) ||
120+
(input.endsWith('Props') && isPrimitive(input.replace(/Props/g, ''))) ||
121+
isPrimitive(input),
117122
);
118123
if (existingPackageAlias) {
119124
existingPackageAlias.set(importName, modelAlias);
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"models": {
3+
"Text": {
4+
"name": "Text",
5+
"fields": {
6+
"id": {
7+
"name": "id",
8+
"isArray": false,
9+
"type": "ID",
10+
"isRequired": true,
11+
"attributes": []
12+
},
13+
"message": {
14+
"name": "message",
15+
"isArray": false,
16+
"type": "String",
17+
"isRequired": false,
18+
"attributes": []
19+
},
20+
"createdAt": {
21+
"name": "createdAt",
22+
"isArray": false,
23+
"type": "AWSDateTime",
24+
"isRequired": false,
25+
"attributes": [],
26+
"isReadOnly": true
27+
},
28+
"updatedAt": {
29+
"name": "updatedAt",
30+
"isArray": false,
31+
"type": "AWSDateTime",
32+
"isRequired": false,
33+
"attributes": [],
34+
"isReadOnly": true
35+
}
36+
},
37+
"syncable": true,
38+
"pluralName": "Texts",
39+
"attributes": [
40+
{
41+
"type": "model",
42+
"properties": {}
43+
}
44+
]
45+
},
46+
"Games": {
47+
"name": "Games",
48+
"fields": {
49+
"id": {
50+
"name": "id",
51+
"isArray": false,
52+
"type": "ID",
53+
"isRequired": true,
54+
"attributes": []
55+
},
56+
"Texts": {
57+
"name": "Texts",
58+
"isArray": true,
59+
"type": {
60+
"model": "Text"
61+
},
62+
"isRequired": false,
63+
"attributes": [],
64+
"isArrayNullable": true,
65+
"association": {
66+
"connectionType": "HAS_MANY",
67+
"associatedWith": "gamesID"
68+
}
69+
},
70+
"createdAt": {
71+
"name": "createdAt",
72+
"isArray": false,
73+
"type": "AWSDateTime",
74+
"isRequired": false,
75+
"attributes": [],
76+
"isReadOnly": true
77+
},
78+
"updatedAt": {
79+
"name": "updatedAt",
80+
"isArray": false,
81+
"type": "AWSDateTime",
82+
"isRequired": false,
83+
"attributes": [],
84+
"isReadOnly": true
85+
}
86+
},
87+
"syncable": true,
88+
"pluralName": "Games",
89+
"attributes": [
90+
{
91+
"type": "model",
92+
"properties": {}
93+
}
94+
]
95+
}
96+
},
97+
"enums": {},
98+
"nonModels": {},
99+
"codegenVersion": "3.4.4",
100+
"version": "4515c51947749e6e168199ab64837ed3"
101+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "GamesCreateForm",
3+
"formActionType": "create",
4+
"dataType": {
5+
"dataSourceType": "DataStore",
6+
"dataTypeName": "Games"
7+
},
8+
"fields": {},
9+
"sectionalElements": {},
10+
"style": {},
11+
"cta": {
12+
"clear": {},
13+
"cancel": {},
14+
"submit": {}
15+
}
16+
}

0 commit comments

Comments
 (0)