Skip to content
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

Patching conflict between generateImmutableModels and generateNoArgsConstructorOnly #1395

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/main/resources/templates/java-lang/type.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class ${className} implements java.io.Serializable<#if implements?has_con
public ${className}() {
}

<#if fields?has_content && !generateNoArgsConstructorOnly>
<#if fields?has_content && (!generateNoArgsConstructorOnly || immutableModels)>
public ${className}(<#list fields as field>${field.type} ${field.name}<#if field_has_next>, </#if></#list>) {
<#list fields as field>
this.${field.name} = ${field.name};
Expand Down Expand Up @@ -202,7 +202,7 @@ public class ${className} implements java.io.Serializable<#if implements?has_con
</#if>

public ${className} build() {
<#if generateNoArgsConstructorOnly>
<#if !immutableModels && generateNoArgsConstructorOnly>
${className} result = new ${className}();
<#list fields as field>
<#if field.visibility == 'public'>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,20 @@ void generate_CheckFiles() throws Exception {
new File("src/test/resources/expected-classes/immutable/Event.java.txt"),
getFileByName(files, "Event.java"));
}

@Test
void generate_noArgs_CheckFiles() throws Exception {
mappingConfig.setPackageName("com.kobylynskyi.graphql.immutable");
mappingConfig.setGenerateImmutableModels(true);
mappingConfig.setGenerateNoArgsConstructorOnly(true);

new JavaGraphQLCodegen(singletonList("src/test/resources/schemas/test.graphqls"),
outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo(mappingConfig)).generate();

File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());

assertSameTrimmedContent(
new File("src/test/resources/expected-classes/immutable/Event.java.txt"),
getFileByName(files, "Event.java"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ void generate_PublicFields() throws Exception {
void generate_PublicFields_NoArgsConstructor_immutableModels() throws Exception {
mappingConfig.setGenerateModelsWithPublicFields(true);
mappingConfig.setGenerateNoArgsConstructorOnly(true);
mappingConfig.setGenerateImmutableModels(true);
mappingConfig.setGenerateImmutableModels(false);
mappingConfig.setGenerateClient(true);

generate("src/test/resources/schemas/test.graphqls");
Expand All @@ -483,7 +483,7 @@ void generate_PublicFields_NoArgsConstructor_immutableModels() throws Exception
void generate_PublicFields_NoBuilder_NoArgsConstructor() throws Exception {
mappingConfig.setGenerateModelsWithPublicFields(true);
mappingConfig.setGenerateNoArgsConstructorOnly(true);
mappingConfig.setGenerateImmutableModels(true);
mappingConfig.setGenerateImmutableModels(false);
mappingConfig.setGenerateBuilder(false);

generate("src/test/resources/schemas/test.graphqls");
Expand Down