async-http-client
async-http-client copied to clipboard
Provide an ImmutableRequestBuilder so addQueryParams/setQueryParams method argument are not mutated
the addQueryParams/setQueryParams methods are assigning the passed argument directly to the class field queryParams. This has unwanted side-effects.
Code:
RequestBuilderBase.addQueryParams
RequestBuilderBase.setQueryParams
Example:
List<Param> params = new ArrayList<>();
params.add(new Param("testKey", "testVal"));
Request request = httpClient.prepareGet(url)
.setQueryParams(params)
.addQueryParam("testKey2", "testVal2") // params list is modified here
.build();
params.forEach(p -> System.out.println(p.getName() + ": " + p.getValue()));
// EXPECTED:
// testKey: testVal
// OUTPUT:
// testKey: testVal
// testKey2: testVal2
Another example:
// params as unmodifiable collection
List<Param> params = Collections.singletonList(new Param("testKey", "testVal"));
Request request = httpClient.prepareGet(url)
.setQueryParams(params)
.addQueryParam("testKey2", "testVal2")
.build();
// throws java.lang.UnsupportedOperationException on addQueryParam("testKey2", "testVal2")
I suggest replacing the assignments queryParams = params with copies as below:
queryParams = new ArrayList<>(params);
Would you be willing to accept a pull request?
The current behavior is intended: mutable builder for best performance with limited allocations.
Would you be willing to accept a pull request?
Maybe introduce a second ImmutableRequestBuilder?
Just for clarification, ImmutableRequestBuilder would still be mutating it's internal state on each method call. But it would no longer modify objects passed as arguments from calling methods.
Is that it?
No, please go with full immutability.
Similar to Scala case classes.