Skip to content

support msg for deprecated #471

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

Merged
merged 5 commits into from
Dec 29, 2020
Merged
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
@@ -1,5 +1,6 @@
package com.kobylynskyi.graphql.codegen.java;

import com.kobylynskyi.graphql.codegen.mapper.DataModelMapper;
import com.kobylynskyi.graphql.codegen.mapper.GraphQLTypeMapper;
import com.kobylynskyi.graphql.codegen.mapper.ValueMapper;
import com.kobylynskyi.graphql.codegen.model.MappingContext;
Expand All @@ -9,6 +10,7 @@
import graphql.language.Argument;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static java.util.Arrays.asList;
Expand Down Expand Up @@ -87,4 +89,30 @@ public String mapDirectiveArgumentValue(MappingContext mappingContext, Argument
return valueMapper.map(mappingContext, dirArg.getValue(), null, argumentValueFormatter);
}

@Override
public NamedDefinition getLanguageType(MappingContext mappingContext, String graphQLType, String name,
String parentTypeName, boolean mandatory, boolean collection) {
Map<String, String> customTypesMapping = mappingContext.getCustomTypesMapping();
Set<String> serializeFieldsUsingObjectMapper = mappingContext.getUseObjectMapperForRequestSerialization();
String langTypeName;
boolean primitiveCanBeUsed = !collection;
boolean serializeUsingObjectMapper = false;
if (name != null && parentTypeName != null && customTypesMapping.containsKey(parentTypeName + "." + name)) {
langTypeName = customTypesMapping.get(parentTypeName + "." + name);
primitiveCanBeUsed = false;
} else if (customTypesMapping.containsKey(graphQLType)) {
langTypeName = customTypesMapping.get(graphQLType);
} else {
langTypeName = DataModelMapper.getModelClassNameWithPrefixAndSuffix(mappingContext, graphQLType);
}
if (serializeFieldsUsingObjectMapper.contains(graphQLType) ||
(name != null && parentTypeName != null &&
serializeFieldsUsingObjectMapper.contains(parentTypeName + "." + name))) {
serializeUsingObjectMapper = true;
}

return new NamedDefinition(langTypeName, graphQLType, mappingContext.getInterfacesName().contains(graphQLType),
mandatory, primitiveCanBeUsed, serializeUsingObjectMapper);
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.kobylynskyi.graphql.codegen.mapper;

import com.kobylynskyi.graphql.codegen.model.DeprecatedDefinition;
import com.kobylynskyi.graphql.codegen.model.EnumValueDefinition;
import com.kobylynskyi.graphql.codegen.model.MappingContext;
import com.kobylynskyi.graphql.codegen.model.definitions.ExtendedEnumTypeDefinition;
import com.kobylynskyi.graphql.codegen.model.definitions.ExtendedUnionTypeDefinition;
import com.kobylynskyi.graphql.codegen.utils.Utils;
import graphql.language.Comment;
import graphql.language.Directive;
import graphql.language.DirectivesContainer;

import java.util.Collections;
Expand Down Expand Up @@ -50,10 +50,8 @@ private static Set<String> getUnionInterfaces(MappingContext mappingContext,
.collect(Collectors.toSet());
}

private static boolean isDeprecated(DirectivesContainer<?> directivesContainer) {
return directivesContainer.getDirectives().stream()
.map(Directive::getName)
.anyMatch(Deprecated.class.getSimpleName()::equalsIgnoreCase);
public DeprecatedDefinition getDeprecated(MappingContext mappingContext, DirectivesContainer<?> directivesContainer) {
return graphQLTypeMapper.getDeprecated(mappingContext, directivesContainer);
}

private static List<String> getJavaDoc(graphql.language.EnumValueDefinition def) {
Expand Down Expand Up @@ -101,7 +99,7 @@ private List<EnumValueDefinition> map(MappingContext mappingContext, List<graphq
dataModelMapper.capitalizeIfRestricted(mappingContext, f.getName()),
f.getName(),
getJavaDoc(f),
isDeprecated(f)))
getDeprecated(mappingContext, f)))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private ParameterDefinition mapField(MappingContext mappingContext, ExtendedFiel
parameter.setType(graphQLTypeMapper.getTypeConsideringPrimitive(mappingContext, namedDefinition, namedDefinition.getJavaName()));
parameter.setAnnotations(graphQLTypeMapper.getAnnotations(mappingContext, fieldDef.getType(), fieldDef, parentTypeName, false));
parameter.setJavaDoc(fieldDef.getJavaDoc());
parameter.setDeprecated(fieldDef.isDeprecated());
parameter.setDeprecated(fieldDef.getDeprecated(mappingContext));
parameter.setSerializeUsingObjectMapper(namedDefinition.isSerializeUsingObjectMapper());
return parameter;
}
Expand All @@ -147,7 +147,7 @@ private ProjectionParameterDefinition mapProjectionField(MappingContext mappingC
parameter.setParametrizedInputClassName(
DataModelMapper.getParametrizedInputClassName(mappingContext, fieldDef, parentTypeDef));
}
parameter.setDeprecated(fieldDef.isDeprecated());
parameter.setDeprecated(fieldDef.getDeprecated(mappingContext));
return parameter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private OperationDefinition map(MappingContext mappingContext, ExtendedFieldDefi
operation.setAnnotations(annotations);
operation.setParameters(parameters);
operation.setJavaDoc(fieldDef.getJavaDoc());
operation.setDeprecated(fieldDef.isDeprecated());
operation.setDeprecated(fieldDef.getDeprecated(mappingContext));
operation.setThrowsException(mappingContext.getGenerateApisWithThrowsException());
return operation;
}
Expand All @@ -202,7 +202,7 @@ private List<ParameterDefinition> getOperationParameters(MappingContext mappingC
if (!Utils.isGraphqlOperation(parentTypeName)) {
String parentObjectParamType = graphQLTypeMapper.getLanguageType(mappingContext, new TypeName(parentTypeName));
String parentObjectParamName = dataModelMapper.capitalizeIfRestricted(mappingContext, Utils.uncapitalize(parentObjectParamType));
parameters.add(new ParameterDefinition(parentObjectParamType, parentObjectParamName, parentObjectParamName, null, emptyList(), emptyList(), resolvedField.isDeprecated(), false));
parameters.add(new ParameterDefinition(parentObjectParamType, parentObjectParamName, parentObjectParamName, null, emptyList(), emptyList(), resolvedField.getDeprecated(mappingContext), false));
}

// 2. Next parameters are input values
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
package com.kobylynskyi.graphql.codegen.mapper;

import com.kobylynskyi.graphql.codegen.model.GeneratedLanguage;
import com.kobylynskyi.graphql.codegen.model.DeprecatedDefinition;
import com.kobylynskyi.graphql.codegen.model.MappingContext;
import com.kobylynskyi.graphql.codegen.model.MultiLanguageDeprecated;
import com.kobylynskyi.graphql.codegen.model.NamedDefinition;
import com.kobylynskyi.graphql.codegen.model.definitions.ExtendedDefinition;
import com.kobylynskyi.graphql.codegen.utils.Utils;
import graphql.language.Argument;
import graphql.language.Directive;
import graphql.language.DirectivesContainer;
import graphql.language.ListType;
import graphql.language.NamedNode;
import graphql.language.NonNullType;
import graphql.language.Type;
import graphql.language.TypeName;
import graphql.language.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

/**
* Map GraphQL type to language-specific type (java/scala/kotlin/etc)
Expand Down Expand Up @@ -213,9 +203,7 @@ default NamedDefinition getLanguageType(MappingContext mappingContext, String gr
if (name != null && parentTypeName != null && customTypesMapping.containsKey(parentTypeName + "." + name)) {
langTypeName = customTypesMapping.get(parentTypeName + "." + name);
primitiveCanBeUsed = false;
} else if (mandatory && customTypesMapping.containsKey(getMandatoryType(graphQLType)) &&
!mappingContext.getGeneratedLanguage().equals(GeneratedLanguage.JAVA)){
//Java primitive types can't be used as generic parameters this, but Scala/Kotlin can
} else if (mandatory && customTypesMapping.containsKey(getMandatoryType(graphQLType))) {
langTypeName = customTypesMapping.get(getMandatoryType(graphQLType));
} else if (customTypesMapping.containsKey(graphQLType)) {
langTypeName = customTypesMapping.get(graphQLType);
Expand Down Expand Up @@ -330,5 +318,14 @@ default String getTypeConsideringPrimitive(MappingContext mappingContext,
return computedTypeName;
}

default DeprecatedDefinition getDeprecated(MappingContext mappingContext, DirectivesContainer<?> directivesContainer) {
return directivesContainer.getDirectives()
.stream()
.filter(d -> d.getName().equalsIgnoreCase(Deprecated.class.getSimpleName()))
.findFirst()
.map(directive -> MultiLanguageDeprecated.getLanguageDeprecated(mappingContext.getGeneratedLanguage(), directive))
.orElse(null);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private ParameterDefinition map(MappingContext mappingContext, InputValueDefinit
parameter.setType(graphQLTypeMapper.getTypeConsideringPrimitive(mappingContext, namedDefinition, namedDefinition.getJavaName()));
parameter.setDefaultValue(valueMapper.map(mappingContext, inputValueDefinition.getDefaultValue(), inputValueDefinition.getType()));
parameter.setAnnotations(graphQLTypeMapper.getAnnotations(mappingContext, inputValueDefinition.getType(), inputValueDefinition, parentTypeName, false));
parameter.setDeprecated(isDeprecated(inputValueDefinition));
parameter.setDeprecated(graphQLTypeMapper.getDeprecated(mappingContext, inputValueDefinition));
parameter.setSerializeUsingObjectMapper(namedDefinition.isSerializeUsingObjectMapper());
return parameter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public Map<String, Object> mapResponse(MappingContext mappingContext,
dataModel.put(ANNOTATIONS, graphQLTypeMapper.getAnnotations(mappingContext, className));
dataModel.put(CLASS_NAME, className);
dataModel.put(JAVA_DOC, operationDef.getJavaDoc());
dataModel.put(DEPRECATED, operationDef.isDeprecated());
dataModel.put(DEPRECATED, operationDef.getDeprecated(mappingContext));
dataModel.put(OPERATION_NAME, operationDef.getName());
dataModel.put(METHOD_NAME, dataModelMapper.capitalizeMethodNameIfRestricted(mappingContext, operationDef.getName()));
dataModel.put(RETURN_TYPE_NAME, javaType);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.kobylynskyi.graphql.codegen.model;

/**
* @author 梦境迷离
* @version 1.0, 2020/12/28
*/
public class DeprecatedDefinition {

private final static String DEFAULT_MSG = "No longer supported";

// For different languages, real implementation.
private final String annotation;
private final String reason;

public DeprecatedDefinition(String annotation) {
this.annotation = annotation;
this.reason = DEFAULT_MSG;
}

/**
* field reason Only support in Scala/Kotlin.
*
* @param annotation Definitions in different languages
* @param reason Description in graphql schema Directive
*/
public DeprecatedDefinition(String annotation, String reason) {
this.annotation = annotation;
this.reason = reason == null ? DEFAULT_MSG : reason;
}

public String getAnnotation() {
return annotation;
}

public String getReason() {
return reason;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public class EnumValueDefinition {
private final String javaName;
private final String graphqlName;
private final List<String> javaDoc;
private final boolean deprecated;
private final DeprecatedDefinition deprecated;

public EnumValueDefinition(String javaName, String graphqlName, List<String> javaDoc, boolean deprecated) {
public EnumValueDefinition(String javaName, String graphqlName, List<String> javaDoc, DeprecatedDefinition deprecated) {
this.javaName = javaName;
this.graphqlName = graphqlName;
this.javaDoc = javaDoc;
Expand All @@ -33,7 +33,7 @@ public List<String> getJavaDoc() {
return javaDoc;
}

public boolean isDeprecated() {
public DeprecatedDefinition getDeprecated() {
return deprecated;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public GeneratedInformation getGeneratedInformation() {
public Set<String> getEnumImportItSelfInScala() {
// Only for scala
if (GeneratedLanguage.SCALA.equals(this.config.getGeneratedLanguage()) && enumImportItSelfInScala == null) {
enumImportItSelfInScala = this.document.getEnumDefinitions().parallelStream().map(this::getModelClassNameWithPrefixAndSuffix).collect(Collectors.toSet());
enumImportItSelfInScala = this.document.getEnumDefinitions().stream().map(this::getModelClassNameWithPrefixAndSuffix).collect(Collectors.toSet());
}
return enumImportItSelfInScala;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.kobylynskyi.graphql.codegen.model;

import graphql.language.Directive;
import graphql.language.StringValue;

/**
* @author [email protected]
* @version 1.0, 2020/12/28
*/
public class MultiLanguageDeprecated {

private final static String REASON = "reason";
private final static String SCALA_ANNOTATION = "deprecated";
private final static String JAVA_ANNOTATION = "Deprecated";
private final static String KOTLIN_ANNOTATION = "Deprecated";

public static DeprecatedDefinition getLanguageDeprecated(GeneratedLanguage generatedLanguage, Directive directive) {
String msg = null;
if (directive.getArguments().stream().anyMatch(argument -> argument.getName().equals(REASON))) {
msg = ((StringValue) directive.getArgument(REASON).getValue()).getValue();
}
switch (generatedLanguage) {
case KOTLIN:
return new DeprecatedDefinition(KOTLIN_ANNOTATION, msg);
case SCALA:
return new DeprecatedDefinition(SCALA_ANNOTATION, msg);
//ignore msg
case JAVA:
default:
return new DeprecatedDefinition(JAVA_ANNOTATION);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class OperationDefinition {
private List<String> annotations = new ArrayList<>();
private List<ParameterDefinition> parameters = new ArrayList<>();
private List<String> javaDoc = new ArrayList<>();
private boolean deprecated;
private DeprecatedDefinition deprecated;
private boolean throwsException;

public String getName() {
Expand Down Expand Up @@ -75,11 +75,11 @@ public void setJavaDoc(List<String> javaDoc) {
this.javaDoc = javaDoc;
}

public boolean isDeprecated() {
public DeprecatedDefinition getDeprecated() {
return deprecated;
}

public void setDeprecated(boolean deprecated) {
public void setDeprecated(DeprecatedDefinition deprecated) {
this.deprecated = deprecated;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class ParameterDefinition {

public static final ParameterDefinition DATA_FETCHING_ENVIRONMENT = new ParameterDefinition(
DataFetchingEnvironment.class.getName(), "env", "env", null, emptyList(), emptyList(), false, false);
DataFetchingEnvironment.class.getName(), "env", "env", null, emptyList(), emptyList(), null, false);

private String type;
/**
Expand All @@ -30,15 +30,15 @@ public class ParameterDefinition {
private String defaultValue;
private List<String> annotations = new ArrayList<>();
private List<String> javaDoc = new ArrayList<>();
private boolean deprecated;
private DeprecatedDefinition deprecated;
private boolean serializeUsingObjectMapper;

public ParameterDefinition() {
}

public ParameterDefinition(String type, String name, String originalName, String defaultValue,
List<String> annotations, List<String> javaDoc,
boolean deprecated, boolean serializeUsingObjectMapper) {
DeprecatedDefinition deprecated, boolean serializeUsingObjectMapper) {
this.type = type;
this.name = name;
this.originalName = originalName;
Expand Down Expand Up @@ -97,11 +97,11 @@ public void setJavaDoc(List<String> javaDoc) {
this.javaDoc = javaDoc;
}

public boolean isDeprecated() {
public DeprecatedDefinition getDeprecated() {
return deprecated;
}

public void setDeprecated(boolean deprecated) {
public void setDeprecated(DeprecatedDefinition deprecated) {
this.deprecated = deprecated;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ProjectionParameterDefinition {
private String type;
private String name;
private String methodName;
private boolean deprecated;
private DeprecatedDefinition deprecated;
private String parametrizedInputClassName;

public String getType() {
Expand All @@ -37,11 +37,11 @@ public void setMethodName(String methodName) {
this.methodName = methodName;
}

public boolean isDeprecated() {
public DeprecatedDefinition getDeprecated() {
return deprecated;
}

public void setDeprecated(boolean deprecated) {
public void setDeprecated(DeprecatedDefinition deprecated) {
this.deprecated = deprecated;
}

Expand Down
Loading