Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional and optional named query parameters are not handled properly #50

Closed
Aidanvii7 opened this issue Aug 28, 2019 · 3 comments
Closed
Assignees
Labels
enhancement New feature or request

Comments

@Aidanvii7
Copy link

Aidanvii7 commented Aug 28, 2019

Description
This is arguably a feature request, however it can be considered a bug as well as the optional and optional named parameters are accepted.

To Reproduce
Implement something like:

import 'package:dio/dio.dart';
import 'package:retrofit/http.dart';
import 'package:meta/meta.dart';

part 'my_client.g.dart';

@RestApi()
abstract class MyClient {

  factory MyClient(Dio dio) = _MyClient;

  @GET("content/api")
  Future<Result> namedExample(@Query("apikey") String apiKey,
      @Query("scope") String scope,
      @Query("type") String type, {
        @required @Query("from") int from
      });

  @GET("content/api")
  Future<Result> optionalNamedExample(@Query("apikey") String apiKey,
      @Query("scope") String scope,
      @Query("type") String type, {
        @Query("from") int from
      });

  @GET("content/api")
  Future<Result> optionalExample(@Query("apikey") String apiKey,
      @Query("scope") String scope,
      @Query("type") String type, [
        @Query("from") int from
      ]);
}

Expected behavior
In the case of namedExample, I would expect a null assertion to occur for the from parameter as of the existence of the @required annotation, however this is not the case.

In the case of optionalNamedExample, I would expect no null assertion for the from parameter as @required is not present. This is correct, however I would expect it to only be added to the queryParameters if from is not null (I think Square's Retrofit handles this). This could be done simply with Dart 2.3's control flow collections by doing:

final queryParameters = <String, dynamic>{
      'apikey': apiKey,
      'scope': scope,
      'type': type,
      if(from != null) 'from': from
    };

In the case of optionalExample, I would expect the same sort of behaviour as above.

@Aidanvii7
Copy link
Author

p.s. great job with the library!

@trevorwang trevorwang self-assigned this Sep 5, 2019
@trevorwang trevorwang added the enhancement New feature or request label Sep 5, 2019
@devkabiir
Copy link
Contributor

@required is deprecated, Work is in progress for the new required syntax see: dart-lang/language#15.

Temporarily I can enable the @required But It will go away in the future.

@marinat
Copy link

marinat commented Feb 14, 2020

is this realized? optional query parameter still adds even it is null

trevorwang added a commit that referenced this issue Feb 15, 2020
* chore: remove unused demo

* feat: remove query parameter which is null   #50 

* chore: minor changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants