watson-developer-cloud / java-sdk

:1st_place_medal: Java SDK to use the IBM Watson services.
http://watson-developer-cloud.github.io/java-sdk/
Apache License 2.0
590 stars 533 forks source link

[Discovery] Query execution fails with RuntimeException if query string contains '|' character #654

Closed mwakao closed 7 years ago

mwakao commented 7 years ago

The following code using Discovery service Java Client API fails with error. If I change the query to just "author", it works fine.

import com.ibm.watson.developer_cloud.discovery.v1.Discovery;
import com.ibm.watson.developer_cloud.discovery.v1.model.query.QueryRequest;
import com.ibm.watson.developer_cloud.discovery.v1.model.query.QueryResponse;
import com.ibm.watson.developer_cloud.http.ServiceCall;

public class DiscoveryTest {
    static private String endPoint = "https://gateway.watsonplatform.net/discovery/api";
    static private String username = "[User name]";
    static private String password = "[Password]";
    static private String environmentId = "[Environment ID]";
    static private String collectionId = "[Collection ID]";

//    static private String query = "author";
    static private String query = "author|writer";

    static public void main(String args[]){
        Discovery service = new Discovery("2016-12-11");
        service.setEndPoint(endPoint);
        service.setUsernameAndPassword(username, password);

        QueryRequest.Builder builder = new QueryRequest.Builder(environmentId, collectionId);
        QueryRequest queryRequest = builder.query(query).build();

        ServiceCall<QueryResponse> call = service.query(queryRequest);
        QueryResponse response = call.execute();

        System.out.println(response.toString());
    }
}
maniax89 commented 7 years ago

seems like @germanattanasio already opened an issue a long time ago about this https://github.com/square/okhttp/issues/1976

mwakao commented 7 years ago

I think https://github.com/square/okhttp/issues/1976 was a problem of okhttp3.HttpUrl#uri() . Now uri() method escapes '|'. That's fine now.

com.ibm.watson.developer_cloud.http.RequestBuilder that constructs URL in Watson SDK uses okhttp3.HttpUrl#url() method that doesn't escape '|' character. That may cause this problem.

See https://square.github.io/okhttp/3.x/okhttp/okhttp3/HttpUrl.html for more detail for the difference between uri and url method.

So, I think there are inconsistency between SDK implementation and Service implementation. SDK uses url() method that is less strict than uri(), but service side requires strict one. I think the same version of SDK worked fine with the discovery service until February, so service side changed something at the time.