Skip to content

Commit c7e8de6

Browse files
Issue #569: excludePropertyAnnotations not respected when using gson (#570)
* Issue #569: excludePropertyAnnotations not respected when using gson The excludePropertyAnnotations property is not respected when using gson as JsonLibrary. * Configured an exclusion strategy on the GsonParser to fix the issue * Adjusted the IncludeExcludePropertyTest to reveal the issue and check the fix. * Fixed checkstyle warnings
1 parent 13fa083 commit c7e8de6

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/parser/GsonParser.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cz.habarta.typescript.generator.parser;
22

3+
import com.google.gson.ExclusionStrategy;
4+
import com.google.gson.FieldAttributes;
35
import com.google.gson.Gson;
46
import com.google.gson.GsonBuilder;
57
import com.google.gson.annotations.SerializedName;
@@ -50,6 +52,17 @@ public GsonParser(Settings settings, TypeProcessor commonTypeProcessor,
5052
: Modifier.STATIC | Modifier.TRANSIENT;
5153
this.gson = new GsonBuilder()
5254
.excludeFieldsWithModifiers(modifiers)
55+
.setExclusionStrategies(new ExclusionStrategy() {
56+
@Override
57+
public boolean shouldSkipField(FieldAttributes fieldAttributes) {
58+
return !isAnnotatedPropertyIncluded(fieldAttributes::getAnnotation, fieldAttributes.getDeclaringClass().getName() + "." + fieldAttributes.getName());
59+
}
60+
61+
@Override
62+
public boolean shouldSkipClass(Class<?> aClass) {
63+
return false;
64+
}
65+
})
5366
.create();
5467
}
5568

typescript-generator-core/src/test/java/cz/habarta/typescript/generator/IncludeExcludePropertyTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,37 @@
44
import java.lang.annotation.Retention;
55
import java.lang.annotation.RetentionPolicy;
66
import java.util.Arrays;
7+
import java.util.Collection;
8+
import java.util.stream.Collectors;
79
import org.junit.Assert;
810
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.junit.runners.Parameterized;
913

1014

1115
@SuppressWarnings("unused")
16+
@RunWith(Parameterized.class)
1217
public class IncludeExcludePropertyTest {
1318

19+
@Parameterized.Parameters(name = "{index} - {0}")
20+
public static Collection<Object[]> data() {
21+
return Arrays.stream(JsonLibrary.values())
22+
.filter(library -> library != JsonLibrary.jackson1 && library != JsonLibrary.jsonb)
23+
.map(library -> new Object[]{library})
24+
.collect(Collectors.toList());
25+
}
26+
27+
private final JsonLibrary library;
28+
29+
public IncludeExcludePropertyTest(JsonLibrary library) {
30+
this.library = library;
31+
}
32+
33+
1434
@Test
1535
public void testInclude() {
1636
final Settings settings = TestUtils.settings();
37+
settings.jsonLibrary = library;
1738
settings.includePropertyAnnotations = Arrays.asList(MyInclude.class);
1839
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(ClassWithAnnotatedProperties.class));
1940
Assert.assertTrue(!output.contains("property1"));
@@ -25,6 +46,7 @@ public void testInclude() {
2546
@Test
2647
public void testExclude() {
2748
final Settings settings = TestUtils.settings();
49+
settings.jsonLibrary = library;
2850
settings.excludePropertyAnnotations = Arrays.asList(MyExclude.class);
2951
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(ClassWithAnnotatedProperties.class));
3052
Assert.assertTrue(output.contains("property1"));
@@ -36,6 +58,8 @@ public void testExclude() {
3658
@Test
3759
public void testBoth() {
3860
final Settings settings = TestUtils.settings();
61+
settings.jsonLibrary = library;
62+
3963
settings.includePropertyAnnotations = Arrays.asList(MyInclude.class);
4064
settings.excludePropertyAnnotations = Arrays.asList(MyExclude.class);
4165
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(ClassWithAnnotatedProperties.class));

0 commit comments

Comments
 (0)