Skip to content

Conversation

Ali1Ammar
Copy link

@Ali1Ammar Ali1Ammar commented Aug 4, 2025

This PR introduces a powerful new pattern that allows Chopper interceptors to access the data of our API Swagger request

We've created a SwaggerMetaData class that carries endpoint-specific details—like security schemes and tags—and injects this data into the request lifecycle using Chopper's @Tag() feature.

Motivation
Previously, our interceptors operated with limited context. An AuthInterceptor, for example, had no easy way to know if a specific endpoint required authentication, often leading to brittle logic that applied headers to all requests. This change allows us to build smarter, context-aware interceptors that act based on the endpoint's formal definition.

Implementation
The code generator now creates a SwaggerMetaData object for each API method, populated with data from the Swagger spec. This object is passed as a tag, making it available within any interceptor.

This enables cleaner, more reliable logic. For instance, the AuthInterceptor can now simply check if the endpoint's metadata includes the BearerAuth security requirement before attaching a token.

  @POST(path: '/example/example', optionalBody: true)
  Future<chopper.Response<ExampleExamplePost$Response>> _exampleExamplePost({
    @Body() required ExampleExamplePost$RequestBody? body,
    @chopper.Tag()
    SwaggerMetaData swaggerMetaData = const SwaggerMetaData(
      summary: '',
      description: 'This endpoint is for STUDENT',
      operationId: '',
      consumes: [],
      produces: [],
      security: ["BearerAuth"],
      tags: ["STUDENT", "example"],
      deprecated: false,
    ),
  });
class AuthInterceptor implements Interceptor {
  late final Future<String> Function() getToken;

  @override
  FutureOr<Response<BodyType>> intercept<BodyType>(
      Chain<BodyType> chain) async {
    var request = chain.request;
    if (request.tag case SwaggerMetaData swaggerMetaData
        when swaggerMetaData.security.contains('BearerAuth')) {
      final token = await getToken();
      request = applyHeader(
        request,
        HttpHeaders.authorizationHeader,
        'Bearer $token',
        override: false,
      );
    }

    final response = await chain.proceed(request);
    return response;
  }
}

…code generator

- Introduced SwaggerMetaData class to encapsulate metadata information for API requests.
- Updated the code generator to create metadata files alongside existing output files.
- Modified existing service classes to include SwaggerMetaData as a parameter in API methods.
- Enhanced the build process to support the generation of metadata files for each API endpoint.
- Updated pubspec.yaml to include new dependencies and ensure compatibility with the latest Dart SDK.
@Ali1Ammar Ali1Ammar marked this pull request as ready for review August 5, 2025 00:06
@diegotori
Copy link
Contributor

@Vovanella95 ball's in your court.

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.

2 participants