trevorwang / retrofit.dart

retrofit.dart is an dio client generator using source_gen and inspired by Chopper and Retrofit.
https://mings.in/retrofit.dart/
MIT License
1.09k stars 250 forks source link

Invalid url generated when using dio baseUrl with an endpoint (like /api/v1) #713

Closed ChiggyLucify closed 1 month ago

ChiggyLucify commented 1 month ago

Describe the bug When using both dio baseUrl and the retrofit annotation's baseUrl together, the annotation's baseUrl overrides the existing baseUrl's endpoint. For example: /api/v1 with /employee.

To Reproduce This can be reproduced from the example given in the documentation (README) Steps to reproduce the behavior:

@RestApi(baseUrl: '/tasks')
abstract class RestClient {
  factory RestClient(Dio dio, {String? baseUrl}) = _RestClient;

  @GET('{id}')
  Future<HttpResponse<Task>> getTask(@Path('id') String id);

  @GET('')
  Future<HttpResponse<List<Task>>> getTasks();
}

// Inside main
dio.options.baseUrl = 'https://mockapi.io/api/v1';
final client = RestClient(dio);
await client.getTasks();

Expected behavior The client should send a request to https://mockapi.io/api/v1/tasks. But instead, it makes a request to https://mockapi.io/tasks.

The issue is that /api/v1 is removed, which messes up everything.

I have confirmed this by both logging (talker_dio) and also by debugging with breakpoint. I can see the url being generated inside the _combineBaseUrls method of my generated class. With return Uri.parse(dioBaseUrl).resolveUri(url).toString();

trevorwang commented 1 month ago

remove the / from your @RestApi(baseUrl: '/tasks')

ChiggyLucify commented 1 month ago

I feel like an idiot now 🤡 Thank you! Maybe this can updated in the README.md example 😅