Skip to content

fix(cli-internal): use consistent model name casing for DynamoDB table references - #14892

Merged
sharonyajain merged 7 commits into
devfrom
fix/non-pascal-case-model-14561
Aug 26, 2026
Merged

fix(cli-internal): use consistent model name casing for DynamoDB table references#14892
sharonyajain merged 7 commits into
devfrom
fix/non-pascal-case-model-14561

Conversation

@adrianjoshua-strutt

Copy link
Copy Markdown
Member

Description

When a GraphQL model uses non-PascalCase naming (e.g., randomItem), the generated code previously used naive capitalization to reconstruct the model name from the uppercase environment variable (API_MYAPI_RANDOMITEMTABLE_ARNRandomitem). This caused deployment failures because backend.data.resources.tables is keyed by the original model name from the schema.

Root Cause

The extractTableName function applied charAt(0).toUpperCase() + slice(1).toLowerCase() to the captured segment from the env var name. Since Gen1 uppercases model names in env vars, this transformation cannot reconstruct names like randomItem or MealPlan — it produces Randomitem and Mealplan instead.

Fix

The fix reads model names from the GraphQL schema (using the same regex pattern already used by DataGenerator) and performs a case-insensitive lookup to recover the original casing. Both the environment variable escape hatches and the table grant statements now consistently use the correct model name.

The lookup is cached per FunctionGenerator instance to avoid redundant schema reads.

Testing

  • Added a test exercising randomItem and MealPlan models to verify correct casing in generated output
  • All 242 existing tests in gen2-migration/generate continue to pass

Fixes #14561

@adrianjoshua-strutt
adrianjoshua-strutt marked this pull request as ready for review May 18, 2026 17:10
@adrianjoshua-strutt
adrianjoshua-strutt requested a review from a team as a code owner May 18, 2026 17:10
@adrianjoshua-strutt
adrianjoshua-strutt changed the base branch from gen2-migration to dev May 18, 2026 17:40
…e references

Extract model names from the GraphQL schema and use case-insensitive
matching when resolving table names from environment variable names.
This fixes incorrect capitalization for non-PascalCase models (e.g.,
'randomItem' was incorrectly becoming 'Randomitem').

---
Prompt: Fix non-PascalCase model name resolution in gen2-migration
function generator by reading model names from the GraphQL schema
and matching case-insensitively.
@adrianjoshua-strutt
adrianjoshua-strutt force-pushed the fix/non-pascal-case-model-14561 branch from d559b48 to d0f114f Compare May 18, 2026 20:58
soberm
soberm previously approved these changes May 19, 2026
sarayev
sarayev previously approved these changes May 19, 2026
…hemaModelNames

The original readSchemaModelNames() only read schema.graphql (single file)
and used a fragile regex that fails when directives appear between the type
name and @model (e.g., @auth(rules: [{...}]) @model).

This commit:
- Uses build/schema.graphql with ModelXConnection regex as the primary
  approach (matching createTableMappings pattern from DataGenerator).
- Falls back to raw user schema when build schema is unavailable,
  supporting both single schema.graphql AND schema/*.graphql directory
  patterns (matching collectUserSchema from DataGenerator).
- Replaces the fragile regex with a parenthesis-depth-aware parser that
  correctly handles directives with nested braces before @model.

Adds tests for: build schema primary path, raw schema fallback, multiple
directives before @model, schema directory pattern, and no-API case.
@adrianjoshua-strutt
adrianjoshua-strutt dismissed stale reviews from sarayev and soberm via 84b0c74 May 19, 2026 09:31
@adrianjoshua-strutt adrianjoshua-strutt self-assigned this May 19, 2026
@adrianjoshua-strutt
adrianjoshua-strutt requested review from sarayev and soberm and removed request for soberm May 26, 2026 14:45
sarayev
sarayev previously approved these changes May 26, 2026
@sarayev sarayev self-assigned this Jul 6, 2026
@adrianjoshua-strutt adrianjoshua-strutt removed their assignment Jul 14, 2026
sharonyajain
sharonyajain previously approved these changes Aug 18, 2026
@soberm
soberm dismissed stale reviews from sharonyajain and sarayev via 5f512ae August 20, 2026 08:58
@sarayev sarayev added run-e2e and removed run-e2e labels Aug 21, 2026
@sharonyajain
sharonyajain requested a review from sarayev August 24, 2026 15:39
soberm
soberm previously approved these changes Aug 25, 2026
@adrianjoshua-strutt
adrianjoshua-strutt dismissed soberm’s stale review August 25, 2026 09:04

The merge-base changed after approval.

soberm
soberm previously approved these changes Aug 25, 2026
sharonyajain
sharonyajain previously approved these changes Aug 25, 2026
@sharonyajain
sharonyajain removed the request for review from sarayev August 25, 2026 09:09
@adrianjoshua-strutt
adrianjoshua-strutt dismissed stale reviews from sharonyajain and soberm August 25, 2026 09:09

The merge-base changed after approval.

@sharonyajain
sharonyajain requested a review from sarayev August 25, 2026 09:10
soberm
soberm previously approved these changes Aug 25, 2026
sharonyajain
sharonyajain previously approved these changes Aug 25, 2026
@sharonyajain
sharonyajain requested review from a team and sarayev and removed request for sarayev August 25, 2026 09:17
Comment thread packages/amplify-cli/adr/006-social-auth.md
…ution

- Merge build-schema and raw-schema model names instead of returning early,
  so models declared @model(queries: null) (no ModelXConnection type) are not
  missed and fall through to naive casing.
- extractTableName: match known model names directly against the env var to
  handle underscore-containing names; anchor the naive fallback regex to the
  last segment before TABLE_; document the case-insensitive collision
  assumption.
- Harden @model detection with a word boundary so @models/@modelFoo no longer
  match.
- Tighten the naive-fallback test to assert the exact generated table
  reference, and add extractTableName unit tests plus a queries:null merge
  regression test.
@sharonyajain
sharonyajain dismissed stale reviews from soberm and themself via 237f4bc August 25, 2026 13:11
- bound the raw-schema @model scan to each type so a bodyless type cannot leak into the next (defense-in-depth)
- document the single-file-XOR-directory schema invariant in collectUserSchema
- emit printer.debug in the two schema-read catch blocks to aid diagnosing a miscased table
- add a guard test that a non-model type preceding real models is never attributed as a table
@sharonyajain
sharonyajain requested a review from sarayev August 26, 2026 12:52
@sharonyajain
sharonyajain removed their request for review August 26, 2026 13:27
@sharonyajain
sharonyajain merged commit 33d2ca2 into dev Aug 26, 2026
5 checks passed
@sharonyajain
sharonyajain deleted the fix/non-pascal-case-model-14561 branch August 26, 2026 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(gen2-migration) generate command creates incorrect env variable declaration for a function that accesses a non pascal cased model name table

5 participants