Skip to content

Conversation

@Fellmonkey
Copy link
Contributor

@Fellmonkey Fellmonkey commented Nov 4, 2025

Updates Dart test template to use the first enum value for string properties with enums when generating test data, instead of always using the example value. This ensures generated test data is valid for properties with restricted enum values.

What does this PR do?

This PR updates the Dart SDK test generation template (service_test.dart.twig) to properly handle enum properties in generated test mocks. Previously, all string properties used property.example as mock values, which could lead to invalid enum values causing "Bad state: No element" errors in model constructors like Message.fromMap when the example value didn't match any enum option.

The change adds conditional logic for string properties:

  • If the property has an enum defined, use the first enum value (property.enum[0]) as the mock value
  • Otherwise, fall back to using property.example as before

This ensures that generated tests use valid enum values for properties with enums, preventing runtime errors during test execution.

Test Plan

regenerate sdk and run dart test

also push changes and ensure the GitHub Actions workflow for Dart SDK completes successfully with all tests passing. #1247

before:
image
image
after:
{7AD8E36C-F2DB-45FC-887E-CD8B0C5C9ECA}

Have you read the Contributing Guidelines on issues?

Yes

Summary by CodeRabbit

  • New Features
    • Enhanced test data generation to properly handle enum constraints for string properties, using the first enum value when available instead of placeholder examples.

Updates Dart test template to use the first enum value for string properties with enums when generating test data, instead of always using the example value. This ensures generated test data is valid for properties with restricted enum values.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 4, 2025

Walkthrough

This change modifies the Dart test service template to add enum support for string-type properties in generated test data. When generating test data, if a string property defines an enum, the template now uses the first enum value instead of the example value. The modification affects both the sub-schema property rendering and test data construction for response models, while the overall control flow remains unchanged.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Conditional logic correctness: Verify that the enum[0] selection properly handles edge cases, such as empty enum arrays or missing enum definitions
  • Template variable scope: Ensure enum variable references are correctly available in both sub-schema and response model contexts
  • Test data consistency: Confirm that using the first enum value produces valid, representative test data across different test scenarios
  • Template syntax validation: Check that Twig template syntax for the enum conditional is correct and properly indented

Pre-merge checks and finishing touches

❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'Fix dart tests' is vague and generic, failing to convey the specific nature of the fix—which is adding enum support for string-type properties in test data generation. Consider a more descriptive title such as 'Add enum support for string properties in Dart test data generation' or 'Handle string enum properties in Dart test mock data' to better communicate the specific change.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cd71267 and da3e088.

📒 Files selected for processing (1)
  • templates/dart/test/services/service_test.dart.twig (2 hunks)
🔇 Additional comments (2)
templates/dart/test/services/service_test.dart.twig (2)

72-72: Consistent implementation, same verification needed.

The enum handling here mirrors line 3, which is good for consistency. The same concern about empty enum arrays applies here as well.

The fix correctly addresses the runtime error described in the PR objectives. The conditional logic ensures generated tests use valid enum values, preventing "Bad state: No element" errors in model constructors.

Please run the same verification from the previous comment to confirm enum arrays are never empty across both usage locations.


3-3: No changes required—the existing enum check is already safe.

The code correctly uses {% if property.enum %} which is idiomatic Twig and already defensive. In Twig, empty arrays evaluate to false, so accessing property.enum[0] within the if block is safe. The additional length check suggested in the review comment is redundant; the existing condition already prevents index-out-of-bounds errors when enums are empty.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant