Skip to content

Commit

Permalink
fix ClassCastException for RHPAM-4189
Browse files Browse the repository at this point in the history
  • Loading branch information
bxf12315 committed May 19, 2022
1 parent 63a81a8 commit 5e372bd
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,11 @@ protected void configureMarshaller(Set<Class<?>> classes, final ClassLoader clas
if (classes == null) {
classes = new HashSet<Class<?>>();
}

// add byte array handling support to allow byte[] to be send as payload
classes.add(JaxbByteArray.class);
classes.add(java.time.LocalDateTime.class);

if (!formatDate) {
classes.add(Date.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,79 @@ public void testLegalizeWrapObjectFieldnames() throws Exception {
BatchExecutionCommandImpl unconverted = marshaller.unmarshall(converted, BatchExecutionCommandImpl.class);
assertEquals("all", ((Order) ((InsertObjectCommand) unconverted.getCommands().get(0)).getObject()).getORDER_ID());
}

@Test
public void testLocalDateTimeWithClasses() throws Exception {
HashSet hs = new HashSet<>();
hs.add(org.kie.server.api.marshalling.Person.class);
Marshaller marshaller = new JSONMarshaller(hs, getClass().getClassLoader(), false, false);

String wrapLocalDateTimeWithType = "{\"person\":{\"org.kie.server.api.marshalling.Person\":{\"fullname\":\"123\",\"dateBirth\":{\"java.time.LocalDateTime\":\"2022-05-19T00:00\"},\"age\":\"21\"}}}";
Map converted = marshaller.unmarshall(wrapLocalDateTimeWithType, Map.class);
assertEquals(org.kie.server.api.marshalling.Person.class, converted.get("person").getClass());
assertEquals(java.time.LocalDateTime.class, ((Person) converted.get("person")).getDateBirth().getClass());

String wrapLocalDateTimeWithoutType = "{\"person\":{\"org.kie.server.api.marshalling.Person\":{\"fullname\":\"123\",\"dateBirth\":\"2022-05-19T00:00\",\"age\":\"21\"}}}";
Map converted1 = marshaller.unmarshall(wrapLocalDateTimeWithoutType, Map.class);
assertEquals(org.kie.server.api.marshalling.Person.class, converted1.get("person").getClass());
assertEquals(java.time.LocalDateTime.class, ((Person) converted1.get("person")).getDateBirth().getClass());

String localDateTimeStringWithType = "{\n" +
" \"bdate\":{\"java.time.LocalDateTime\":\"2022-05-17T14:54\"},\n" +
" \"name\":\"123\",\n" +
" \"bbdate\":{\"java.time.LocalDateTime\":\"2022-05-18T00:00\"}\n" +
"}";
Map converted2 = marshaller.unmarshall(localDateTimeStringWithType, Map.class);
assertEquals(java.time.LocalDateTime.class, converted2.get("bdate").getClass());

String localDateTimeStringWithoutType = "{\n" +
" \"bdate\":\"2022-05-17T14:54\",\n" +
" \"name\":\"123\",\n" +
" \"bbdate\":\"2022-05-18T00:00\"}\n" +
"}";
Map converted3 = marshaller.unmarshall(localDateTimeStringWithoutType, Map.class);
assertEquals(String.class, converted3.get("bdate").getClass());
}

}

class Person {
private java.lang.String fullname;
private java.lang.Integer age;
private java.time.LocalDateTime dateBirth;
public Person() {
}

public java.lang.String getFullname() {
return this.fullname;
}

public void setFullname(java.lang.String fullname) {
this.fullname = fullname;
}

public java.lang.Integer getAge() {
return this.age;
}

public void setAge(java.lang.Integer age) {
this.age = age;
}

public java.time.LocalDateTime getDateBirth() {
return this.dateBirth;
}

public void setDateBirth(java.time.LocalDateTime dateBirth) {
this.dateBirth = dateBirth;
}

public Person(java.lang.String fullname, java.lang.Integer age,
java.time.LocalDateTime dateBirth) {
this.fullname = fullname;
this.age = age;
this.dateBirth = dateBirth;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -706,13 +706,21 @@ protected void appendFieldJSON(StringBuilder jsonTemplate, String type, String n
}

protected void appendFieldJSON(StringBuilder jsonTemplate, String type, FormField field, String jsType) {
jsonTemplate.append("'")
.append(field.getBinding())
.append("' : ")
.append(getJSFieldType(type))
.append(appendExtractionExpression(type, field.getBinding(), field.getId(), jsType, field.isShowTime()))
.append(wrapEndFieldType(type))
.append(",");
if ("datetime-local".equals(type)) {
jsonTemplate.append("'")
.append(field.getBinding())
.append("' : ")
.append(appendExtractionExpression(type, field.getBinding(), field.getId(), jsType, field.isShowTime()))
.append(",");
}else {
jsonTemplate.append("'")
.append(field.getBinding())
.append("' : ")
.append(getJSFieldType(type))
.append(appendExtractionExpression(type, field.getBinding(), field.getId(), jsType, field.isShowTime()))
.append(wrapEndFieldType(type))
.append(",");
}
}

protected String getJSFieldType(String type) {
Expand Down Expand Up @@ -778,10 +786,9 @@ protected String appendExtractionExpression(String type, String name, String id,
.append("')");
} else if (type.equals("datetime-local")) {
if (isShowTime) {
jsonTemplate.append("document.getElementById('")
jsonTemplate.append("getLocalDateWithTime('")
.append(id)
.append("')")
.append(getExtractionValue(jsType));
.append("') ");
} else {
jsonTemplate.append("getLocalDateWithoutTime('")
.append(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,11 +556,20 @@ function closeCreationForm(fieldId) {
currentRow = null;
}

function getFormattedLocalDateTime(jsondate) {
return {'java.time.LocalDateTime': jsondate};
}

function getLocalDateWithTime(elementId) {
var value = document.getElementById(elementId).value;
return getFormattedLocalDateTime(value);
}

function getLocalDateWithoutTime(elementId) {
var value = document.getElementById(elementId).value;
if (value === '') {
return value;
return getFormattedLocalDateTime(value);
} else {
return value + 'T00:00';
return getFormattedLocalDateTime(value + 'T00:00');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void testProcessFormRendererWithDate() {

BootstrapFormRenderer bootstrapFormRenderer = new BootstrapFormRenderer();
String outString = bootstrapFormRenderer.renderProcess("test-containerId", processAssetDesc, form);
assertThat(outString).contains("'dateBirth' : String(getLocalDateWithoutTime('field_1703386699666296E12') )");
assertThat(outString).contains("'dateBirth' : getLocalDateWithoutTime('field_1703386699666296E12') ");
assertThat(outString).contains("<input id=\"field_1703386699666296E12\" name=\"dateBirth\" type=\"date\" class=\"form-control\" value=");
}

Expand All @@ -49,7 +49,7 @@ public void testProcessFormRendererWithDateTime() {
BootstrapFormRenderer bootstrapFormRenderer = new BootstrapFormRenderer();
String outString = bootstrapFormRenderer.renderProcess("test-containerId", processAssetDesc, form);

assertThat(outString).contains("'dateBirth' : String(document.getElementById('field_1703386699666296E12').value)");
assertThat(outString).contains("'dateBirth' : getLocalDateWithTime('field_1703386699666296E12')");
assertThat(outString).contains("<input id=\"field_1703386699666296E12\" name=\"dateBirth\" type=\"datetime-local\" class=\"form-control\" value=");
}
}

0 comments on commit 5e372bd

Please sign in to comment.