Skip to content

Commit

Permalink
[incubator-kie-issues-1105] Polymorphic json typing causes long delay…
Browse files Browse the repository at this point in the history
…s in first request
  • Loading branch information
elguardian committed Apr 22, 2024
1 parent 16a50c2 commit 7b270d4
Show file tree
Hide file tree
Showing 2,109 changed files with 73,998 additions and 21 deletions.
22 changes: 14 additions & 8 deletions kie-server-parent/kie-server-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@
</properties>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.32</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<!-- core dependencies -->
<!-- core dependencies -->
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-api</artifactId>
Expand All @@ -56,7 +62,7 @@
<artifactId>kie-soup-xstream</artifactId>
</dependency>

<!-- we need to find a way to remove the following 2 dependencies from this jar -->
<!-- we need to find a way to remove the following 2 dependencies from this jar -->
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
Expand All @@ -74,13 +80,13 @@
<artifactId>optaplanner-persistence-jaxb</artifactId>
</dependency>

<!-- Needed only for the DMNResult, DMNContext, etc and FEELEvent so should be possible later to migrate to kie-dmn-API -->
<!-- Needed only for the DMNResult, DMNContext, etc and FEELEvent so should be possible later to migrate to kie-dmn-API -->
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-dmn-api</artifactId>
</dependency>

<!-- json -->
<!-- json -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
Expand All @@ -102,13 +108,13 @@
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

<!-- xstream -->
<!-- xstream -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
</dependency>

<!-- utils -->
<!-- utils -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down Expand Up @@ -142,7 +148,7 @@
<artifactId>jaxb-xjc</artifactId>
<scope>test</scope>
</dependency>
<!-- TRUSTY -->
<!-- TRUSTY -->
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-pmml-dependencies</artifactId>
Expand All @@ -153,7 +159,7 @@
</exclusion>
</exclusions>
</dependency>
<!-- LEGACY -->
<!-- LEGACY -->
<dependency>
<groupId>org.drools</groupId>
<artifactId>kie-pmml</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,5 @@ public class KieServerConstants {
public static final String RESET_CONTAINER_BEFORE_UPDATE = "resetBeforeUpdate";

public static final String JSON_CUSTOM_OBJECT_DESERIALIZER_CNFE_BEHAVIOR = "org.kie.server.json.customObjectDeserializerCNFEBehavior";
public static final String JSON_DEFAULT_TYPING_DESERIALIZER_ENABLED = "org.kie.server.json.defaultTyping.enabled";
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;

import org.drools.core.xml.jaxb.util.JaxbListAdapter;
import org.drools.core.xml.jaxb.util.JaxbListWrapper;
import org.drools.core.xml.jaxb.util.JaxbUnknownAdapter;
import org.kie.server.api.KieServerConstants;
import org.kie.server.api.marshalling.Marshaller;
import org.kie.server.api.marshalling.MarshallingException;
import org.kie.server.api.marshalling.MarshallingFormat;
import org.kie.server.api.marshalling.ModelWrapper;
import org.kie.server.api.model.Wrapped;
import org.kie.server.api.model.definition.QueryParam;
import org.kie.server.api.model.type.JaxbByteArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.JsonGenerator;
Expand Down Expand Up @@ -80,19 +94,6 @@
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
import org.drools.core.xml.jaxb.util.JaxbListAdapter;
import org.drools.core.xml.jaxb.util.JaxbListWrapper;
import org.drools.core.xml.jaxb.util.JaxbUnknownAdapter;
import org.kie.server.api.KieServerConstants;
import org.kie.server.api.marshalling.Marshaller;
import org.kie.server.api.marshalling.MarshallingException;
import org.kie.server.api.marshalling.MarshallingFormat;
import org.kie.server.api.marshalling.ModelWrapper;
import org.kie.server.api.model.Wrapped;
import org.kie.server.api.model.definition.QueryParam;
import org.kie.server.api.model.type.JaxbByteArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JSONMarshaller implements Marshaller {

Expand All @@ -109,6 +110,8 @@ public class JSONMarshaller implements Marshaller {


private boolean useStrictJavaBeans;
private static boolean defaultTypingEnabled = Boolean.parseBoolean(System.getProperty(KieServerConstants.JSON_DEFAULT_TYPING_DESERIALIZER_ENABLED, "true"));

private boolean fallbackClassLoaderEnabled = Boolean.parseBoolean(System.getProperty("org.kie.server.json.fallbackClassLoader.enabled", "false"));

private boolean findDeserializerFirst = Boolean.parseBoolean(System.getProperty("org.kie.server.json.findDeserializerFirst.enabled", "true"));
Expand All @@ -122,6 +125,10 @@ public enum CNFEBehavior {
EXCEPTION
}

public static Boolean isDefaultTypingActivate() {
return defaultTypingEnabled;
}

private CNFEBehavior customObjectDeserializerCNFEBehavior = getCNFEBehavior();

public static class JSONContext {
Expand Down Expand Up @@ -347,6 +354,10 @@ public TypeDeserializer buildTypeDeserializer(DeserializationConfig config, Java
typer2 = typer2.init(JsonTypeInfo.Id.CLASS, null);
typer2 = typer2.inclusion(JsonTypeInfo.As.WRAPPER_OBJECT);
deserializeObjectMapper.setDefaultTyping(typer2);
// this avoids the use of default typing
if(!defaultTypingEnabled) {
deserializeObjectMapper.deactivateDefaultTyping();
}

SimpleModule modDeser = new SimpleModule("custom-object-unmapper", Version.unknownVersion());
modDeser.addDeserializer(Object.class, new CustomObjectDeserializer(classes));
Expand Down Expand Up @@ -1155,4 +1166,6 @@ public void setClassLoader(ClassLoader classLoader) {
public ClassLoader getClassLoader() {
return classLoader;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.manyrules;

import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
import java.util.stream.Collectors;

import org.drools.core.util.IoUtils;
import org.junit.Test;
import org.kie.server.api.marshalling.json.JSONMarshaller;
import org.kie.server.api.marshalling.test.model.Fact;
import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assume.assumeThat;

public class JSONMarshallerLargeTest {

private Logger logger = LoggerFactory.getLogger(JSONMarshallerLargeTest.class);

@Test(timeout=5000L)
public void testLargeNumberOfClasses() throws IOException {
assumeThat(JSONMarshaller.isDefaultTypingActivate(), is(false));
try (InputStream is = JSONMarshallerLargeTest.class.getClassLoader().getResourceAsStream("complex_payload.json")) {
String content = new String(IoUtils.readBytesFromInputStream(is));

Reflections reflections = new Reflections("org.kie.server.api.marshalling.test.model", new SubTypesScanner(false));
Set<Class<?>> clazzes = reflections.getSubTypesOf(Object.class).stream().collect(Collectors.toSet());
clazzes.remove(Fact.class);

JSONMarshaller marshaller = new JSONMarshaller(clazzes, JSONMarshallerLargeTest.class.getClassLoader());
Fact object = marshaller.unmarshall(content, Fact.class);
assertThat(object).isNotNull();
logger.info("object captured {}", object);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.kie.server.api.marshalling.test.model;

public class Fact implements java.io.Serializable {

private Object object;

public Object getObject() {
return object;
}

public void setObject(Object object) {
this.object = object;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.kie.server.api.marshalling.test.model.dummy.package1;

import lombok.Data;
import javax.xml.bind.annotation.*;

import java.io.Serializable;

@SuppressWarnings("java:S1068")
@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "DummyData1")
public class DummyData1 implements Serializable {

private static final long serialVersionUID = 5174127032991892033L;

@XmlAttribute(name = "dummyString")
private String dummyString;
private int dummyInt;
@XmlElement(required = true)
private Object dummyObj;

public DummyData1() {
}

public DummyData1(int dummyInt, Object dummyObj) {
this.dummyInt = dummyInt;
this.dummyObj = dummyObj;
}

public DummyData1(int dummyInt, String dummyString, Object dummyObj) {
this.dummyInt = dummyInt;
this.dummyString = dummyString;
this.dummyObj = dummyObj;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.kie.server.api.marshalling.test.model.dummy.package1;

import lombok.Data;
import javax.xml.bind.annotation.*;

import java.io.Serializable;

@SuppressWarnings("java:S1068")
@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "DummyData10")
public class DummyData10 implements Serializable {

private static final long serialVersionUID = 5174127032991892033L;

@XmlAttribute(name = "dummyString")
private String dummyString;
private int dummyInt;
@XmlElement(required = true)
private Object dummyObj;

public DummyData10() {
}

public DummyData10(int dummyInt, Object dummyObj) {
this.dummyInt = dummyInt;
this.dummyObj = dummyObj;
}

public DummyData10(int dummyInt, String dummyString, Object dummyObj) {
this.dummyInt = dummyInt;
this.dummyString = dummyString;
this.dummyObj = dummyObj;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.kie.server.api.marshalling.test.model.dummy.package1;

import lombok.Data;
import javax.xml.bind.annotation.*;

import java.io.Serializable;

@SuppressWarnings("java:S1068")
@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "DummyData100")
public class DummyData100 implements Serializable {

private static final long serialVersionUID = 5174127032991892033L;

@XmlAttribute(name = "dummyString")
private String dummyString;
private int dummyInt;
@XmlElement(required = true)
private Object dummyObj;

public DummyData100() {
}

public DummyData100(int dummyInt, Object dummyObj) {
this.dummyInt = dummyInt;
this.dummyObj = dummyObj;
}

public DummyData100(int dummyInt, String dummyString, Object dummyObj) {
this.dummyInt = dummyInt;
this.dummyString = dummyString;
this.dummyObj = dummyObj;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.kie.server.api.marshalling.test.model.dummy.package1;

import lombok.Data;
import javax.xml.bind.annotation.*;

import java.io.Serializable;

@SuppressWarnings("java:S1068")
@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "DummyData11")
public class DummyData11 implements Serializable {

private static final long serialVersionUID = 5174127032991892033L;

@XmlAttribute(name = "dummyString")
private String dummyString;
private int dummyInt;
@XmlElement(required = true)
private Object dummyObj;

public DummyData11() {
}

public DummyData11(int dummyInt, Object dummyObj) {
this.dummyInt = dummyInt;
this.dummyObj = dummyObj;
}

public DummyData11(int dummyInt, String dummyString, Object dummyObj) {
this.dummyInt = dummyInt;
this.dummyString = dummyString;
this.dummyObj = dummyObj;
}
}
Loading

0 comments on commit 7b270d4

Please sign in to comment.