Skip to content

fix(java): catch null exception for ClientOptions APIC-537 #703

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

Merged
merged 4 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ApiClient(String appId, String apiKey, String clientName, String version,

this.algoliaAgent = new AlgoliaAgent(version);
this.algoliaAgent.addSegment(new AlgoliaAgent.Segment(clientName, version));
if (options.getAlgoliaAgentSegments() != null) {
if (options != null && options.getAlgoliaAgentSegments() != null) {
for (AlgoliaAgent.Segment segment : options.getAlgoliaAgentSegments()) {
this.algoliaAgent.addSegment(segment);
}
Expand All @@ -49,8 +49,9 @@ public ApiClient(String appId, String apiKey, String clientName, String version,
addDefaultHeader("Accept", this.contentType);
addDefaultHeader("Content-Type", this.contentType);

this.requester = options.getRequester();
if (this.requester == null) {
if (options != null && options.getRequester() != null) {
this.requester = options.getRequester();
} else {
this.requester = new HttpRequester();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ public class ClientOptions {
private List<AlgoliaAgent.Segment> algoliaAgentSegments;
private List<StatefulHost> hosts;

private ClientOptions() {
public ClientOptions() {
algoliaAgentSegments = new ArrayList<>();
}

public static ClientOptions build() {
return new ClientOptions();
}

public Requester getRequester() {
return this.requester;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public static void main(String[] args) {
SearchClient client = new SearchClient(
dotenv.get("ALGOLIA_APPLICATION_ID"),
dotenv.get("ALGOLIA_ADMIN_KEY"),
ClientOptions
.build()
new ClientOptions()
.addAlgoliaAgentSegment("test", "8.0.0")
.addAlgoliaAgentSegment("JVM", "11.0.14")
.addAlgoliaAgentSegment("no version")
Expand Down
14 changes: 7 additions & 7 deletions templates/java/libraries/okhttp-gson/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public class {{classname}} extends ApiClient {

public {{classname}}(String appId, String apiKey, String region, ClientOptions options) {
super(appId, apiKey, "{{{baseName}}}", "{{{packageVersion}}}", options);
if (options.getHosts() == null) {
this.setHosts(getDefaultHosts(region));
} else {
if (options != null && options.getHosts() != null) {
this.setHosts(options.getHosts());
} else {
this.setHosts(getDefaultHosts(region));
}
this.setConnectTimeout(2000);
this.setReadTimeout(5000);
Expand All @@ -65,11 +65,11 @@ public class {{classname}} extends ApiClient {
}

public {{classname}}(String appId, String apiKey, ClientOptions options) {
super(appId, apiKey, "{{{baseName}}}", "{{{packageVersion}}}", options);
if (options.getHosts() == null) {
this.setHosts(getDefaultHosts(appId));
} else {
super(appId, apiKey, "{{{baseName}}}", "{{{packageVersion}}}", options);
if (options != null && options.getHosts() != null) {
this.setHosts(options.getHosts());
} else {
this.setHosts(getDefaultHosts(appId));
}
this.setConnectTimeout(2000);
this.setReadTimeout(5000);
Expand Down
2 changes: 1 addition & 1 deletion templates/java/tests/client/createClient.mustache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{^autoCreateClient}}{{client}} $client = {{/autoCreateClient}}new {{client}}("{{parametersWithDataTypeMap.appId.value}}","{{parametersWithDataTypeMap.apiKey.value}}",{{#hasRegionalHost}}{{#parametersWithDataTypeMap.region}}"{{parametersWithDataTypeMap.region.value}}",{{/parametersWithDataTypeMap.region}}{{/hasRegionalHost}}ClientOptions.build().setRequester(requester));
{{^autoCreateClient}}{{client}} $client = {{/autoCreateClient}}new {{client}}("{{parametersWithDataTypeMap.appId.value}}","{{parametersWithDataTypeMap.apiKey.value}}",{{#hasRegionalHost}}{{#parametersWithDataTypeMap.region}}"{{parametersWithDataTypeMap.region.value}}",{{/parametersWithDataTypeMap.region}}{{/hasRegionalHost}}new ClientOptions().setRequester(requester));
2 changes: 1 addition & 1 deletion templates/java/tests/client/suite.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class {{client}}ClientTests {
}

{{client}} createClient() {
return new {{client}}("appId", "apiKey", {{#hasRegionalHost}}"{{defaultRegion}}", {{/hasRegionalHost}}ClientOptions.build().setRequester(requester));
return new {{client}}("appId", "apiKey", {{#hasRegionalHost}}"{{defaultRegion}}", {{/hasRegionalHost}}new ClientOptions().setRequester(requester));
}

{{#blocksClient}}
Expand Down
2 changes: 1 addition & 1 deletion templates/java/tests/requests/requests.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class {{client}}RequestsTests {
HttpRequester requester = new HttpRequester();
echo = new EchoInterceptor();
requester.addInterceptor(echo.getEchoInterceptor());
client = new {{client}}("appId", "apiKey", {{#hasRegionalHost}}"{{defaultRegion}}", {{/hasRegionalHost}}ClientOptions.build().setRequester(requester));
client = new {{client}}("appId", "apiKey", {{#hasRegionalHost}}"{{defaultRegion}}", {{/hasRegionalHost}}new ClientOptions().setRequester(requester));
}

{{#blocksRequests}}
Expand Down
4 changes: 2 additions & 2 deletions website/docs/clients/guides/customized-client-usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ import com.algolia.api.SearchClient;
SearchClient client = new SearchClient(
"<YOUR_APP_ID>",
"<YOUR_API_KEY>",
ClientOptions.build().addAlgoliaAgentSegments("my user agent", "optional version")
new ClientOptions().addAlgoliaAgentSegments("my user agent", "optional version")
);
```

Expand Down Expand Up @@ -135,7 +135,7 @@ import com.algolia.api.SearchClient;
SearchClient client = new SearchClient(
"<YOUR_APP_ID>",
"<YOUR_API_KEY>",
ClientOptions.build().setRequester(new MyOwnRequester())
new ClientOptions().setRequester(new MyOwnRequester())
);
```

Expand Down