Skip to content

Commit f60d4b7

Browse files
esfomeadokobylynskyi
authored andcommitted
Generate Jackson annotations for interfaces #1033 #1034
1 parent 62c7ae0 commit f60d4b7

File tree

8 files changed

+25
-12
lines changed

8 files changed

+25
-12
lines changed

src/main/java/com/kobylynskyi/graphql/codegen/mapper/AnnotationsMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import graphql.language.Argument;
99
import graphql.language.Directive;
1010
import graphql.language.InputValueDefinition;
11+
import graphql.language.InterfaceTypeDefinition;
1112
import graphql.language.ListType;
1213
import graphql.language.NamedNode;
1314
import graphql.language.NonNullType;
@@ -204,7 +205,7 @@ public List<String> getAnnotationsForDirective(MappingContext mappingContext,
204205
public List<String> getJacksonTypeIdAnnotations(MappingContext mappingContext, NamedNode<?> def) {
205206
List<String> defaults = new ArrayList<>();
206207
if (Boolean.TRUE.equals(mappingContext.getGenerateJacksonTypeIdResolver())
207-
&& def instanceof UnionTypeDefinition) {
208+
&& (def instanceof UnionTypeDefinition || def instanceof InterfaceTypeDefinition)) {
208209
defaults.add("com.fasterxml.jackson.annotation.JsonTypeInfo(use = " +
209210
"com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, property = \"__typename\")");
210211
String modelPackageName = DataModelMapper.getModelPackageName(mappingContext);

src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static com.kobylynskyi.graphql.codegen.TestUtils.assertFileContainsElements;
2222
import static com.kobylynskyi.graphql.codegen.TestUtils.assertSameTrimmedContent;
2323
import static com.kobylynskyi.graphql.codegen.TestUtils.getFileByName;
24+
import static java.lang.System.lineSeparator;
2425
import static java.util.Collections.emptyList;
2526
import static java.util.Collections.singletonList;
2627
import static java.util.Collections.singletonMap;
@@ -141,11 +142,10 @@ void generate_NoPackage() throws Exception {
141142
generate("src/test/resources/schemas/test.graphqls");
142143

143144
File[] files = Objects.requireNonNull(outputBuildDir.listFiles());
144-
assertFileContainsElements(files, "Event.java", System.lineSeparator() +
145-
"/**" + System.lineSeparator() +
146-
" * An event that describes a thing that happens" + System
147-
.lineSeparator() +
148-
" */" + System.lineSeparator());
145+
assertFileContainsElements(files, "Event.java", lineSeparator() +
146+
"/**" + lineSeparator() +
147+
" * An event that describes a thing that happens" + lineSeparator() +
148+
" */" + lineSeparator());
149149
}
150150

151151
@Test

src/test/resources/expected-classes/jackson-resolver-union/ResultObject.java.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ package com.kobylynskyi.graphql.unionresolver;
55
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
66
date = "2020-12-31T23:59:59-0500"
77
)
8+
@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, property = "__typename")
9+
@com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver(com.kobylynskyi.graphql.unionresolver.GraphqlJacksonTypeIdResolver.class)
810
public interface ResultObject {
911

1012
java.util.List<? extends UnionToResolve> getList();
1113

12-
}
14+
}

src/test/resources/expected-classes/jackson-resolver-union/without-model-package/MyResultObjectSuffix.java.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
33
date = "2020-12-31T23:59:59-0500"
44
)
5+
@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, property = "__typename")
6+
@com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver(GraphqlJacksonTypeIdResolver.class)
57
public interface MyResultObjectSuffix {
68

79
java.util.List<? extends MyUnionToResolveSuffix> getList();
810

9-
}
11+
}

src/test/resources/expected-classes/kt/jackson-resolver-union/ResultObject.kt.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ package com.kobylynskyi.graphql.unionresolver
55
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
66
date = "2020-12-31T23:59:59-0500"
77
)
8+
@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, property = "__typename")
9+
@com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver(com.kobylynskyi.graphql.unionresolver.GraphqlJacksonTypeIdResolver::class)
810
interface ResultObject {
911

1012
val list: List<out UnionToResolve>?
1113

12-
}
14+
}

src/test/resources/expected-classes/kt/jackson-resolver-union/without-model-package/MyResultObjectSuffix.kt.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
33
date = "2020-12-31T23:59:59-0500"
44
)
5+
@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, property = "__typename")
6+
@com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver(GraphqlJacksonTypeIdResolver::class)
57
interface MyResultObjectSuffix {
68

79
val list: List<out MyUnionToResolveSuffix>?
810

9-
}
11+
}

src/test/resources/expected-classes/scala/jackson-resolver-union/ResultObject.scala.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ package com.kobylynskyi.graphql.unionresolver
55
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
66
date = "2020-12-31T23:59:59-0500"
77
)
8+
@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, property = "__typename")
9+
@com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver(classOf[com.kobylynskyi.graphql.unionresolver.GraphqlJacksonTypeIdResolver])
810
trait ResultObject {
911

1012
val list: scala.Seq[_ <: UnionToResolve]
1113

12-
}
14+
}

src/test/resources/expected-classes/scala/jackson-resolver-union/without-model-package/MyResultObjectSuffix.scala.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
33
date = "2020-12-31T23:59:59-0500"
44
)
5+
@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, property = "__typename")
6+
@com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver(classOf[GraphqlJacksonTypeIdResolver])
57
trait MyResultObjectSuffix {
68

79
val list: scala.Seq[_ <: MyUnionToResolveSuffix]
810

9-
}
11+
}

0 commit comments

Comments
 (0)