Skip to content
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
1 change: 1 addition & 0 deletions docs/codegen-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
| `useOptionalForNullableReturnTypes` | Boolean | False | Specifies whether nullable return types of api methods should be wrapped into [`java.util.Optional<>`](https://docs.oracle.com/javase/8/docs/api/index.html?java/util/Optional.html). Lists will not be wrapped. |
| `generateApisWithThrowsException` | Boolean | True | Specifies whether api interface methods should have `throws Exception` in signature. |
| `generateNoArgsConstructorOnly` | Boolean | False | Specifies whether model classes should only have a no-args constructor. All-args constructor will not be generated in case value is <b>true</b> |
| `generateModelsWithPublicFields` | Boolean | False | Specifies whether model classes should have public fields and NO getters/setters. By default, fields are private and there are getters/setters for each field. |
| `apiReturnType` | String | Empty | Return type for api methods (query/mutation). For example: `reactor.core.publisher.Mono`, etc. |
| `apiReturnListType` | String | Empty | Return type for api methods (query/mutation) having list type. For example: `reactor.core.publisher.Flux`, etc. By default is empty, so `apiReturnType` will be used. |
| `subscriptionReturnType` | String | Empty | Return type for subscription methods. For example: `org.reactivestreams.Publisher`, `io.reactivex.Observable`, etc. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public class GraphQLCodegenGradleTask extends DefaultTask implements GraphQLCode
private Boolean generateJacksonTypeIdResolver = MappingConfigConstants.DEFAULT_GENERATE_JACKSON_TYPE_ID_RESOLVER;
private Boolean addGeneratedAnnotation = MappingConfigConstants.DEFAULT_ADD_GENERATED_ANNOTATION;
private Boolean generateNoArgsConstructorOnly = MappingConfigConstants.DEFAULT_GENERATE_NOARGS_CONSTRUCTOR_ONLY;
private Boolean generateModelsWithPublicFields = MappingConfigConstants.DEFAULT_GENERATE_MODELS_WITH_PUBLIC_FIELDS;
private String generatedAnnotation;
private Set<String> fieldsWithResolvers = new HashSet<>();
private Set<String> fieldsWithoutResolvers = new HashSet<>();
Expand Down Expand Up @@ -157,6 +158,7 @@ public void generate() throws Exception {
mappingConfig.setGenerateApisWithThrowsException(generateApisWithThrowsException);
mappingConfig.setGenerateJacksonTypeIdResolver(generateJacksonTypeIdResolver);
mappingConfig.setGenerateNoArgsConstructorOnly(generateNoArgsConstructorOnly);
mappingConfig.setGenerateModelsWithPublicFields(generateModelsWithPublicFields);
mappingConfig.setAddGeneratedAnnotation(addGeneratedAnnotation);
mappingConfig.setGeneratedAnnotation(generatedAnnotation);
mappingConfig.setApiReturnType(apiReturnType);
Expand Down Expand Up @@ -695,6 +697,17 @@ public void setGenerateNoArgsConstructorOnly(Boolean generateNoArgsConstructorOn
this.generateNoArgsConstructorOnly = generateNoArgsConstructorOnly;
}

@Input
@Optional
@Override
public Boolean isGenerateModelsWithPublicFields() {
return generateModelsWithPublicFields;
}

public void setGenerateModelsWithPublicFields(Boolean generateModelsWithPublicFields) {
this.generateModelsWithPublicFields = generateModelsWithPublicFields;
}

@Input
@Optional
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenCo
@Parameter(defaultValue = MappingConfigConstants.DEFAULT_GENERATE_NOARGS_CONSTRUCTOR_ONLY_STRING)
private boolean generateNoArgsConstructorOnly;

@Parameter(defaultValue = MappingConfigConstants.DEFAULT_GENERATE_MODELS_WITH_PUBLIC_FIELDS_STRING)
private boolean generateModelsWithPublicFields;

@Parameter
private String generatedAnnotation;

Expand Down Expand Up @@ -275,6 +278,7 @@ public void execute() throws MojoExecutionException {
mappingConfig.setAddGeneratedAnnotation(addGeneratedAnnotation);
mappingConfig.setGeneratedAnnotation(generatedAnnotation);
mappingConfig.setGenerateNoArgsConstructorOnly(generateNoArgsConstructorOnly);
mappingConfig.setGenerateModelsWithPublicFields(generateModelsWithPublicFields);
mappingConfig.setFieldsWithResolvers(mapToHashSet(fieldsWithResolvers));
mappingConfig.setFieldsWithoutResolvers(mapToHashSet(fieldsWithoutResolvers));
mappingConfig.setRelayConfig(relayConfig);
Expand Down Expand Up @@ -684,6 +688,11 @@ public Boolean isGenerateNoArgsConstructorOnly() {
return generateNoArgsConstructorOnly;
}

@Override
public Boolean isGenerateModelsWithPublicFields() {
return generateModelsWithPublicFields;
}

public ParentInterfacesConfig getParentInterfaces() {
return parentInterfaces;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,16 @@ trait GraphQLCodegenKeys {
"Specifies whether generated union interfaces should be annotated with a custom Jackson type id resolver generated in model package."
)

// not support in scala
// not supported in scala
val generateNoArgsConstructorOnly = settingKey[Boolean](
"Specifies whether model classes should only have a no-args constructor. All-args constructor will not be generated in case value is .true."
)

// not supported in scala
val generateModelsWithPublicFields = settingKey[Boolean](
"Specifies whether model classes should have public fields and NO getters/setters. By default, fields are private and there are getters/setters for each field."
)

// for version
val javaxValidationApiVersion = settingKey[Option[String]]("javax-validation-api version")
val graphqlJavaCodegenVersion = settingKey[Option[String]]("graphql-java-codegen version")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,17 @@ class GraphQLCodegenPlugin(configuration: Configuration, private[codegen] val co
generateBuilder := MappingConfigConstants.DEFAULT_BUILDER,
generateApis := MappingConfigConstants.DEFAULT_GENERATE_APIS,
generateEqualsAndHashCode := MappingConfigConstants.DEFAULT_EQUALS_AND_HASHCODE,
generateImmutableModels := MappingConfigConstants.DEFAULT_GENERATE_IMMUTABLE_MODELS, // TODO change default value
generateToString := MappingConfigConstants.DEFAULT_TO_STRING,
generateImmutableModels := MappingConfigConstants.DEFAULT_GENERATE_IMMUTABLE_MODELS, // TODO change default value
generateToString := MappingConfigConstants.DEFAULT_TO_STRING,
// parent interfaces configs:
parentInterfaces := parentInterfacesConfig,
generateAllMethodInProjection := MappingConfigConstants.DEFAULT_GENERATE_ALL_METHOD,
responseProjectionMaxDepth := MappingConfigConstants.DEFAULT_RESPONSE_PROJECTION_MAX_DEPTH,
supportUnknownFields := MappingConfigConstants.DEFAULT_SUPPORT_UNKNOWN_FIELDS,
unknownFieldsPropertyName := MappingConfigConstants.DEFAULT_UNKNOWN_FIELDS_PROPERTY_NAME,
generateNoArgsConstructorOnly := MappingConfigConstants.DEFAULT_GENERATE_NOARGS_CONSTRUCTOR_ONLY,
skip := false
parentInterfaces := parentInterfacesConfig,
generateAllMethodInProjection := MappingConfigConstants.DEFAULT_GENERATE_ALL_METHOD,
responseProjectionMaxDepth := MappingConfigConstants.DEFAULT_RESPONSE_PROJECTION_MAX_DEPTH,
supportUnknownFields := MappingConfigConstants.DEFAULT_SUPPORT_UNKNOWN_FIELDS,
unknownFieldsPropertyName := MappingConfigConstants.DEFAULT_UNKNOWN_FIELDS_PROPERTY_NAME,
generateNoArgsConstructorOnly := MappingConfigConstants.DEFAULT_GENERATE_NOARGS_CONSTRUCTOR_ONLY,
generateModelsWithPublicFields := MappingConfigConstants.DEFAULT_GENERATE_MODELS_WITH_PUBLIC_FIELDS,
skip := false
)

private def getMappingConfig(): Def.Initialize[MappingConfig] = Def.setting {
Expand Down Expand Up @@ -192,6 +193,7 @@ class GraphQLCodegenPlugin(configuration: Configuration, private[codegen] val co
mappingConfig.setGenerateModelOpenClasses((GraphQLCodegenConfig / generateModelOpenClasses).value)
mappingConfig.setGenerateJacksonTypeIdResolver((GraphQLCodegenConfig / generateJacksonTypeIdResolver).value);
mappingConfig.setGenerateNoArgsConstructorOnly((GraphQLCodegenConfig / generateNoArgsConstructorOnly).value);
mappingConfig.setGenerateModelsWithPublicFields((GraphQLCodegenConfig / generateModelsWithPublicFields).value);

mappingConfig.setSupportUnknownFields((GraphQLCodegenConfig / supportUnknownFields).value)
mappingConfig.setUnknownFieldsPropertyName((GraphQLCodegenConfig / unknownFieldsPropertyName).value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ private ParameterDefinition mapField(MappingContext mappingContext,
mappingContext, fieldDef.getType(), fieldDef, parentDefinition.getName(), false));
parameter.setJavaDoc(fieldDef.getJavaDoc());
parameter.setDeprecated(DeprecatedDefinitionBuilder.build(mappingContext, fieldDef));
parameter.setVisibility(Utils.getFieldVisibility(mappingContext));
parameter.setMandatory(namedDefinition.isMandatory());
parameter.setSerializeUsingObjectMapper(namedDefinition.isSerializeUsingObjectMapper());
parameter.setGetterMethodName(dataModelMapper.capitalizeMethodNameIfRestricted(mappingContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ private ParameterDefinition map(MappingContext mappingContext, InputValueDefinit
namedDefinition.getJavaName()));
parameter.setDefaultValue(valueMapper.map(
mappingContext, inputValueDefinition.getDefaultValue(), inputValueDefinition.getType()));
parameter.setVisibility(Utils.getFieldVisibility(mappingContext));
parameter.setAnnotations(annotationsMapper.getAnnotations(mappingContext, inputValueDefinition.getType(),
inputValueDefinition, parentTypeName, false));
parameter.setDeprecated(DeprecatedDefinitionBuilder.build(mappingContext, inputValueDefinition));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ default Optional<ParameterDefinition> createUnknownFields(MappingContext mapping
unknownFields.setName(mappingContext.getUnknownFieldsPropertyName());
unknownFields.setGetterMethodName("get" + Utils.capitalize(mappingContext.getUnknownFieldsPropertyName()));
unknownFields.setOriginalName(mappingContext.getUnknownFieldsPropertyName());
unknownFields.setVisibility(Utils.getFieldVisibility(mappingContext));
unknownFields.setType("java.util.Map<String, Object>");
unknownFields.setAnnotations(Arrays.asList(
"com.fasterxml.jackson.annotation.JsonAnyGetter",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,5 +522,13 @@ public interface GraphQLCodegenConfiguration {
*/
Boolean isGenerateNoArgsConstructorOnly();

/**
* Specifies whether model classes should have public or private fields.
*
* @return <b>true</b> if model classes should have public fields and no getters/setters.
* <b>false</b> if model classes should have private fields and getters/setters.
*/
Boolean isGenerateModelsWithPublicFields();


}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class MappingConfig implements GraphQLCodegenConfiguration, Combinable<Ma
private Boolean generateJacksonTypeIdResolver;
private Boolean supportUnknownFields;
private Boolean generateNoArgsConstructorOnly;
private Boolean generateModelsWithPublicFields;

// field resolvers configs:
private Set<String> fieldsWithResolvers = new HashSet<>();
Expand Down Expand Up @@ -212,6 +213,8 @@ public void combine(MappingConfig source) {
GraphQLCodegenConfiguration::getGeneratedAnnotation);
generateNoArgsConstructorOnly = getValueOrDefaultToThis(source,
GraphQLCodegenConfiguration::isGenerateNoArgsConstructorOnly);
generateModelsWithPublicFields = getValueOrDefaultToThis(source,
GraphQLCodegenConfiguration::isGenerateNoArgsConstructorOnly);
}

private <T> T getValueOrDefaultToThis(MappingConfig source, Function<MappingConfig, T> getValueFunction) {
Expand Down Expand Up @@ -754,4 +757,13 @@ public Boolean isGenerateNoArgsConstructorOnly() {
public void setGenerateNoArgsConstructorOnly(Boolean generateNoArgsConstructorOnly) {
this.generateNoArgsConstructorOnly = generateNoArgsConstructorOnly;
}

@Override
public Boolean isGenerateModelsWithPublicFields() {
return generateModelsWithPublicFields;
}

public void setGenerateModelsWithPublicFields(Boolean generateModelsWithPublicFields) {
this.generateModelsWithPublicFields = generateModelsWithPublicFields;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ public class MappingConfigConstants {
public static final boolean DEFAULT_GENERATE_NOARGS_CONSTRUCTOR_ONLY = false;
public static final String DEFAULT_GENERATE_NOARGS_CONSTRUCTOR_ONLY_STRING = "false";

public static final boolean DEFAULT_GENERATE_MODELS_WITH_PUBLIC_FIELDS = false;
public static final String DEFAULT_GENERATE_MODELS_WITH_PUBLIC_FIELDS_STRING = "false";

private MappingConfigConstants() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ public static void initDefaultValues(MappingConfig mappingConfig) {
mappingConfig.setGenerateNoArgsConstructorOnly(
MappingConfigConstants.DEFAULT_GENERATE_NOARGS_CONSTRUCTOR_ONLY);
}
if (mappingConfig.isGenerateModelsWithPublicFields() == null) {
mappingConfig.setGenerateModelsWithPublicFields(
MappingConfigConstants.DEFAULT_GENERATE_MODELS_WITH_PUBLIC_FIELDS);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ public Boolean isGenerateNoArgsConstructorOnly() {
return config.isGenerateNoArgsConstructorOnly();
}

@Override
public Boolean isGenerateModelsWithPublicFields() {
return config.isGenerateModelsWithPublicFields();
}

public ExtendedDocument getDocument() {
return document;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class ParameterDefinition {
*/
private String originalName;
private String defaultValue;
private String visibility;
private boolean isMandatory;
private List<String> annotations = new ArrayList<>();
private List<String> javaDoc = new ArrayList<>();
Expand Down Expand Up @@ -82,6 +83,14 @@ public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}

public String getVisibility() {
return visibility;
}

public void setVisibility(String visibility) {
this.visibility = visibility;
}

public boolean isMandatory() {
return isMandatory;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kobylynskyi.graphql.codegen.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.kobylynskyi.graphql.codegen.model.MappingContext;
import com.kobylynskyi.graphql.codegen.model.exception.UnableToCreateDirectoryException;
import com.kobylynskyi.graphql.codegen.model.exception.UnableToDeleteDirectoryException;
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperation;
Expand Down Expand Up @@ -263,4 +264,8 @@ public static String wrapString(String str, String wrapStart, String wrapEnd) {
return wrapStart + str + wrapEnd;
}

public static String getFieldVisibility(MappingContext mappingContext) {
return Boolean.TRUE.equals(mappingContext.isGenerateModelsWithPublicFields()) ? "public" : "private";
}

}
Loading