Skip to content

Commit

Permalink
Merge pull request xapi-project#5610 from danilo-delbusso/bug/jsonvalue
Browse files Browse the repository at this point in the history
[ASAP] CA-392453: Misc fixes to Java SDK
  • Loading branch information
danilo-delbusso authored May 9, 2024
2 parents d0879c7 + 741775d commit 812097d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,8 @@ public String getSessionReference() {
public <T> T dispatch(String methodCall, Object[] methodParameters, TypeReference<T> responseTypeReference) throws XenAPIException, IOException {
var result = client.sendRequest(methodCall, methodParameters, responseTypeReference);
if (result.error != null) {
throw new XenAPIException(String.valueOf(result.error));
}

if (methodCall.equals("session.login_with_password")) {
Types.checkError(result.error);
} else if (methodCall.equals("session.login_with_password")) {
var session = ((Session) result.result);
sessionReference = session.ref;
setAPIVersion();
Expand Down
38 changes: 35 additions & 3 deletions ocaml/sdk-gen/java/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ let gen_class cls folder =
fprintf file
{|package com.xensource.xenapi;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.core.type.TypeReference;
import com.xensource.xenapi.Types.BadServerResponse;
import com.xensource.xenapi.Types.XenAPIException;
Expand All @@ -592,7 +593,10 @@ import java.io.IOException;

if class_is_empty cls then
fprintf file
"\n public String toWireString() {\n return null;\n }\n\n"
" @JsonValue\n\
\ public String toWireString() {\n\
\ return null;\n\
\ }\n\n"
else (
fprintf file " /**\n" ;
fprintf file " * The XenAPI reference (OpaqueRef) to this object.\n" ;
Expand All @@ -608,6 +612,7 @@ import java.io.IOException;
fprintf file
" * @return The XenAPI reference (OpaqueRef) to this object.\n" ;
fprintf file " */\n" ;
fprintf file " @JsonValue\n" ;
fprintf file " public String toWireString() {\n" ;
fprintf file " return this.ref;\n" ;
fprintf file " }\n\n"
Expand Down Expand Up @@ -719,7 +724,9 @@ let generate_reference_task_result_func file clstr =
" public static %s to%s(Task task, Connection connection) throws \
IOException {\n"
clstr clstr ;
fprintf file " return Types.to%s(task.getResult(connection));\n" clstr ;
fprintf file
" return Types.to%s(parseResult(task.getResult(connection)));\n"
clstr ;
fprintf file " }\n" ;
fprintf file "\n"

Expand Down Expand Up @@ -948,6 +955,8 @@ import java.util.*;
import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* This class holds enum types and exceptions.
Expand Down Expand Up @@ -1030,7 +1039,7 @@ public class Types
if(errorData.length == 0){
throw new BadServerResponse(response);
}
var errorName = errorData[0];
var errorName = response.message;
|} ;

Hashtbl.iter (gen_method_error_throw file) Datamodel.errors ;
Expand All @@ -1052,6 +1061,29 @@ public class Types
TypeSet.iter (gen_task_result_func file) !types ;
fprintf file
{|

public static class BadAsyncResult extends XenAPIException
{
public final String result;

public BadAsyncResult(String result)
{
super(result);
this.result = result;
}
}

private static String parseResult(String result) throws BadAsyncResult
{
Pattern pattern = Pattern.compile("<value>(.*)</value>");
Matcher matcher = pattern.matcher(result);
if (!matcher.find() || matcher.groupCount() != 1) {
throw new Types.BadAsyncResult("Can't interpret: " + result);
}

return matcher.group(1);
}

public static EventBatch toEventBatch(Object object) {
if (object == null) {
return null;
Expand Down

0 comments on commit 812097d

Please sign in to comment.