Skip to content

Commit

Permalink
Encode/decode fields in super classes
Browse files Browse the repository at this point in the history
  • Loading branch information
pnwpedro committed Sep 4, 2024
1 parent dddfdf6 commit 0579726
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/main/java/com/fauna/codec/codecs/ClassCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -40,7 +41,12 @@ public ClassCodec(Class<T> ty, CodecProvider provider) {
List<FieldInfo> fieldsList = new ArrayList<>();
Map<String, FieldInfo> byNameMap = new HashMap<>();

for (Field field : ((Class<?>) ty).getDeclaredFields()) {
List<Field> fields = new ArrayList<>();
for (Class<?> c = type; c != null; c = c.getSuperclass()) {
fields.addAll(Arrays.asList(c.getDeclaredFields()));
}

for (Field field : fields) {
if (field.getAnnotation(FaunaIgnore.class) != null) {
continue;
}
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/com/fauna/beans/ClassWithInheritance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.fauna.beans;

import java.util.Objects;

public class ClassWithInheritance extends ClassWithAttributes {


public ClassWithInheritance(String firstName, String lastName, int age) {
super(firstName, lastName, age);
}

public ClassWithInheritance() {
}

@Override
public boolean equals(Object o) {
if (this == o) return true;

if (o == null) return false;

if (getClass() != o.getClass()) return false;

ClassWithInheritance c = (ClassWithInheritance) o;

return Objects.equals(getFirstName(), c.getFirstName())
&& Objects.equals(getLastName(), c.getLastName())
&& Objects.equals(getAge(), c.getAge());
}

@Override
public int hashCode() {
return Objects.hash(getFirstName(), getLastName(), getAge());
}
}
34 changes: 34 additions & 0 deletions src/test/java/com/fauna/beans/ClassWithInheritanceL2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.fauna.beans;

import java.util.Objects;

public class ClassWithInheritanceL2 extends ClassWithInheritance {


public ClassWithInheritanceL2(String firstName, String lastName, int age) {
super(firstName, lastName, age);
}

public ClassWithInheritanceL2() {
}

@Override
public boolean equals(Object o) {
if (this == o) return true;

if (o == null) return false;

if (getClass() != o.getClass()) return false;

ClassWithInheritanceL2 c = (ClassWithInheritanceL2) o;

return Objects.equals(getFirstName(), c.getFirstName())
&& Objects.equals(getLastName(), c.getLastName())
&& Objects.equals(getAge(), c.getAge());
}

@Override
public int hashCode() {
return Objects.hash(getFirstName(), getLastName(), getAge());
}
}
11 changes: 9 additions & 2 deletions src/test/java/com/fauna/codec/codecs/ClassCodecTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fauna.codec.codecs;

import com.fauna.beans.ClassWithClientGeneratedIdCollTsAnnotations;
import com.fauna.beans.ClassWithInheritanceL2;
import com.fauna.beans.ClassWithFaunaIgnore;
import com.fauna.beans.ClassWithIdCollTsAnnotations;
import com.fauna.beans.ClassWithParameterizedFields;
Expand All @@ -10,6 +11,7 @@
import com.fauna.codec.DefaultCodecProvider;
import com.fauna.exception.NullDocumentException;
import com.fauna.types.Module;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -18,7 +20,6 @@
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import static com.fauna.codec.codecs.Fixtures.ESCAPED_OBJECT_WIRE_WITH;
Expand Down Expand Up @@ -83,6 +84,12 @@ public static Stream<Arguments> testCases() {
@MethodSource("testCases")
public <T,E extends Exception> void class_runTestCases(TestType testType, Codec<T> codec, String wire, Object obj, E exception) throws IOException {
runCase(testType, codec, wire, obj, exception);

}
@Test
public void class_roundTripWithInheritance() throws IOException {
var codec = DefaultCodecProvider.SINGLETON.get(ClassWithInheritanceL2.class);
var wire = "{\"first_name\":\"foo\",\"last_name\":\"bar\",\"age\":{\"@int\":\"42\"}}";
var obj = new ClassWithInheritanceL2("foo","bar",42);
runCase(TestType.RoundTrip, codec, wire, obj, null);
}
}

0 comments on commit 0579726

Please sign in to comment.