Skip to content

fix: Use proper camelCase for detach method parameters with multi-word class names - #4082

Merged
SandPod merged 4 commits into
mainfrom
copilot/fix-lower-camel-case-id
Oct 27, 2025
Merged

SandPod merged 4 commits into
mainfrom
copilot/fix-lower-camel-case-id

Conversation

Copilot AI commented Oct 24, 2025 •

Copy link
Copy Markdown
Contributor

Fix detach method parameter name casing for multi-word class names

This PR fixes a bug where the detach method for object relations generates parameter names in all lowercase instead of proper lowerCamelCase when the class name contains multiple words.

Issue Reference: Fixes #2448

Root Cause:
In repository_classes.dart line 1634, the _buildDetachRowFromObjectRelationField method uses className.toCamelCase(isLowerCamelCase: true) which converts multi-word class names like CitizenInt to citizenint (all lowercase) instead of citizenInt (proper camelCase).

Changes Made:

  • Fixed _buildDetachRowFromObjectRelationField to use className.camelCase instead of className.toCamelCase(isLowerCamelCase: true) (line 1633)
  • Removed unused import 'package:super_string/super_string.dart'; (line 6)
  • Added comprehensive test case in database_repository_class_object_relation_additions_test.dart for multi-word class names
  • Regenerated test server code - all generated files now use proper camelCase
  • Regenerated all projects in the repository using util/generate_all script
  • Updated integration test expectations in attach_detach_test.dart to match corrected parameter names
  • Rebased on latest main branch (commit 768c67f - includes SSL cert updates)
  • Verified all 27 repository-related tests pass
  • Verified code analysis passes with no issues
  • Security scan completed - no vulnerabilities detected

Examples of Fixed Code:

// Before (incorrect):
Future<void> address(
  _i1.Session session,
  CitizenInt citizenint, {  // ❌ all lowercase
  _i1.Transaction? transaction,
}) async {

// After (correct):
Future<void> address(
  _i1.Session session,
  CitizenInt citizenInt, {  // ✅ proper camelCase
  _i1.Transaction? transaction,
}) async {

Other affected classes:

  • AddressUuid → parameter now addressUuid (was addressuuid)
  • UserProfile → parameter now userProfile (was userprofile)
  • All multi-word class names now generate proper camelCase parameters

Testing:

  • ✅ 27 repository generation tests pass
  • ✅ New test specifically validates multi-word class name parameter casing
  • ✅ Integration tests updated to expect proper camelCase in error messages
  • ✅ Code analysis passes with --fatal-infos
  • ✅ CodeQL security scan completed
  • ✅ Generated code verified manually
  • ✅ All projects regenerated successfully
  • ✅ Successfully rebased on latest main (768c67f) without conflicts

Breaking Change Note:
None.

Security Summary:
No security vulnerabilities were introduced or discovered during this change. CodeQL analysis returned no issues.

Original prompt

This section details on the original issue you should resolve

<issue_title>Detach method id reference is being generated all lowercase instead of lower camel case</issue_title>
<issue_description>### Describe the bug

When generating the detach method for related classes, the id parameter is being converted to all lower case instead of lower camel case. For example, with a reference class called CitizenInt, the detach method is being generated as:

class CitizenIntDetachRowRepository {
  const CitizenIntDetachRowRepository._();

  /// Detaches the relation between this [CitizenInt] and the [AddressUuid] set in `address`
  /// by setting the [CitizenInt]'s foreign key `id` to `null`.
  ///
  /// This removes the association between the two models without deleting
  /// the related record.
  Future<void> address(
    _i1.Session session,
    CitizenInt citizenint, {  // <-- Should be `citizenInt`, with capital "I" for the second word.
    _i1.Transaction? transaction,
  }) async {

To reproduce

Create two models with more than one word in the name (i.e.: AddressUuid and CitizenInt) and with object relations to each other:

class: AddressUuid
table: address_uuid
fields:
  street: String
  inhabitantId: int?
  inhabitant: CitizenInt?, relation(name=citizen_address, field=inhabitantId)
indexes:
  inhabitant_uuid_index_idx:
    fields: inhabitantId
    unique: true
class: CitizenInt
table: citizen_int
fields:
  name: String
  address: AddressUuid?, relation(name=citizen_address)

Now check the generated CitizenIntRepository to see that the address method of the CitizenIntDetachRowRepository has the parameter citizenint (all lower case), while the address method of the CitizenIntAttachRowRepository has the parameter correctly generated as citizenInt.

class CitizenIntAttachRowRepository {
  const CitizenIntAttachRowRepository._();

  /// Creates a relation between the given [CitizenInt] and [AddressUuid]
  /// by setting the [CitizenInt]'s foreign key `id` to refer to the [AddressUuid].
  Future<void> address(
    _i1.Session session,
    CitizenInt citizenInt,  // Correct
    _i2.AddressUuid address, {
    _i1.Transaction? transaction,
  }) async {
class CitizenIntDetachRowRepository {
  const CitizenIntDetachRowRepository._();

  /// Detaches the relation between this [CitizenInt] and the [AddressUuid] set in `address`
  /// by setting the [CitizenInt]'s foreign key `id` to `null`.
  ///
  /// This removes the association between the two models without deleting
  /// the related record.
  Future<void> address(
    _i1.Session session,
    CitizenInt citizenint, {  // Incorrect
    _i1.Transaction? transaction,
  }) async {

Expected behavior

The CitizenInt parameter should preserve the case of the class name, with only the first letter as lower case - such as already happens for the attach repository.

Library version

2.4.0

Platform information

Windows

Additional context

The issue is probably caused by the conversion to lower camel case in the _buildDetachRowFromObjectRelationField method of the repository_classes.dart file:

  Method _buildDetachRowFromObjectRelationField(String className,
      SerializableModelFieldDefinition field, ClassDefinition classDefinition) {
    return Method((methodBuilder) {
      var classFieldName = className.toCamelCase(isLowerCamelCase: true);  // this should be `.camelCase`
      var fieldName = field.name;

      var relation = field.relation as ObjectRelationDefinition;

      var idTypeReference = relation.parentTableIdType.reference(
        serverCode,
        nullable: false,
        subDirParts: classDefinition.subDirParts,
        config: config,
      ) as TypeReference;

The toCamelCase function is prepared to convert from words separated by spaces or underscores, but not terms that are already as some camel case form. All other parts of the repository_classes.dart use the .camelCase method of the recase package.

How experienced are you with this library?

Expert - Experienced and comfortable with using this library in complex projects

Are you interested in working on a PR for this?

  • I want to work on this</issue_description>

Comments on the Issue (you are @copilot in this section)

@SandPod Thanks for reporting this! 👍

Since this will change the generated interface we will need to fix this in the next major version.</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@coderabbitai

coderabbitai Bot commented Oct 24, 2025

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI changed the title [WIP] Fix id reference case in detach method generation fix: Use proper camelCase for detach method parameters with multi-word class names Oct 24, 2025
Copilot AI requested a review from SandPod October 24, 2025 13:59
@SandPod

SandPod commented Oct 24, 2025

Copy link
Copy Markdown
Contributor

@copilot - The generate test is failing in the CI. Could you run the generate_all script in the utils folder and commit the result? It seems like there are projects that have not been generated with the fix in place.

Copilot AI commented Oct 24, 2025

Copy link
Copy Markdown
Contributor Author

@copilot - The generate test is failing in the CI. Could you run the generate_all script in the utils folder and commit the result? It seems like there are projects that have not been generated with...

Done! I've run the util/generate_all script and committed the result. The script regenerated the UserProfile detach method parameter from userprofile to userProfile (proper camelCase). Commit: e842d77

@SandPod

SandPod commented Oct 25, 2025

Copy link
Copy Markdown
Contributor

@copilot
Test on line in 297 and 312 needs to be updated in test_integration/database_operations/changed_id_type_relations/one_to_one/update/attach_detach_test.dart

Copilot AI commented Oct 25, 2025

Copy link
Copy Markdown
Contributor Author

@copilot
Test on line in 297 and 312 needs to be updated in test_integration/database_operations/changed_id_type_relations/one_to_one/update/attach_detach_test.dart

Updated the test expectations on lines 297 and 312. Changed 'citizenint.id' to 'citizenInt.id' and 'citizenint.address' to 'citizenInt.address' to match the corrected parameter names. Also removed the TODO comments since the issue is now fixed. Commit: 6486c11

@SandPod

SandPod commented Oct 26, 2025

Copy link
Copy Markdown
Contributor

@copilot - please rebase this PR on latest main.

Copilot AI commented Oct 26, 2025

Copy link
Copy Markdown
Contributor Author

@copilot - please rebase this PR on latest main.

Rebased on latest main (commit 768c67f - "test: Update expired certs"). The PR now includes the SSL cert updates from main and all our fixes are preserved. All tests pass. Commit: 8aafc1d

@SandPod
SandPod force-pushed the copilot/fix-lower-camel-case-id branch from 8aafc1d to 2960656 Compare October 26, 2025 16:31
@SandPod
SandPod marked this pull request as ready for review October 26, 2025 17:35
@SandPod
SandPod requested a review from a team October 26, 2025 17:35

@nielsenko nielsenko left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes looks good. However, they are not breaking as the parameters are always positional.

@SandPod
SandPod merged commit f069c56 into main Oct 27, 2025
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 27, 2025
@marcelomendoncasoares
marcelomendoncasoares deleted the copilot/fix-lower-camel-case-id branch November 18, 2025 11:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detach method id reference is being generated all lowercase instead of lower camel case

3 participants