Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
e61ff6d
Make json serialization treat maps as maps, always.
mikekap Apr 15, 2018
8cd92d2
remove obsolete files (#466)
elharo Sep 21, 2018
dacf78b
update app engine SDK (#467)
elharo Sep 21, 2018
19c3c7f
Add Kokoro continuous integration config and badges (#465)
chingor13 Sep 21, 2018
68fdc05
Add CODEOWNERS and issue/pr templates (#470)
chingor13 Sep 21, 2018
2cf19f6
Fix code.google.com links. Fix maven version (#471)
chingor13 Sep 21, 2018
0cd3cbe
fix API doc links (#479)
elharo Sep 24, 2018
70a8902
update test library versions (#481)
elharo Sep 24, 2018
7a1fa3b
Add the ending Java 6 support notice to README (#483)
chingor13 Sep 27, 2018
e601d7d
Fix test to run on environments with German locale (#473) (#474)
geri-m Sep 27, 2018
c1bc063
Kokoro release jobs (#472)
chingor13 Oct 4, 2018
1f067da
add patch to google http client (#486)
cramja Oct 8, 2018
4d6a42d
XML Parsing: Enum as element type (#475) (#476)
geri-m Oct 9, 2018
d27f448
Fix CODEOWNERS path
chingor13 Oct 11, 2018
85cefab
Enable releasetool for this project (#488)
chingor13 Oct 11, 2018
9de9cbb
Revert "add patch to google http client (#486)" (#493)
chingor13 Oct 16, 2018
22b8b95
Release google-http-java-client v1.26.0 (#495)
chingor13 Oct 18, 2018
df0e9f2
Bump next snapshot (#500)
chingor13 Oct 18, 2018
d504e54
Release improvements (#501)
chingor13 Oct 29, 2018
b4770b5
Add Java 11 Kokoro config (#487)
chingor13 Oct 29, 2018
d5effe8
Remove deprecated BackOffPolicy interface (#506)
chingor13 Nov 2, 2018
b3aea0f
Set the version of the jarjar-maven-plugin in pluginManagement (#515)
chingor13 Nov 2, 2018
f4c4567
guava is not provided (#508)
elharo Nov 2, 2018
0a95dd0
Upgrade maven-javadoc-plugin to 3.0.1 (#519)
chingor13 Nov 6, 2018
e1c40f6
Add google-http-client-bom artifact (#517)
chingor13 Nov 6, 2018
287cca1
Allow Enums in DataMaps (#505)
geri-m Nov 6, 2018
710117e
Fix broken snapshot and proto tests (#512)
elharo Nov 6, 2018
c5049a7
Revert "Remove deprecated BackOffPolicy interface (#506)" (#521)
chingor13 Nov 7, 2018
80b1a9c
Fix parameter of maven-javadoc-plugin (#522) (#523)
geri-m Nov 7, 2018
d7f18a3
Skip Lint of JavaDoc (#525)
geri-m Nov 8, 2018
7b95ce8
Add write timeout for post/put requests (#485)
chingor13 Nov 9, 2018
67d8c5d
Release google-http-java-client v1.27.0 (#527)
chingor13 Nov 12, 2018
7d1f23a
Bump next snapshot (#528)
chingor13 Nov 12, 2018
ab627ed
Request charset defaults to UTF-8, Response charset defaults to ISO_8…
ajaaym Dec 4, 2018
fb18555
Update guava to 26.0-android (#531)
chingor13 Dec 4, 2018
f0ca832
Make signature of com.google.api.client.util.Data#nullOf more type sa…
ajaaym Dec 10, 2018
ff93479
GenericData can now overload setters (#538)
ajaaym Dec 10, 2018
d16ab5c
Fix UriTemplate.expand to properly escape value (#534)
ajaaym Dec 10, 2018
02a6040
Fix building HttpResponseException when charset is malformed (#535)
ajaaym Dec 11, 2018
3656dcb
Implement Closeable & Flushable in JsonGenerator and JsonParser (#540)
ajaaym Dec 11, 2018
158545f
Compile to Java 1.7 binary (#542)
chingor13 Dec 12, 2018
455d4a5
Deprecate google-http-client-jackson (#539)
chingor13 Dec 12, 2018
79ed2fe
Deprecate AndroidHttp compatibility shim (#541)
chingor13 Dec 12, 2018
bf4a8da
Split http apache artifact (#543)
chingor13 Dec 12, 2018
16ddb92
Cleanup samples (#544)
chingor13 Dec 12, 2018
c7ad7fd
Merge branch 'master' into maps-can-be-iterable
chingor13 Dec 21, 2018
76eb95d
Add common generator tests for the iterable map and normal map
chingor13 Dec 28, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
XML Parsing: Enum as element type (#475) (#476)
* Allows enums as element types (#475)

* Remove Blank lines (#475)
  • Loading branch information
geri-m authored and chingor13 committed Oct 9, 2018
commit 4d6a42d3a6b3332fa8f43e379a3b07ed172d5e3e
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ private static boolean parseElementInternal(XmlPullParser parser,
@SuppressWarnings("unchecked")
Map<String, Object> destinationMap =
genericXml == null && destination instanceof Map<?, ?> ? Map.class.cast(destination) : null;

// if there is a class, we want to put the data into, create the class Info for this
ClassInfo classInfo =
destinationMap != null || destination == null ? null : ClassInfo.of(destination.getClass());
if (parser.getEventType() == XmlPullParser.START_DOCUMENT) {
Expand Down Expand Up @@ -313,7 +315,11 @@ private static boolean parseElementInternal(XmlPullParser parser,
parseNamespacesForElement(parser, namespaceDictionary);
String namespace = parser.getNamespace();
String alias = namespaceDictionary.getNamespaceAliasForUriErrorOnUnknown(namespace);

// get the "real" field name of the
String fieldName = getFieldName(false, alias, namespace, parser.getName());

// fetch the field from the classInfo
field = classInfo == null ? null : classInfo.getField(fieldName);
Type fieldType = field == null ? valueType : field.getGenericType();
fieldType = Data.resolveWildcardTypeOrTypeVariable(context, fieldType);
Expand All @@ -326,7 +332,9 @@ private static boolean parseElementInternal(XmlPullParser parser,
boolean isArray = Types.isArray(fieldType);
// text content
boolean ignore = field == null && destinationMap == null && genericXml == null;
if (ignore || Data.isPrimitive(fieldType)) {
// is the field an Enum
boolean isEnum = fieldClass != null && fieldClass.isEnum();
if (ignore || Data.isPrimitive(fieldType) || isEnum) {
int level = 1;
while (level != 0) {
switch (parser.next()) {
Expand Down Expand Up @@ -425,7 +433,8 @@ private static boolean parseElementInternal(XmlPullParser parser,
if (subFieldType instanceof ParameterizedType) {
subFieldClass = Types.getRawClass((ParameterizedType) subFieldType);
}
if (Data.isPrimitive(subFieldType)) {
boolean isSubEnum = subFieldClass != null && subFieldClass.isEnum();
if (Data.isPrimitive(subFieldType) || isSubEnum) {
elementValue = parseTextContentForElement(parser, context, false, subFieldType);
} else if (subFieldType == null || subFieldClass != null
&& Types.isAssignableToOrFrom(subFieldClass, Map.class)) {
Expand Down Expand Up @@ -501,9 +510,9 @@ private static boolean parseElementInternal(XmlPullParser parser,
isStopped = true;
break main;
}
break;
}
}
break; // break Switch;
} // end -- switch (event)
} // end -- main: while (true)
arrayValueMap.setValues();
return isStopped;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ private void computeAliases(Object element, SortedSet<String> aliases) {
aliases.add(alias);
}
Class<?> valueClass = value.getClass();
if (!isAttribute && !Data.isPrimitive(valueClass)) {
if (!isAttribute && !Data.isPrimitive(valueClass) && !valueClass.isEnum() ) {
if (value instanceof Iterable<?> || valueClass.isArray()) {
for (Object subValue : Types.iterableOf(value)) {
computeAliases(subValue, aliases);
Expand Down Expand Up @@ -328,6 +328,8 @@ class ElementSerializer {
Class<?> valueClass = elementValue.getClass();
if (Data.isPrimitive(valueClass) && !Data.isNull(elementValue)) {
textValue = elementValue;
} else if (valueClass.isEnum() && !Data.isNull(elementValue)){
textValue = elementValue;
} else {
for (Map.Entry<String, Object> entry : Data.mapOf(elementValue).entrySet()) {
Object fieldValue = entry.getValue();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.google.api.client.xml;

import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import com.google.api.client.util.Key;
import com.google.api.client.util.Value;
import junit.framework.TestCase;

public class XmlEnumTest extends TestCase {

public enum AnyEnum {
@Value ENUM_1,
@Value ENUM_2
}

public static class AnyType {
@Key("@attr")
public Object attr;
@Key
public Object elem;
@Key
public Object rep;
@Key("@anyEnum")
public XmlEnumTest.AnyEnum anyEnum;
@Key
public XmlEnumTest.AnyEnum anotherEnum;
@Key
public ValueType value;
}

public static class AnyTypeEnumElementOnly {
@Key
public XmlEnumTest.AnyEnum elementEnum;
}

public static class AnyTypeEnumAttributeOnly {
@Key("@attributeEnum")
public XmlEnumTest.AnyEnum attributeEnum;
}

public static class ValueType {
@Key("text()")
public XmlEnumTest.AnyEnum content;
}

private static final String XML =
"<?xml version=\"1.0\"?><any anyEnum=\"ENUM_1\" attr=\"value\" xmlns=\"http://www.w3.org/2005/Atom\">"
+ "<anotherEnum>ENUM_2</anotherEnum><elem>content</elem><rep>rep1</rep><rep>rep2</rep><value>ENUM_1</value></any>";

private static final String XML_ENUM_ELEMENT_ONLY = "<?xml version=\"1.0\"?><any xmlns=\"http://www.w3.org/2005/Atom\"><elementEnum>ENUM_2</elementEnum></any>";

private static final String XML_ENUM_ATTRIBUTE_ONLY = "<?xml version=\"1.0\"?><any attributeEnum=\"ENUM_1\" xmlns=\"http://www.w3.org/2005/Atom\" />";

private static final String XML_ENUM_INCORRECT = "<?xml version=\"1.0\"?><any xmlns=\"http://www.w3.org/2005/Atom\"><elementEnum>ENUM_3</elementEnum></any>";


@SuppressWarnings("cast")
public void testParse_anyType() throws Exception {
AnyType xml = new AnyType();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(XML));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
assertTrue(xml.attr instanceof String);
assertTrue(xml.elem.toString(), xml.elem instanceof ArrayList<?>);
assertTrue(xml.rep.toString(), xml.rep instanceof ArrayList<?>);
assertTrue(xml.value instanceof ValueType);
assertTrue(xml.value.content instanceof XmlEnumTest.AnyEnum);
assertTrue(xml.anyEnum instanceof XmlEnumTest.AnyEnum);
assertTrue(xml.anotherEnum instanceof XmlEnumTest.AnyEnum);
assertTrue(xml.anyEnum.equals(AnyEnum.ENUM_1));
assertTrue(xml.anotherEnum.equals(AnyEnum.ENUM_2));
assertTrue(xml.value.content.equals(AnyEnum.ENUM_1));
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(XML, out.toString());
}

public void testParse_enumElementType() throws Exception {
XmlEnumTest.AnyTypeEnumElementOnly xml = new XmlEnumTest.AnyTypeEnumElementOnly();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(XML_ENUM_ELEMENT_ONLY));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
assertTrue(xml.elementEnum instanceof XmlEnumTest.AnyEnum);
assertTrue(xml.elementEnum.equals(AnyEnum.ENUM_2));
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(XML_ENUM_ELEMENT_ONLY, out.toString());
}

public void testParse_enumAttributeType() throws Exception {
XmlEnumTest.AnyTypeEnumAttributeOnly xml = new XmlEnumTest.AnyTypeEnumAttributeOnly();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(XML_ENUM_ATTRIBUTE_ONLY));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
Xml.parseElement(parser, xml, namespaceDictionary, null);
assertTrue(xml.attributeEnum instanceof XmlEnumTest.AnyEnum);
assertTrue(xml.attributeEnum.equals(AnyEnum.ENUM_1));
// serialize
XmlSerializer serializer = Xml.createSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serializer.setOutput(out, "UTF-8");
namespaceDictionary.serialize(serializer, "any", xml);
assertEquals(XML_ENUM_ATTRIBUTE_ONLY, out.toString());
}

public void testParse_enumElementTypeIncorrect() throws Exception {
XmlEnumTest.AnyTypeEnumElementOnly xml = new XmlEnumTest.AnyTypeEnumElementOnly();
XmlPullParser parser = Xml.createParser();
parser.setInput(new StringReader(XML_ENUM_INCORRECT));
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary();
try{
Xml.parseElement(parser, xml, namespaceDictionary, null);
// fail test, if there is no exception
fail();
} catch (final IllegalArgumentException e){
assertEquals("given enum name ENUM_3 not part of enumeration", e.getMessage());
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,10 @@ public static Object parsePrimitiveValue(Type type, String stringValue) {
return new BigDecimal(stringValue);
}
if (primitiveClass.isEnum()) {
if (!ClassInfo.of(primitiveClass).names.contains(stringValue)) {
throw new IllegalArgumentException(String.format("given enum name %s not part of " +
"enumeration", stringValue));
}
@SuppressWarnings({"unchecked", "rawtypes"})
Enum result = ClassInfo.of(primitiveClass).getFieldInfo(stringValue).<Enum>enumValue();
return result;
Expand Down