-
Notifications
You must be signed in to change notification settings - Fork 572
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
[FIX] custom fix when xml adapter has names #3052
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,8 @@ | |
import java.util.regex.Pattern; | ||
|
||
import javax.xml.bind.annotation.XmlAnyElement; | ||
import javax.xml.bind.annotation.XmlElement; | ||
import javax.xml.bind.annotation.XmlElements; | ||
import javax.xml.bind.annotation.adapters.XmlAdapter; | ||
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; | ||
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters; | ||
|
@@ -99,15 +101,15 @@ public class JSONMarshaller implements Marshaller { | |
private static final Logger logger = LoggerFactory.getLogger(JSONMarshaller.class); | ||
|
||
private static final boolean STRICT_ID_FORMAT = Boolean.getBoolean(KieServerConstants.KIE_SERVER_STRICT_ID_FORMAT); | ||
private boolean jsonHandleXmlAnyElementsNames = Boolean.getBoolean(KieServerConstants.JSON_HANDLE_XML_ANY_ELEMENTS_NAMES); | ||
private final boolean STRICT_JAVABEANS_SERIALIZERS = Boolean.getBoolean(KieServerConstants.KIE_SERVER_STRICT_JAVABEANS_SERIALIZERS); | ||
|
||
private static final String FIELDS = "fields"; | ||
private static final String NOT_NULL = "not_null"; | ||
public static final String FIELDS = "fields"; | ||
public static final String NOT_NULL = "not_null"; | ||
|
||
private boolean formatDate; | ||
private String dateFormatStr = System.getProperty("org.kie.server.json.date_format", "yyyy-MM-dd'T'hh:mm:ss.SSSZ"); | ||
|
||
|
||
private boolean useStrictJavaBeans; | ||
private boolean fallbackClassLoaderEnabled = Boolean.parseBoolean(System.getProperty("org.kie.server.json.fallbackClassLoader.enabled", "false")); | ||
|
||
|
@@ -129,7 +131,7 @@ public static class JSONContext { | |
private boolean stripped; | ||
|
||
private boolean wrap; | ||
|
||
private boolean writeNull; | ||
|
||
public JSONContext() { | ||
|
@@ -182,8 +184,6 @@ public void setWriteNull(boolean writeNull) { | |
// Optional Marshaller Extension to handle new types | ||
private static final List<JSONMarshallerExtension> EXTENSIONS; | ||
|
||
|
||
|
||
// Load Marshaller Extension | ||
static { | ||
logger.info("Marshaller extensions init"); | ||
|
@@ -224,7 +224,7 @@ private static CNFEBehavior getCNFEBehavior() { | |
return CNFEBehavior.valueOf(cnfeBehaviorValue); | ||
} catch (IllegalArgumentException iae) { | ||
throw new MarshallingException(cnfeBehaviorValue + " is not supported for " + KieServerConstants.JSON_CUSTOM_OBJECT_DESERIALIZER_CNFE_BEHAVIOR + | ||
". Please choose from " + Arrays.asList(CNFEBehavior.values()).toString(), iae); | ||
". Please choose from " + Arrays.asList(CNFEBehavior.values()).toString(), iae); | ||
} | ||
} | ||
|
||
|
@@ -301,6 +301,7 @@ public boolean useForType(JavaType t) { | |
} | ||
return false; | ||
} | ||
|
||
}; | ||
typer = typer.init(JsonTypeInfo.Id.CLASS, null); | ||
typer = typer.inclusion(JsonTypeInfo.As.WRAPPER_OBJECT); | ||
|
@@ -403,8 +404,7 @@ public String marshall(Object input, Map<String, Object> parameters) { | |
if (parameters.containsKey(MARSHALLER_PARAMETER_STRICT)) { | ||
jsonContext.get().setWrap(Boolean.parseBoolean((String) parameters.get(MARSHALLER_PARAMETER_STRICT))); | ||
} | ||
if (NOT_NULL.equals(parameters.get(FIELDS))) | ||
{ | ||
if (NOT_NULL.equals(parameters.get(FIELDS))) { | ||
jsonContext.get().setWriteNull(false); | ||
} | ||
return marshall(input); | ||
|
@@ -416,7 +416,7 @@ public String marshall(Object input, Map<String, Object> parameters) { | |
@Override | ||
public String marshall(Object objectInput) { | ||
try { | ||
return getMapper(objectMapper,notNullObjectMapper).writeValueAsString(wrap(objectInput)); | ||
return getMapper(objectMapper, notNullObjectMapper).writeValueAsString(wrap(objectInput)); | ||
} catch (IOException e) { | ||
throw new MarshallingException("Error marshalling input", e); | ||
} | ||
|
@@ -432,7 +432,6 @@ public byte[] marshallAsBytes(Object objectInput) { | |
|
||
} | ||
|
||
|
||
@Override | ||
public <T> T unmarshall(String serializedInput, Class<T> type) { | ||
|
||
|
@@ -503,20 +502,34 @@ public ExtendedJaxbAnnotationIntrospector(List<NamedType> customClasses, ObjectM | |
|
||
@Override | ||
public List<NamedType> findSubtypes(Annotated a) { | ||
List<NamedType> base = super.findSubtypes(a); | ||
|
||
List<NamedType> complete = new ArrayList<NamedType>(); | ||
if (base != null) { | ||
complete.addAll(base); | ||
} | ||
if (customClasses != null) { | ||
for (NamedType namedType : customClasses) { | ||
for (NamedType namedType : customClasses) { | ||
Class<?> clazz = namedType.getType(); | ||
if (!a.getRawType().equals(clazz) && a.getRawType().isAssignableFrom(clazz)) { | ||
complete.add(namedType); | ||
} | ||
} | ||
} | ||
|
||
XmlElements elements = findAnnotation(XmlElements.class, a, false, false, false); | ||
if (elements != null) { | ||
for (XmlElement elem : elements.value()) { | ||
if (!jsonHandleXmlAnyElementsNames && customClasses.contains(new NamedType(elem.type(), elem.type().getSimpleName()))) { | ||
continue; | ||
} | ||
String name = elem.name(); | ||
if (MARKER_FOR_DEFAULT.equals(name)) { | ||
name = null; | ||
} | ||
complete.add(new NamedType(elem.type(), name)); | ||
} | ||
} else { | ||
List<NamedType> base = super.findSubtypes(a); | ||
if (base != null) { | ||
complete.addAll(base); | ||
} | ||
} | ||
return complete; | ||
} | ||
|
||
|
@@ -706,7 +719,7 @@ public CustomObjectSerializer(ObjectMapper customObjectMapper) { | |
|
||
@Override | ||
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { | ||
jgen.writeRawValue(getMapper(customObjectMapper,notNullObjectMapper).writeValueAsString(value)); | ||
jgen.writeRawValue(getMapper(customObjectMapper, notNullObjectMapper).writeValueAsString(value)); | ||
} | ||
} | ||
|
||
|
@@ -725,17 +738,17 @@ public void serialize(Object value, JsonGenerator jgen, SerializerProvider provi | |
String className = value.getClass().getName(); | ||
|
||
if (value instanceof Collection) { | ||
String collectionJson = writeCollection((Collection) value, getMapper(customObjectMapper,notNullObjectMapper)); | ||
String collectionJson = writeCollection((Collection) value, getMapper(customObjectMapper, notNullObjectMapper)); | ||
jgen.writeRawValue(collectionJson); | ||
} else if (value instanceof Map) { | ||
String mapJson = writeMap((Map) value, getMapper(customObjectMapper,notNullObjectMapper)); | ||
String mapJson = writeMap((Map) value, getMapper(customObjectMapper, notNullObjectMapper)); | ||
jgen.writeRawValue(mapJson); | ||
} else if (value instanceof Object[] || value.getClass().isArray()) { | ||
String arrayJson = writeArray((Object[]) value, getMapper(customObjectMapper,notNullObjectMapper)); | ||
String arrayJson = writeArray((Object[]) value, getMapper(customObjectMapper, notNullObjectMapper)); | ||
jgen.writeRawValue(arrayJson); | ||
} else { | ||
|
||
String json = getMapper(customObjectMapper,notNullObjectMapper).writeValueAsString(value); | ||
String json = getMapper(customObjectMapper, notNullObjectMapper).writeValueAsString(value); | ||
|
||
// don't wrap java and javax classes as they are always available, in addition avoid double wrapping | ||
if (!className.startsWith("java.") && !className.startsWith("javax.") && !json.contains(className)) { | ||
|
@@ -842,14 +855,11 @@ private String writeCollection(Collection<?> collection, ObjectMapper customObje | |
return builder.toString(); | ||
} | ||
} | ||
|
||
private ObjectMapper getMapper(ObjectMapper alwaysMapper, ObjectMapper notNullMapper) | ||
{ | ||
|
||
private ObjectMapper getMapper(ObjectMapper alwaysMapper, ObjectMapper notNullMapper) { | ||
return jsonContext.get().isWriteNull() ? alwaysMapper : notNullMapper; | ||
} | ||
|
||
|
||
|
||
class CustomObjectDeserializer extends UntypedObjectDeserializer { | ||
|
||
private final Pattern VALID_JAVA_IDENTIFIER = Pattern.compile("(\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*\\.)*\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*"); | ||
|
@@ -976,6 +986,7 @@ public Object wrapCustomObject(Map<String, Object> result) { | |
} | ||
return result; | ||
} | ||
|
||
private Object[] toArray(Object element) { | ||
if (element != null) { | ||
|
||
|
@@ -1160,4 +1171,8 @@ public void setClassLoader(ClassLoader classLoader) { | |
public ClassLoader getClassLoader() { | ||
return classLoader; | ||
} | ||
|
||
public void setXmlAnyElementsNames(boolean jsonHandleXmlAnyElementsNames) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this method used only for testing purposes? if yes... maybe there's a better way to do so.... I personally like Anonymous classes for hack the tested class... but I also understand that it might not be the most popular design choice. |
||
this.jsonHandleXmlAnyElementsNames = jsonHandleXmlAnyElementsNames; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...kie-server-api/src/test/java/org/kie/server/api/marshalling/objects/FreeFormItemType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2024 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.kie.server.api.marshalling.objects; | ||
|
||
import java.io.Serializable; | ||
|
||
import javax.xml.bind.annotation.XmlAccessType; | ||
import javax.xml.bind.annotation.XmlAccessorType; | ||
import javax.xml.bind.annotation.XmlElement; | ||
import javax.xml.bind.annotation.XmlType; | ||
|
||
|
||
@XmlAccessorType(XmlAccessType.FIELD) | ||
@XmlType(name = "FreeFormItemType", propOrder = { | ||
"itemValue" | ||
}) | ||
public class FreeFormItemType implements Serializable, Cloneable | ||
{ | ||
|
||
private final static long serialVersionUID = 1L; | ||
|
||
@XmlElement(required = true) | ||
protected String itemValue; | ||
|
||
/** | ||
* Gets the value of the itemValue property. | ||
* | ||
* @return | ||
* possible object is | ||
* {@link String } | ||
* | ||
*/ | ||
public String getItemValue() { | ||
return itemValue; | ||
} | ||
|
||
/** | ||
* Sets the value of the itemValue property. | ||
* | ||
* @param value | ||
* allowed object is | ||
* {@link String } | ||
* | ||
*/ | ||
public void setItemValue(String value) { | ||
this.itemValue = value; | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't we check jsonHandleXmlAnyElementsNames before the loop, so we can avoid unnecessary processing (I know that over time JIT will do the job... but still)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it won't give the same outcome.