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

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

Open
3 of 6 tasks
liuyuliang092 opened this issue May 23, 2023 · 0 comments
Labels
bug Something is not working.

Comments

@liuyuliang092
Copy link

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

@liuyuliang092 liuyuliang092 added the bug Something is not working. label May 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is not working.
Projects
None yet
Development

No branches or pull requests

1 participant