Skip to content
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

ignore overridden setters and getters in JavaBeanSchema #33052

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.beam.sdk.schemas.annotations.SchemaCaseFormat;
import org.apache.beam.sdk.schemas.annotations.SchemaFieldName;
Expand Down Expand Up @@ -58,9 +60,11 @@ public static class GetterTypeSupplier implements FieldValueTypeSupplier {

@Override
public List<FieldValueTypeInformation> get(TypeDescriptor<?> typeDescriptor) {
Set<String> gettersSeen = new HashSet<>();
List<Method> methods =
ReflectUtils.getMethods(typeDescriptor.getRawType()).stream()
.filter(ReflectUtils::isGetter)
.filter(m -> gettersSeen.add(m.getName()))
.filter(m -> !m.isAnnotationPresent(SchemaIgnore.class))
.collect(Collectors.toList());
List<FieldValueTypeInformation> types = Lists.newArrayListWithCapacity(methods.size());
Expand Down Expand Up @@ -108,8 +112,10 @@ public static class SetterTypeSupplier implements FieldValueTypeSupplier {

@Override
public List<FieldValueTypeInformation> get(TypeDescriptor<?> typeDescriptor) {
Set<String> settersSeen = new HashSet<>();
return ReflectUtils.getMethods(typeDescriptor.getRawType()).stream()
.filter(ReflectUtils::isSetter)
.filter(m -> settersSeen.add(m.getName()))
.filter(m -> !m.isAnnotationPresent(SchemaIgnore.class))
.map(m -> FieldValueTypeInformation.forSetter(typeDescriptor, m))
.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,4 +625,11 @@ public void testSetterConstructionWithRenamedFields() throws NoSuchSchemaExcepti
assertEquals(
registry.getFromRowFunction(BeanWithCaseFormat.class).apply(row), beanWithCaseFormat);
}

@Test
public void testSimpleBeanWithOverriddenAccessors() throws Exception {
SchemaRegistry registry = SchemaRegistry.createDefault();
Schema schema = registry.getSchema(TestJavaBeans.SimpleBeanWithOverriddenAccessors.class);
SchemaTestUtils.assertSchemaEquivalent(SIMPLE_BEAN_SCHEMA, schema);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1397,4 +1397,17 @@ public void setValue(@Nullable Float value) {
Schema.Field.nullable("value", FieldType.FLOAT)
.withDescription("This value is the value stored in the object as a float."))
.build();

@DefaultSchema(JavaBeanSchema.class)
public static class SimpleBeanWithOverriddenAccessors extends SimpleBean {
@Override
public String getStr() {
return super.getStr();
}

@Override
public void setStr(String str) {
super.setStr(str);
}
}
}
Loading