lukecwik commented on a change in pull request #15338:
URL: https://github.com/apache/beam/pull/15338#discussion_r691583028
##########
File path:
sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionsFactory.java
##########
@@ -1572,6 +1601,80 @@ public int compare(Method o1, Method o2) {
return builder.build();
}
+ private static JsonDeserializer<Object> deserializerForMethod(Method method,
JavaType type)
+ throws JsonMappingException {
+ 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);
+
+ Object maybeDeserializerClass =
+
DESERIALIZATION_CONTEXT.getAnnotationIntrospector().findDeserializer(annotatedMethod);
+
+ JsonDeserializer<Object> jsonDeserializer =
+ DESERIALIZATION_CONTEXT.deserializerInstance(annotatedMethod,
maybeDeserializerClass);
+
+ if (jsonDeserializer == null) {
+ jsonDeserializer =
DESERIALIZATION_CONTEXT.findContextualValueDeserializer(type, prop);
+ }
+ return jsonDeserializer;
+ }
+
+ /**
+ * 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.
Review comment:
```suggestion
* used, otherwise the default ObjectMapper deserialization strategy is
used.
```
##########
File path:
sdks/java/core/src/test/java/org/apache/beam/sdk/options/PipelineOptionsFactoryTest.java
##########
@@ -2080,6 +2082,93 @@ static String myStaticMethod(OptionsWithStaticMethod o) {
}
}
+ public static class SimpleParsedObject {
+ public String value;
+
+ public SimpleParsedObject(String value) {
+ this.value = value;
+ }
+ }
+
+ public interface OptionsWithParsing extends PipelineOptions {
+ SimpleParsedObject getSimple();
+
+ void setSimple(SimpleParsedObject value);
+ }
+
+ @Test
+ public void testAutoQuoteStringArgumentsForComplexObjects() {
+ OptionsWithParsing options =
+
PipelineOptionsFactory.fromArgs("--simple=test").as(OptionsWithParsing.class);
+
+ assertEquals("test", options.getSimple().value);
+ }
+
+ public static class ComplexType2 {
+ public String value;
+ }
+
+ public interface OptionsWithJsonDeserialize1 extends PipelineOptions {
+ @JsonDeserialize(using = ComplexType2Deserializer1.class)
+ ComplexType2 getComplexType();
+
+ void setComplexType(ComplexType2 value);
+ }
+
+ public interface OptionsWithJsonDeserialize2 extends PipelineOptions {
+ @JsonDeserialize(using = ComplexType2Deserializer2.class)
+ ComplexType2 getComplexType();
+
+ void setComplexType(ComplexType2 value);
+ }
+
+ public static class ComplexType2Deserializer1 extends
StdDeserializer<ComplexType2> {
+ public ComplexType2Deserializer1() {
+ super(ComplexType2.class);
+ }
+
+ @Override
+ public ComplexType2 deserialize(JsonParser p, DeserializationContext ctxt)
+ throws IOException, JsonProcessingException {
+ ComplexType2 ct = new ComplexType2();
+ ct.value = p.getText();
+ return ct;
+ }
+ }
+
+ public static class ComplexType2Deserializer2 extends
StdDeserializer<ComplexType2> {
+ public ComplexType2Deserializer2() {
+ super(ComplexType2.class);
+ }
+
+ @Override
+ public ComplexType2 deserialize(JsonParser p, DeserializationContext ctxt)
+ throws IOException, JsonProcessingException {
+ ComplexType2 ct = new ComplexType2();
+ ct.value = p.getText();
+ return ct;
+ }
+ }
+
+ @Test
+ public void testJsonDeserializeAttribute_NoConflict() {
+ OptionsWithJsonDeserialize1 options =
+
PipelineOptionsFactory.fromArgs("--complexType=test").as(OptionsWithJsonDeserialize1.class);
+
+ assertEquals("test", options.getComplexType().value);
+ }
+
+ @Test
+ public void testJsonDeserializeAttribute_Conflict() {
+ OptionsWithJsonDeserialize1 options =
+
PipelineOptionsFactory.fromArgs("--complexType=test").as(OptionsWithJsonDeserialize1.class);
+
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage(
Review comment:
`ExpectedException` is deprecated, please use `assertThrows(..., lambda
that throws exception);`
##########
File path:
sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionsFactory.java
##########
@@ -1572,6 +1601,80 @@ public int compare(Method o1, Method o2) {
return builder.build();
}
+ private static JsonDeserializer<Object> deserializerForMethod(Method method,
JavaType type)
Review comment:
nit: method comment
--
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]