From 0e9952dc11f70b1bb89c28201ddd966d0c1bb832 Mon Sep 17 00:00:00 2001 From: David Lopez Date: Fri, 8 Dec 2023 12:50:48 -0800 Subject: [PATCH] fix: check if import name is a primitive and needs aliasing --- .../__utils__/amplify-renderer-generator.ts | 3 +- .../studio-ui-codegen-react-forms.test.ts | 12 +++ .../lib/imports/import-collection.ts | 7 +- .../example-schemas/datastore/games.json | 101 ++++++++++++++++++ .../example-schemas/forms/games.json | 16 +++ 5 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 packages/codegen-ui/example-schemas/datastore/games.json create mode 100644 packages/codegen-ui/example-schemas/forms/games.json diff --git a/packages/codegen-ui-react/lib/__tests__/__utils__/amplify-renderer-generator.ts b/packages/codegen-ui-react/lib/__tests__/__utils__/amplify-renderer-generator.ts index 416e3397..a55623e8 100644 --- a/packages/codegen-ui-react/lib/__tests__/__utils__/amplify-renderer-generator.ts +++ b/packages/codegen-ui-react/lib/__tests__/__utils__/amplify-renderer-generator.ts @@ -77,6 +77,7 @@ export const generateComponentOnlyWithAmplifyFormRenderer = ( formJsonFile: string, dataSchemaJsonFile: string | undefined, renderConfig: ReactRenderConfig = defaultCLIRenderConfig, + featureFlags?: FormFeatureFlags, ) => { let dataSchema: GenericDataSchema | undefined; if (dataSchemaJsonFile) { @@ -84,7 +85,7 @@ export const generateComponentOnlyWithAmplifyFormRenderer = ( dataSchema = getGenericFromDataStore(dataStoreSchema); } const rendererFactory = new StudioTemplateRendererFactory( - (component: StudioForm) => new AmplifyFormRenderer(component, dataSchema, renderConfig), + (component: StudioForm) => new AmplifyFormRenderer(component, dataSchema, renderConfig, featureFlags), ); const renderer = rendererFactory.buildRenderer(loadSchemaFromJSONFile(formJsonFile)); diff --git a/packages/codegen-ui-react/lib/__tests__/studio-ui-codegen-react-forms.test.ts b/packages/codegen-ui-react/lib/__tests__/studio-ui-codegen-react-forms.test.ts index 11401015..e5210341 100644 --- a/packages/codegen-ui-react/lib/__tests__/studio-ui-codegen-react-forms.test.ts +++ b/packages/codegen-ui-react/lib/__tests__/studio-ui-codegen-react-forms.test.ts @@ -383,6 +383,18 @@ describe('amplify form renderer tests', () => { expect(importCollection).toBeDefined(); }); + it('should contain Text as alias map because its a primitive component name', () => { + const { importCollection } = generateComponentOnlyWithAmplifyFormRenderer( + 'forms/games', + 'datastore/games', + { ...defaultCLIRenderConfig, dependencies: { 'aws-amplify': '^6.0.0' } }, + { isNonModelSupported: true, isRelationshipSupported: true }, + ); + + const aliasMap = importCollection.getAliasMap(); + expect(aliasMap.model.Text).toBe('Text0'); + }); + it('should 1:1 relationships without types file path - Create amplify js v6', () => { const { componentText, declaration } = generateWithAmplifyFormRenderer( 'forms/owner-dog-create', diff --git a/packages/codegen-ui-react/lib/imports/import-collection.ts b/packages/codegen-ui-react/lib/imports/import-collection.ts index 6f6620e7..f005c89c 100644 --- a/packages/codegen-ui-react/lib/imports/import-collection.ts +++ b/packages/codegen-ui-react/lib/imports/import-collection.ts @@ -113,7 +113,12 @@ export class ImportCollection { const modelAlias = createUniqueName( importName, (input) => - this.importedNames.has(input) || (input.endsWith('Props') && isPrimitive(input.replace(/Props/g, ''))), + // Testing if the name is not unique. + // Props is for testing the primitive types e.g. "TextProps". + // And test for a matching primitive name value e.g. "Text" + this.importedNames.has(input) || + (input.endsWith('Props') && isPrimitive(input.replace(/Props/g, ''))) || + isPrimitive(input), ); if (existingPackageAlias) { existingPackageAlias.set(importName, modelAlias); diff --git a/packages/codegen-ui/example-schemas/datastore/games.json b/packages/codegen-ui/example-schemas/datastore/games.json new file mode 100644 index 00000000..5fe6ec91 --- /dev/null +++ b/packages/codegen-ui/example-schemas/datastore/games.json @@ -0,0 +1,101 @@ +{ + "models": { + "Text": { + "name": "Text", + "fields": { + "id": { + "name": "id", + "isArray": false, + "type": "ID", + "isRequired": true, + "attributes": [] + }, + "message": { + "name": "message", + "isArray": false, + "type": "String", + "isRequired": false, + "attributes": [] + }, + "createdAt": { + "name": "createdAt", + "isArray": false, + "type": "AWSDateTime", + "isRequired": false, + "attributes": [], + "isReadOnly": true + }, + "updatedAt": { + "name": "updatedAt", + "isArray": false, + "type": "AWSDateTime", + "isRequired": false, + "attributes": [], + "isReadOnly": true + } + }, + "syncable": true, + "pluralName": "Texts", + "attributes": [ + { + "type": "model", + "properties": {} + } + ] + }, + "Games": { + "name": "Games", + "fields": { + "id": { + "name": "id", + "isArray": false, + "type": "ID", + "isRequired": true, + "attributes": [] + }, + "Texts": { + "name": "Texts", + "isArray": true, + "type": { + "model": "Text" + }, + "isRequired": false, + "attributes": [], + "isArrayNullable": true, + "association": { + "connectionType": "HAS_MANY", + "associatedWith": "gamesID" + } + }, + "createdAt": { + "name": "createdAt", + "isArray": false, + "type": "AWSDateTime", + "isRequired": false, + "attributes": [], + "isReadOnly": true + }, + "updatedAt": { + "name": "updatedAt", + "isArray": false, + "type": "AWSDateTime", + "isRequired": false, + "attributes": [], + "isReadOnly": true + } + }, + "syncable": true, + "pluralName": "Games", + "attributes": [ + { + "type": "model", + "properties": {} + } + ] + } + }, + "enums": {}, + "nonModels": {}, + "codegenVersion": "3.4.4", + "version": "4515c51947749e6e168199ab64837ed3" +} \ No newline at end of file diff --git a/packages/codegen-ui/example-schemas/forms/games.json b/packages/codegen-ui/example-schemas/forms/games.json new file mode 100644 index 00000000..159e6ae7 --- /dev/null +++ b/packages/codegen-ui/example-schemas/forms/games.json @@ -0,0 +1,16 @@ +{ + "name": "GamesCreateForm", + "formActionType": "create", + "dataType": { + "dataSourceType": "DataStore", + "dataTypeName": "Games" + }, + "fields": {}, + "sectionalElements": {}, + "style": {}, + "cta": { + "clear": {}, + "cancel": {}, + "submit": {} + } +} \ No newline at end of file