Skip to content

Commit

Permalink
Rename AutomaticallyDefaultNullable to InitializeNullableTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
zmack committed Nov 10, 2021
1 parent 7d1ef68 commit 8710bb6
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 34 deletions.
2 changes: 1 addition & 1 deletion docs/codegen-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ See [DirectiveAnnotationsMapping](#option-directiveannotationsmapping)* |
| `responseProjectionMaxDepth` | Integer | 3 | Sets max depth when use `all$()` which for facilitating the construction of projection automatically, the fields on all projections are provided when it be invoked. This is a global configuration, of course, you can use `all$(max)` to set for each method. For self recursive types, too big depth may result in a large number of returned data!|
| `generatedLanguage` | Enum | GeneratedLanguage.JAVA | Choose which language you want to generate, Java,Scala,Kotlin were supported. Note that due to language features, there are slight differences in default values between languages.|
| `generateModelOpenClasses` | Boolean | false | The class type of the generated model. If true, generate normal classes, else generate data classes. It only support in kotlin(```data class```) and scala(```case class```). Maybe we will consider to support Java ```record``` in the future.|
| `automaticallyDefaultNullable` | Boolean | false | Adds a default null value to nullable arguments. Only supported in Kotlin.
| `initializeNullableTypes` | Boolean | false | Adds a default null value to nullable arguments. Only supported in Kotlin.
| `typesAsInterfaces` | Set(String) | Empty | Types that must generated as interfaces should be defined here in format: `TypeName` or `@directive`. E.g.: `User`, `@asInterface`. |

### Option `graphqlSchemas`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public class GraphQLCodegenGradleTask extends DefaultTask implements GraphQLCode
private List<String> configurationFiles;
private GeneratedLanguage generatedLanguage = MappingConfigConstants.DEFAULT_GENERATED_LANGUAGE;
private Boolean generateModelOpenClasses = MappingConfigConstants.DEFAULT_GENERATE_MODEL_OPEN_CLASSES;
private Boolean automaticallyDefaultNullable = MappingConfigConstants.DEFAULT_AUTOMATICALLY_DEFAULT_NULLABLE;
private Boolean initializeNullableTypes = MappingConfigConstants.DEFAULT_INITIALIZE_NULLABLE_TYPES;

public GraphQLCodegenGradleTask() {
setGroup("codegen");
Expand Down Expand Up @@ -179,7 +179,7 @@ public void generate() throws Exception {

mappingConfig.setGeneratedLanguage(generatedLanguage);
mappingConfig.setGenerateModelOpenClasses(generateModelOpenClasses);
mappingConfig.setAutomaticallyDefaultNullable(automaticallyDefaultNullable);
mappingConfig.setInitializeNullableTypes(initializeNullableTypes);

instantiateCodegen(mappingConfig).generate();
}
Expand Down Expand Up @@ -847,7 +847,14 @@ public void setGenerateModelOpenClasses(Boolean generateModelOpenClasses) {
this.generateModelOpenClasses = generateModelOpenClasses;
}

public void setAutomaticallyDefaultNullable(Boolean automaticallyDefaultNullable) {
this.automaticallyDefaultNullable = automaticallyDefaultNullable;
@Input
@Optional
@Override
public Boolean isInitializeNullableTypes() {
return initializeNullableTypes;
}

public void setInitializeNullableTypes(Boolean initializeNullableTypes) {
this.initializeNullableTypes = initializeNullableTypes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenCo
private boolean generateModelOpenClasses;

@Parameter(defaultValue = MappingConfigConstants.DEFAULT_AUTOMATICALLY_DEFAULT_NULLABLE_STRING)
private boolean automaticallyDefaultNullable;
private boolean initializeNullableTypes;

@Override
public void execute() throws MojoExecutionException {
Expand Down Expand Up @@ -270,7 +270,7 @@ public void execute() throws MojoExecutionException {

mappingConfig.setGeneratedLanguage(generatedLanguage);
mappingConfig.setGenerateModelOpenClasses(isGenerateModelOpenClasses());
mappingConfig.setAutomaticallyDefaultNullable(isAutomaticallyDefaultNullable());
mappingConfig.setInitializeNullableTypes(isInitializeNullableTypes());

try {
instantiateCodegen(mappingConfig).generate();
Expand Down Expand Up @@ -608,8 +608,8 @@ public Boolean isGenerateModelOpenClasses() {
}

@Override
public Boolean isAutomaticallyDefaultNullable() {
return automaticallyDefaultNullable;
public Boolean isInitializeNullableTypes() {
return initializeNullableTypes;
}

public ParentInterfacesConfig getParentInterfaces() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ protected void initDefaultValues(MappingConfig mappingConfig) {
if (mappingConfig.isGenerateModelOpenClasses() == null) {
mappingConfig.setGenerateModelOpenClasses(false);
}
if (mappingConfig.isAutomaticallyDefaultNullable() == null) {
mappingConfig.setAutomaticallyDefaultNullable(true);
if (mappingConfig.isInitializeNullableTypes() == null) {
mappingConfig.setInitializeNullableTypes(false);
}
if (mappingConfig.getGenerateBuilder() == null) {
// functional expression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import static com.kobylynskyi.graphql.codegen.model.DataModelFields.AUTOMATICALLY_DEFAULT_NULLABLE;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.INITIALIZE_NULLABLE_TYPES;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.CLASS_NAME;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.ENUM_IMPORT_IT_SELF_IN_SCALA;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_ANNOTATION;
Expand Down Expand Up @@ -163,7 +163,7 @@ private Map<String, Object> mapToResolverModel(MappingContext mappingContext, St
dataModel.put(GENERATED_INFO, mappingContext.getGeneratedInformation());
dataModel.put(ENUM_IMPORT_IT_SELF_IN_SCALA, mappingContext.getEnumImportItSelfInScala());
dataModel.put(GENERATE_MODEL_OPEN_CLASSES, mappingContext.isGenerateModelOpenClasses());
dataModel.put(AUTOMATICALLY_DEFAULT_NULLABLE, mappingContext.isAutomaticallyDefaultNullable());
dataModel.put(INITIALIZE_NULLABLE_TYPES, mappingContext.isInitializeNullableTypes());
return dataModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.Map;

import static com.kobylynskyi.graphql.codegen.model.DataModelFields.ANNOTATIONS;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.AUTOMATICALLY_DEFAULT_NULLABLE;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.INITIALIZE_NULLABLE_TYPES;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.BUILDER;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.CLASS_NAME;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.ENUM_IMPORT_IT_SELF_IN_SCALA;
Expand Down Expand Up @@ -68,7 +68,7 @@ public Map<String, Object> map(MappingContext mappingContext, ExtendedInputObjec
dataModel.put(GENERATED_INFO, mappingContext.getGeneratedInformation());
dataModel.put(ENUM_IMPORT_IT_SELF_IN_SCALA, mappingContext.getEnumImportItSelfInScala());
dataModel.put(GENERATE_MODEL_OPEN_CLASSES, mappingContext.isGenerateModelOpenClasses());
dataModel.put(AUTOMATICALLY_DEFAULT_NULLABLE, mappingContext.isAutomaticallyDefaultNullable());
dataModel.put(INITIALIZE_NULLABLE_TYPES, mappingContext.isInitializeNullableTypes());
return dataModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.stream.Collectors;

import static com.kobylynskyi.graphql.codegen.model.DataModelFields.ANNOTATIONS;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.AUTOMATICALLY_DEFAULT_NULLABLE;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.INITIALIZE_NULLABLE_TYPES;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.BUILDER;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.CLASS_NAME;
import static com.kobylynskyi.graphql.codegen.model.DataModelFields.ENUM_IMPORT_IT_SELF_IN_SCALA;
Expand Down Expand Up @@ -103,7 +103,7 @@ public Map<String, Object> map(MappingContext mappingContext,
dataModel.put(ENUM_IMPORT_IT_SELF_IN_SCALA, mappingContext.getEnumImportItSelfInScala());
dataModel.put(PARENT_INTERFACE_PROPERTIES, mappingContext.getParentInterfaceProperties());
dataModel.put(GENERATE_MODEL_OPEN_CLASSES, mappingContext.isGenerateModelOpenClasses());
dataModel.put(AUTOMATICALLY_DEFAULT_NULLABLE, mappingContext.isAutomaticallyDefaultNullable());
dataModel.put(INITIALIZE_NULLABLE_TYPES, mappingContext.isInitializeNullableTypes());
return dataModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public final class DataModelFields {
public static final String PARENT_INTERFACE_PROPERTIES = "parentInterfaceProperties";
public static final String SERIALIZATION_LIBRARY = "serializationLibrary";
public static final String GENERATE_MODEL_OPEN_CLASSES = "generateModelOpenClasses";
public static final String AUTOMATICALLY_DEFAULT_NULLABLE = "automaticallyDefaultNullable";
public static final String INITIALIZE_NULLABLE_TYPES = "initializeNullableTypes";

private DataModelFields() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,6 @@ public interface GraphQLCodegenConfiguration {
*
* @return <b>true</b> if nullable fields should be defaulted to null.
*/
Boolean isAutomaticallyDefaultNullable();
Boolean isInitializeNullableTypes();

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class MappingConfig implements GraphQLCodegenConfiguration, Combinable<Ma
private Set<String> typesAsInterfaces = new HashSet<>();

private boolean generateModelOpenClasses;
private boolean automaticallyDefaultNullable;
private boolean initializeNullableTypes;

private GeneratedLanguage generatedLanguage;

Expand Down Expand Up @@ -186,8 +186,8 @@ public void combine(MappingConfig source) {
generatedLanguage = getValueOrDefaultToThis(source, GraphQLCodegenConfiguration::getGeneratedLanguage);
generateModelOpenClasses = getValueOrDefaultToThis(source,
GraphQLCodegenConfiguration::isGenerateModelOpenClasses);
automaticallyDefaultNullable = getValueOrDefaultToThis(source,
GraphQLCodegenConfiguration::isAutomaticallyDefaultNullable);
initializeNullableTypes = getValueOrDefaultToThis(source,
GraphQLCodegenConfiguration::isInitializeNullableTypes);
}

private <T> T getValueOrDefaultToThis(MappingConfig source, Function<MappingConfig, T> getValueFunction) {
Expand Down Expand Up @@ -658,12 +658,12 @@ public void setGenerateModelOpenClasses(boolean generateModelOpenClasses) {
this.generateModelOpenClasses = generateModelOpenClasses;
}

public Boolean isAutomaticallyDefaultNullable() {
return automaticallyDefaultNullable;
public Boolean isInitializeNullableTypes() {
return initializeNullableTypes;
}

public void setAutomaticallyDefaultNullable(boolean automaticallyDefaultNullable) {
this.automaticallyDefaultNullable = automaticallyDefaultNullable;
public void setInitializeNullableTypes(boolean initializeNullableTypes) {
this.initializeNullableTypes = initializeNullableTypes;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public class MappingConfigConstants {
public static final String DEFAULT_GENERATE_MODEL_OPEN_CLASSES_STRING = "false";

//Only supported in kotlin.
public static final boolean DEFAULT_AUTOMATICALLY_DEFAULT_NULLABLE = false;
public static final String DEFAULT_AUTOMATICALLY_DEFAULT_NULLABLE_STRING = "false";
public static final boolean DEFAULT_INITIALIZE_NULLABLE_TYPES = false;
public static final String DEFAULT_INITIALIZE_NULLABLE_TYPES_STRING = "false";


private MappingConfigConstants() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public Boolean isGenerateModelOpenClasses() {
}

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

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/kotlin-lang/type.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ open class ${className}()<#if implements?has_content> : <#list implements as int
<#if parentInterfaces?has_content><#list parentInterfaces as parent><#if parent == field.name>override
</#if></#list></#if><#if !immutableModels><#list field.annotations as annotation>@get:${annotation}
</#list>var <#else><#list field.annotations as annotation>@get:${annotation}
</#list>val </#if>${field.name}: ${field.type}<#if field.defaultValue?has_content> = ${field.defaultValue}<#elseif field.type?ends_with("?") && (automaticallyDefaultNullable == true)> = null</#if><#if field_has_next>,</#if>
</#list>val </#if>${field.name}: ${field.type}<#if field.defaultValue?has_content> = ${field.defaultValue}<#elseif field.type?ends_with("?") && (initializeNullableTypes == true)> = null</#if><#if field_has_next>,</#if>
</#list>
</#if>
)<#if implements?has_content> : <#list implements as interface>${interface}<#if interface_has_next>, </#if></#list></#if> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import static com.kobylynskyi.graphql.codegen.TestUtils.getFileByName;
import static java.util.Collections.singletonList;

class GraphQLCodegenAutomaticallyDefaultNullableTest {
class GraphQLCodegenInitializeNullableTypesTest {

private final File outputBuildDir = new File("build/generated");
private final File outputScalaClassesDir = new File("build/generated/com/github/graphql");
Expand All @@ -30,7 +30,7 @@ void init() {
mappingConfig.setGenerateApis(true);
mappingConfig.setGenerateClient(true);
mappingConfig.setGenerateEqualsAndHashCode(true);
mappingConfig.setAutomaticallyDefaultNullable(true);
mappingConfig.setInitializeNullableTypes(true);
mappingConfig.setGenerateBuilder(true);
}

Expand All @@ -40,7 +40,7 @@ void cleanup() {
}

@Test
void generate_AutomaticallyDefaultNullable() throws Exception {
void generate_InitializeNullableTypes() throws Exception {
new KotlinGraphQLCodegen(singletonList("src/test/resources/schemas/github.graphqls"),
outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();
File[] files = Objects.requireNonNull(outputScalaClassesDir.listFiles());
Expand Down

0 comments on commit 8710bb6

Please sign in to comment.