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

Exclude null query vars by default and add new @Method annotation includeNullQueryVars #372

Merged
merged 2 commits into from
Oct 14, 2022

Conversation

techouse
Copy link
Collaborator

@techouse techouse commented Oct 13, 2022

Fix #371

Because some backends do not ignore null query parameters I have re-introduced the behaviour of excluding null query vars by default. This includes nested null query parameters inside maps.

This was the default behaviour prior to v5.0.1 and the merge of #364.

Additionally, I have added a @Method annotation bool includeNullQueryVars which, if set to true, includes null query parameters when encoding them into the URL query.

  • Example with includeNullQueryVars: false (default; same behaviour as before v5.0.1)

    @Get(path: '/get_data')
    Future<Response<String>> getData({
      @Query('foo') String? foo,
      @Query('bar') String? bar,
      @Query('baz') String? baz,
    });
    
    final response = await service.getData(
      foo: 'foo_val',
      baz: 'baz_val',
    );

    Produces /test/get_data&foo=foo_val&baz=baz_val

  • Example with includeNullQueryVars: true (the current default in v5.0.1)

    @Get(
      path: '/get_data', 
      includeNullQueryVars: true,
    )
    Future<Response<String>> getData({
      @Query('foo') String? foo,
      @Query('bar') String? bar,
      @Query('baz') String? baz,
    });
    
    final response = await service.getData(
      foo: 'foo_val',
      baz: 'baz_val',
    );

    Produces /test/get_data&foo=foo_val&bar=&baz=baz_val

NOTE: Empty Strings are always included. If you do not want them to be included make them nullable, i.e. String?

@Get(path: '/get_data')
Future<Response<String>> getData({
  @Query('foo') String foo = '',
  @Query('bar') String? bar,
  @Query('baz') String? baz,
});

final response = await service.getData(
  bar: null,
  baz: 'baz_val',
);

Produces /test/get_data&foo=&baz=baz_val

NOTE 2: null values and empty Strings inside query Lists are always excluded.

@Get(path: '/get_data')
Future<Response<String>> getData({
  @Query('foo') List<String?> foo,
});

final response = await service.getData(
  foo: <String?>[
    'value1', 
    '', 
    null, 
    'value4',
  ],
);

Produces /test/get_data&foo=value1&foo=value4

@luis901101
Copy link
Contributor

Hi and thanks for this fix, I also think that default should be ignoreNullQueryVars: true and not otherwise, because as you commented this was the default behavior prior to v5.0.1, so I think it is better to keep the same default to maintain backward compatibility otherwise this will break some existing projects requests, like it's currently happening to me using Java Jooby as Backend.
Not ignoring null query vars as default could be for a new breaking change version like v6.0.0, but I think for v5.x.x the default behavior should remain the same.

@techouse
Copy link
Collaborator Author

Not ignoring null query vars as default could be for a new breaking change version like v6.0.0, but I think for v5.x.x the default behavior should remain the same.

Agreed. I'll amend it.

@techouse
Copy link
Collaborator Author

techouse commented Oct 14, 2022

I have renamed ignoreNullQueryVars to includeNullQueryVars. The default for this setting is false, meaning that the default behaviour will be reverted to the one before v5.0.1.

NOTE: Empty strings are always included.

NOTE 2: null values and empty Strings inside query Lists are always excluded.

I'll now amend the PR description.

@techouse techouse changed the title Add Method annotation to ignore null query vars Fix #371 Exclude null query vars by default and add new @Method annotation includeNullQueryVars Oct 14, 2022
@techouse techouse marked this pull request as ready for review October 14, 2022 05:35
@techouse techouse changed the title Fix #371 Exclude null query vars by default and add new @Method annotation includeNullQueryVars Exclude null query vars by default and add new @Method annotation includeNullQueryVars Oct 14, 2022
@@ -14,7 +14,7 @@ dart_code_metrics:
cyclomatic-complexity: 20
number-of-arguments: 4
maximum-nesting-level: 5
number-of-parameters: 6
number-of-parameters: 10
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hehe)

@JEuler
Copy link
Collaborator

JEuler commented Oct 14, 2022

LGTM, great work as always, thank you so much! ❤️

@JEuler JEuler merged commit bd8d65f into lejard-h:develop Oct 14, 2022
@techouse techouse deleted the ignore-null-query-vars branch October 14, 2022 11:20
JEuler added a commit that referenced this pull request Oct 15, 2022
* Fix Header Option Casting (#260)

Co-authored-by: Ivan Terekhin <[email protected]>

* Fix for #259 (#263)

* 4.0.1 fixes (#264)

* analyzer dependency upgraded (#296)

* fix(generator): fix PartValueFile value not nullable if arg is (#288) (#293)

* Chopper generator release 4.0.2 (#297)

* fix: fix this.body cast of null value when response body is null (#291) (#292)

* Interpolation fixes (#275)

* encodeQueryComponent now encodeComponent (#278)

* Prevent double call on token refreshment (#276)

* Fixes for #309 #308 (#310)

* Remove new keyword from interceptors.md (#312)

* Analyzer upgrade (#320)

Co-authored-by: István Juhos <[email protected]>

* Add unnecessary_brace_in_string_interps to lint ignores (#317)

* Extend pragma to quiet the linter (#318)

Co-authored-by: Ivan Terekhin <[email protected]>

* Fix converter getting called twice if using an authenticator with a JsonConverter on the request (#324)

* migrate example to nullsafety (#331)

* Resolve problem in main_json_serializable example (#328)

* Add @FiledMap @PartMap @PartFileMap (#335)

Co-authored-by: Meysam Karimi <[email protected]>

* Upgrade of analyzer (#340)

* Fix nullable QueryMap fails to compile (#344)

* Change return type of decodeJson to FutureOr in order to be able to support compute() (#345)

* Migrate from pedantic to lints ^2.0.0 with lints/recommended.yaml (#349)

* Version bumped for release (#352)

* Revert analyzer to ^4.1.0 and silence linters for Element.enclosingElement (#354)

* [chopper_generator] Update analyzer to ^4.4.0 and code_builde to ^4.3.0 and migrate deprecated code (#358)

* Add Makefiles to streamline development (#357)

* Add Bug Report Github issue template (#359)

* [chopper_generator] Add types to the generated variables (#360)

* Provide an example using an Isolate Worker Pool with Squadron (#361)

* mapToQuery changes (#364)

* Version bumped / changelog update (#367)

* Request extends http.BaseRequest (#370)

* Exclude null query vars by default and add new @method annotation includeNullQueryVars (#372)

* 5.1.0 (dev) (#373)

Co-authored-by: Ivan Terekhin <[email protected]>

Co-authored-by: Youssef Raafat <[email protected]>
Co-authored-by: luis901101 <[email protected]>
Co-authored-by: melvspace <[email protected]>
Co-authored-by: Michal Šrůtek <[email protected]>
Co-authored-by: István Juhos <[email protected]>
Co-authored-by: Andre <[email protected]>
Co-authored-by: John Wimer <[email protected]>
Co-authored-by: Max Röhrl <[email protected]>
Co-authored-by: ipcjs <[email protected]>
Co-authored-by: ibadin <[email protected]>
Co-authored-by: Meysam Karimi <[email protected]>
Co-authored-by: Meysam Karimi <[email protected]>
Co-authored-by: Klemen Tusar <[email protected]>
Co-authored-by: Klemen Tusar <[email protected]>
Co-authored-by: Ivan Terekhin <[email protected]>
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.

Query with null value included
3 participants