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

fix: check if import name is a primitive and needs aliasing #1142

Merged
merged 1 commit into from
Dec 8, 2023
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 @@ -77,14 +77,15 @@ export const generateComponentOnlyWithAmplifyFormRenderer = (
formJsonFile: string,
dataSchemaJsonFile: string | undefined,
renderConfig: ReactRenderConfig = defaultCLIRenderConfig,
featureFlags?: FormFeatureFlags,
) => {
let dataSchema: GenericDataSchema | undefined;
if (dataSchemaJsonFile) {
const dataStoreSchema = loadSchemaFromJSONFile<DataStoreSchema | ModelIntrospectionSchema>(dataSchemaJsonFile);
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<StudioForm>(formJsonFile));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
101 changes: 101 additions & 0 deletions packages/codegen-ui/example-schemas/datastore/games.json
Original file line number Diff line number Diff line change
@@ -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"
}
16 changes: 16 additions & 0 deletions packages/codegen-ui/example-schemas/forms/games.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "GamesCreateForm",
"formActionType": "create",
"dataType": {
"dataSourceType": "DataStore",
"dataTypeName": "Games"
},
"fields": {},
"sectionalElements": {},
"style": {},
"cta": {
"clear": {},
"cancel": {},
"submit": {}
}
}
Loading