diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/GraphQLTypeMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/GraphQLTypeMapper.java index 7776aa78b..303eb2212 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/GraphQLTypeMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/GraphQLTypeMapper.java @@ -272,7 +272,7 @@ default List getAnnotations(MappingContext mappingContext, Type type, default List getAnnotations(MappingContext mappingContext, ExtendedDefinition extendedDefinition) { NamedNode def = extendedDefinition != null ? extendedDefinition.getDefinition() : null; return getAnnotations(mappingContext, extendedDefinition.getName(), extendedDefinition.getName(), null, - Collections.emptyList(), false, def); + extendedDefinition.getDirectives(), false, def); } default List getAnnotations(MappingContext mappingContext, String name) { diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/model/definitions/ExtendedDefinition.java b/src/main/java/com/kobylynskyi/graphql/codegen/model/definitions/ExtendedDefinition.java index d04de0352..176e8695b 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/model/definitions/ExtendedDefinition.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/model/definitions/ExtendedDefinition.java @@ -115,6 +115,26 @@ public List getDirectiveNames() { return directives; } + /** + * Return all directives for this definition + * + * @return list of directive names + */ + public List getDirectives() { + List directives = new ArrayList<>(); + if (this.definition instanceof DirectivesContainer) { + List definitionDirectives = ((DirectivesContainer) this.definition).getDirectives(); + if (!Utils.isEmpty(definitionDirectives)) { + directives.addAll(definitionDirectives); + } + this.extensions.stream().filter(Objects::nonNull) + .map(DirectivesContainer.class::cast) + .map(DirectivesContainer::getDirectives).filter(Objects::nonNull) + .forEach(ds -> ds.forEach(d -> directives.add(((Directive) d)))); + } + return directives; + } + public T getDefinition() { return definition; } diff --git a/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenAnnotationsTest.java b/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenAnnotationsTest.java index 9965572f7..92443742e 100644 --- a/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenAnnotationsTest.java +++ b/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenAnnotationsTest.java @@ -209,6 +209,7 @@ void generate_Directives() throws Exception { "int={{int}}, " + "n={{n?toString}})")); directiveAnnotationsMapping.put("valid", singletonList("@javax.validation.Valid")); + directiveAnnotationsMapping.put("customResolver", singletonList("@com.example.CustomAnnotation")); mappingConfig.setDirectiveAnnotationsMapping(directiveAnnotationsMapping); new JavaGraphQLCodegen(singletonList("src/test/resources/schemas/test.graphqls"), @@ -221,6 +222,9 @@ void generate_Directives() throws Exception { assertSameTrimmedContent( new File("src/test/resources/expected-classes/annotation/MutationResolver.java.txt"), getFileByName(files, "MutationResolver.java")); + assertSameTrimmedContent( + new File("src/test/resources/expected-classes/annotation/EventProperty.java.txt"), + getFileByName(files, "EventProperty.java")); } } diff --git a/src/test/resources/expected-classes/annotation/EventProperty.java.txt b/src/test/resources/expected-classes/annotation/EventProperty.java.txt new file mode 100644 index 000000000..014aedb06 --- /dev/null +++ b/src/test/resources/expected-classes/annotation/EventProperty.java.txt @@ -0,0 +1,192 @@ +package com.kobylynskyi.graphql.test1; + + +/** + * An event property have all possible types + */ +@javax.annotation.Generated( + value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen", + date = "2020-12-31T23:59:59-0500" +) +@com.example.CustomAnnotation +public class EventProperty implements java.io.Serializable { + + private Double floatVal; + private Boolean booleanVal; + private int intVal; + private java.util.List intVals; + private String stringVal; + private java.util.List child; + private Event parent; + + public EventProperty() { + } + + public EventProperty(Double floatVal, Boolean booleanVal, int intVal, java.util.List intVals, String stringVal, java.util.List child, Event parent) { + this.floatVal = floatVal; + this.booleanVal = booleanVal; + this.intVal = intVal; + this.intVals = intVals; + this.stringVal = stringVal; + this.child = child; + this.parent = parent; + } + + /** + * Float property + * with multiline comment + */ + public Double getFloatVal() { + return floatVal; + } + /** + * Float property + * with multiline comment + */ + public void setFloatVal(Double floatVal) { + this.floatVal = floatVal; + } + + public Boolean getBooleanVal() { + return booleanVal; + } + public void setBooleanVal(Boolean booleanVal) { + this.booleanVal = booleanVal; + } + + public int getIntVal() { + return intVal; + } + public void setIntVal(int intVal) { + this.intVal = intVal; + } + + /** + * primitive should not be generated + */ + public java.util.List getIntVals() { + return intVals; + } + /** + * primitive should not be generated + */ + public void setIntVals(java.util.List intVals) { + this.intVals = intVals; + } + + /** + * String comment + */ + public String getStringVal() { + return stringVal; + } + /** + * String comment + */ + public void setStringVal(String stringVal) { + this.stringVal = stringVal; + } + + /** + * Properties + */ + public java.util.List getChild() { + return child; + } + /** + * Properties + */ + public void setChild(java.util.List child) { + this.child = child; + } + + /** + * Parent event of the property + */ + public Event getParent() { + return parent; + } + /** + * Parent event of the property + */ + public void setParent(Event parent) { + this.parent = parent; + } + + + + public static EventProperty.Builder builder() { + return new EventProperty.Builder(); + } + + public static class Builder { + + private Double floatVal; + private Boolean booleanVal; + private int intVal; + private java.util.List intVals; + private String stringVal; + private java.util.List child; + private Event parent; + + public Builder() { + } + + /** + * Float property + * with multiline comment + */ + public Builder setFloatVal(Double floatVal) { + this.floatVal = floatVal; + return this; + } + + public Builder setBooleanVal(Boolean booleanVal) { + this.booleanVal = booleanVal; + return this; + } + + public Builder setIntVal(int intVal) { + this.intVal = intVal; + return this; + } + + /** + * primitive should not be generated + */ + public Builder setIntVals(java.util.List intVals) { + this.intVals = intVals; + return this; + } + + /** + * String comment + */ + public Builder setStringVal(String stringVal) { + this.stringVal = stringVal; + return this; + } + + /** + * Properties + */ + public Builder setChild(java.util.List child) { + this.child = child; + return this; + } + + /** + * Parent event of the property + */ + public Builder setParent(Event parent) { + this.parent = parent; + return this; + } + + + public EventProperty build() { + return new EventProperty(floatVal, booleanVal, intVal, intVals, stringVal, child, parent); + } + + } +} \ No newline at end of file