lukecwik commented on a change in pull request #15338:
URL: https://github.com/apache/beam/pull/15338#discussion_r690640631
##########
File path:
sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionsFactory.java
##########
@@ -1572,6 +1588,63 @@ public int compare(Method o1, Method o2) {
return builder.build();
}
+ /**
+ * Attempt to parse an input string into an instance of `type` using an
{@link ObjectMapper}.
+ *
+ * <p>If the getter method is annotated with {@link
+ * com.fasterxml.jackson.databind.annotation.JsonDeserialize} the specified
deserializer will be
+ * used, otherwise the default ObjectMapper deserialization strategy.
+ *
+ * <p>Parsing is attempted twice, once with the raw string value. If that
attempt fails, another
+ * attempt is made by wrapping the value in quotes so that it is interpreted
as a JSON string.
+ */
+ private static Object tryParseObject(String value, JavaType type, Method
method)
+ throws IOException {
+ AnnotationCollector ac = AnnotationCollector.emptyCollector();
+ for (Annotation ann : method.getAnnotations()) {
+ ac = ac.addOrOverride(ann);
+ }
+
+ AnnotatedMethod annotatedMethod =
+ new AnnotatedMethod(
+ new TypeResolutionContext.Empty(MAPPER.getTypeFactory()),
+ method,
+ ac.asAnnotationMap(),
+ null);
+
+ BeanPropertyDefinition propDef =
+
SimpleBeanPropertyDefinition.construct(MAPPER.getDeserializationConfig(),
annotatedMethod);
+
+ BeanProperty prop =
+ new MethodProperty(
+ propDef,
+ type,
+ MAPPER.getDeserializationConfig().findTypeDeserializer(type),
+ ac.asAnnotations(),
+ annotatedMethod);
+
+ JsonNode tree;
+ try {
+ tree = MAPPER.readTree(value);
+ } catch (JsonParseException e) {
+ // try again, quoting the input string if it wasn't already
+ if (!(value.startsWith("\"") && value.endsWith("\""))) {
+ try {
+ tree = MAPPER.readTree("\"" + value + "\"");
Review comment:
Thanks, now I understand but Jackson does seem to ignore that when the
object type being converted to is a String since on
https://github.com/apache/beam/blob/2ab002486345f5d97f11ccfbeb86541796c080e0/sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionsFactory.java#L1676
it seems as to allow the conversion and there are a few tests in
PipelineOptionsFactoryTest that pass even without the quoting.
I think adding the quoting makes sense unless there is a way to fix
JsonDeserializer types to support unquoted complex objects.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]