Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 20, 2025

Problem

When using @DioOptions() parameter annotation in any service method, the helper method newRequestOptions() was being generated not only for that service but for all subsequently processed services, even those that never use @DioOptions(). This led to:

  1. Unnecessary code bloat in generated files
  2. Non-deterministic builds (behavior dependent on file processing order)
  3. Confusion when the same source code would sometimes generate different output

Example of the issue:

// ServiceA.dart - no @DioOptions
@RestApi()
abstract class ServiceA {
  @GET('/users')
  Future<String> getUsers();
}

// ServiceB.dart - has @DioOptions
@RestApi()
abstract class ServiceB {
  @GET('/posts')
  Future<String> getPosts(@DioOptions() Options options);
}

// ServiceC.dart - no @DioOptions
@RestApi()
abstract class ServiceC {
  @GET('/comments')
  Future<String> getComments();
}

Before the fix:

  • ServiceA: ✅ No newRequestOptions (processed first)
  • ServiceB: ✅ Has newRequestOptions (uses @DioOptions)
  • ServiceC: ❌ Has newRequestOptions (incorrectly inherited from ServiceB)

After the fix:

  • ServiceA: ✅ No newRequestOptions
  • ServiceB: ✅ Has newRequestOptions
  • ServiceC: ✅ No newRequestOptions

Root Cause

The hasCustomOptions instance variable in RetrofitGenerator was never reset between processing different service classes. Once set to true by a service using @DioOptions(), it remained true for all subsequent class generations, causing state leakage.

Solution

Reset hasCustomOptions = false at the start of each class generation in the _implementClass() method. This ensures each service class is generated independently without state pollution.

Changes

  • generator/lib/src/generator.dart: Added state reset at the start of class generation
  • generator/test/src/generator_test_src.dart: Added test case to verify services without @DioOptions don't get the helper method

Testing

  • ✅ All 151 existing tests pass
  • ✅ New test validates the fix:
    • CustomOptions (with @DioOptions) correctly generates newRequestOptions() method
    • ServiceWithoutCustomOptions (without @DioOptions) correctly does NOT generate newRequestOptions() method
  • ✅ Dart analyzer: No issues
  • ✅ Code review: No comments

Fixes the issue reported where newRequestOptions was being generated for every service after the first @DioOptions() usage.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://storage.googleapis.com/flutter_infra_release/flutter/d2913632a4578ee4d0b8b1c4a69888c8a0672c4b/dart-sdk-linux-x64.zip
    • Triggering command: curl --retry 3 --continue-at - --location --output /tmp/flutter/bin/cache/dart-sdk-linux-x64.zip REDACTED (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>newRequestOptions is generated for every service after first @DioOptions() usage</issue_title>
<issue_description>### Describe the problem
If at least one endpoint in any service has a parameter annotated with
@DioOptions() the helper method newRequestOptions() is generated not
only for this service but for ALL subsequently processed services. Even it is not used.
ALSO
This is a floating error. For a one try extra newRequestOptions may be generated and may not be generated for classes where it is not used.

Steps to reproduce

  1. Create two services AService (without @DioOptions) and BService
    (with one method that has @DioOptions()).
  2. Generate code.
  3. Check AService.g.dart – it contains newRequestOptions()
    even though it is never used.
    3.1 For some times it may be not generated. And for next times may be generated. For the same source code.

Versions

retrofit_generator: 9.1.8
retrofit: 4.4.2
flutter: 3.29.2</issue_description>

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

@trevorwang PR is appriciated

Fixes #764


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix generation of newRequestOptions for services Fix: Prevent newRequestOptions from being generated for all services after first @DioOptions() usage Oct 20, 2025
Copilot AI requested a review from trevorwang October 20, 2025 13:02
Copilot finished work on behalf of trevorwang October 20, 2025 13:02
@trevorwang trevorwang marked this pull request as ready for review October 22, 2025 05:05
@trevorwang trevorwang merged commit 864f50f into master Oct 22, 2025
9 checks passed
@trevorwang trevorwang deleted the copilot/fix-new-request-options-generation branch October 22, 2025 05:07
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.

newRequestOptions is generated for every service after first @DioOptions() usage

2 participants