ory / ory/sdk

Error reporting when calling RelationshipApi.createRelationship() using Java Client for Ory Keto(verison:0.11.0-alpha.0)

Open
#271 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
PHP
Stars
178
Forks
96
Avg merge
6d 23h
Merged PRs (30d)
3

Description

Preflight checklist
Describe the bug

Error reporting when calling RelationshipApi.createRelationship() using Java Client for Ory Keto(verison:0.11.0-alpha.0),detail info:
Exception in thread "main" java.lang.NoSuchMethodError: okhttp3.RequestBody.create(Ljava/lang/String;Lokhttp3/MediaType;)Lokhttp3/RequestBody;
at sh.ory.ApiClient.serialize(ApiClient.java:998)
at sh.ory.ApiClient.buildRequest(ApiClient.java:1251)
at sh.ory.ApiClient.buildCall(ApiClient.java:1203)
at sh.ory.api.RelationshipApi.createRelationshipCall(RelationshipApi.java:264)
at sh.ory.api.RelationshipApi.createRelationshipValidateBeforeCall(RelationshipApi.java:269)
at sh.ory.api.RelationshipApi.createRelationshipWithHttpInfo(RelationshipApi.java:307)
at sh.ory.api.RelationshipApi.createRelationship(RelationshipApi.java:288)
at com.tech.fate.portal.flow.KetoTest.createRelationship(KetoTest.java:105)
at com.tech.fate.portal.flow.KetoTest.main(KetoTest.java:46)

Reproducing the bug

First, I started the keto service:
1.mkdir data & cd data
2.clone https://github.com/ory/keto.git
3.download https://raw.githubusercontent.com/ory/meta/master/install.sh
4.bash install.sh -d -b . keto v0.11.1-alpha.0
5.sudo mv ./keto /usr/local/bin/
6.keto serve -c /data/keto/contrib/docs-code-samples/expand-api-display-access/keto.yml

Secondly, I use the Java Client for Ory Keto(verison:0.11.0-alpha.0) to call the keto service according to the demo in the guides
1.init a springboot project
2.import Java Client for Ory Keto(verison:0.11.0-alpha.0) in the pom.xml

sh.ory.keto
keto-client
0.11.0-alpha.0

3.Create a junit test class as follows and test it according to the example(https://github.com/ory/client-java/blob/master/docs/RelationshipApi.md#createRelationship)

private static void createRelationship() {
ApiClient defaultClient = Configuration.getDefaultApiClient();
defaultClient.setBasePath("http://localhost:4467");
RelationshipApi apiInstance = new RelationshipApi(defaultClient);
CreateRelationshipBody createRelationshipBody = new CreateRelationshipBody(); // CreateRelationshipBody |
createRelationshipBody.setNamespace("messages");
createRelationshipBody.setObject("lyl_obj");
createRelationshipBody.setRelation("lyl_relation");
createRelationshipBody.setSubjectId("lyl_sub");
SubjectSet subjectSet = new SubjectSet();
subjectSet.setNamespace("messages");
subjectSet.setObject("lyl_obj");
subjectSet.setRelation("lyl_relation");
createRelationshipBody.setSubjectSet(subjectSet);
try {
Relationship result = apiInstance.createRelationship(createRelationshipBody);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling RelationshipApi#createRelationship");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
}
}

4.I found that the order of the incoming parameters is wrong when ApiClient#serialize calls RequestBody#create: the first parameter of RequestBody#create of okhttp3 is MediaType, but when ApiClient#serialize is called, the second parameter is passed in as MediaType:
ApiClient:
public RequestBody serialize(Object obj, String contentType) throws ApiException {
if (obj instanceof byte[]) {
// Binary (byte array) body parameter support.
return RequestBody.create((byte[]) obj, MediaType.parse(contentType));
} else if (obj instanceof File) {
// File body parameter support.
return RequestBody.create((File) obj, MediaType.parse(contentType));
} else if ("text/plain".equals(contentType) && obj instanceof String) {
return RequestBody.create((String) obj, MediaType.parse(contentType));
} else if (isJsonMime(contentType)) {
String content;
if (obj != null) {
content = JSON.serialize(obj);
} else {
content = null;
}
return RequestBody.create(content, MediaType.parse(contentType));
} else if (obj instanceof String) {
return RequestBody.create((String) obj, MediaType.parse(contentType));
} else {
throw new ApiException("Content type "" + contentType + "" is not supported");
}
}

okhttp3:
public static RequestBody create(@Nullable MediaType contentType, String content) {
Charset charset = StandardCharsets.UTF_8;
if (contentType != null) {
charset = contentType.charset();
if (charset == null) {
charset = StandardCharsets.UTF_8;
contentType = MediaType.parse(contentType + "; charset=utf-8");
}
}

    byte[] bytes = content.getBytes(charset);
    return create(contentType, bytes);
}
Relevant log output
Exception in thread "main" java.lang.NoSuchMethodError: okhttp3.RequestBody.create(Ljava/lang/String;Lokhttp3/MediaType;)Lokhttp3/RequestBody;
	at sh.ory.ApiClient.serialize(ApiClient.java:998)
	at sh.ory.ApiClient.buildRequest(ApiClient.java:1251)
	at sh.ory.ApiClient.buildCall(ApiClient.java:1203)
	at sh.ory.api.RelationshipApi.createRelationshipCall(RelationshipApi.java:264)
	at sh.ory.api.RelationshipApi.createRelationshipValidateBeforeCall(RelationshipApi.java:269)
	at sh.ory.api.RelationshipApi.createRelationshipWithHttpInfo(RelationshipApi.java:307)
	at sh.ory.api.RelationshipApi.createRelationship(RelationshipApi.java:288)
	at com.tech.fate.portal.flow.KetoTest.createRelationship(KetoTest.java:105)
	at com.tech.fate.portal.flow.KetoTest.main(KetoTest.java:46)
Relevant configuration
dsn: memory
log:
  level: trace

namespaces:
  - name: messages
    id: 1
Version

0.11.0-alpha.0

On which operating system are you observing this issue?

None

In which environment are you deploying?

None

Additional Context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with sh.ory.ApiClient.serialize, then trace the call from RelationshipApi.createRelationship using the Java client example in docs/RelationshipApi.md. Reproduce the NoSuchMethodError with the reported Ory Keto and client versions, and verify that the relationship creation call completes without the okhttp3 method error.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.