Skip to content

Commit fb17635

Browse files
Scala: fix generateModelOpenClasses (#741)
When use `generateModelOpenClasses`, add `val` before field declaration
1 parent c40ec60 commit fb17635

File tree

4 files changed

+451
-29
lines changed

4 files changed

+451
-29
lines changed

src/main/resources/templates/scala-lang/scalaClassGraphqlType.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ import ${enum}._
8080
<#list field.annotations as annotation>
8181
@${annotation}
8282
</#list>
83-
<#if parentInterfaces?has_content><#list parentInterfaces as parent><#if parent == field.name>override </#if></#list></#if><#if !immutableModels>var <#else><#if parentInterfaces?has_content><#list parentInterfaces as parent><#if parent == field.name>val </#if></#list></#if></#if>${field.name}: ${field.type}<#if field.defaultValue?has_content> = <#if MapperUtil.isScalaOption(field.type)><#if field.defaultValue != "null">Some(${field.defaultValue})<#else>None</#if><#else>${field.defaultValue}</#if></#if><#if field_has_next>,</#if>
83+
<#if parentInterfaces?has_content><#list parentInterfaces as parent><#if parent == field.name>override </#if></#list></#if><#if !immutableModels>var <#elseif generateModelOpenClasses>val <#else><#if parentInterfaces?has_content><#list parentInterfaces as parent><#if parent == field.name>val </#if></#list></#if></#if>${field.name}: ${field.type}<#if field.defaultValue?has_content> = <#if MapperUtil.isScalaOption(field.type)><#if field.defaultValue != "null">Some(${field.defaultValue})<#else>None</#if><#else>${field.defaultValue}</#if></#if><#if field_has_next>,</#if>
8484
</#list>
8585
</#if>
8686
)<#if implements?has_content> extends <#list implements as interface>${interface}<#if interface_has_next> with </#if></#list></#if> {

src/test/java/com/kobylynskyi/graphql/codegen/scala/GraphQLCodegenOpenclassesTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,14 @@ void generate_MultipleInterfacesPerType() throws Exception {
4949
getFileByName(files, "Commit.scala"));
5050
}
5151

52+
@Test
53+
void generate_MultipleInterfacesPerTypeVarFields() throws Exception {
54+
mappingConfig.setGenerateImmutableModels(false);
55+
new ScalaGraphQLCodegen(singletonList("src/test/resources/schemas/github.graphqls"),
56+
outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();
57+
File[] files = Objects.requireNonNull(outputScalaClassesDir.listFiles());
58+
59+
assertSameTrimmedContent(getFileByName(files, "Commit.scala"),
60+
new File("src/test/resources/expected-classes/scala/Commit_normal_class_var_fields.scala.txt"));
61+
}
5262
}

src/test/resources/expected-classes/scala/Commit_no_final_class.scala.txt

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,67 +12,67 @@ import SubscriptionState._
1212
class Commit(
1313
@javax.validation.constraints.NotNull
1414
override val abbreviatedOid: String,
15-
additions: Int,
16-
associatedPullRequests: PullRequestConnection,
17-
author: GitActor,
18-
authoredByCommitter: Boolean,
15+
val additions: Int,
16+
val associatedPullRequests: PullRequestConnection,
17+
val author: GitActor,
18+
val authoredByCommitter: Boolean,
1919
@javax.validation.constraints.NotNull
20-
authoredDate: String,
20+
val authoredDate: String,
2121
@javax.validation.constraints.NotNull
22-
blame: Blame,
23-
changedFiles: Int,
22+
val blame: Blame,
23+
val changedFiles: Int,
2424
@javax.validation.constraints.NotNull
25-
comments: CommitCommentConnection,
25+
val comments: CommitCommentConnection,
2626
@javax.validation.constraints.NotNull
2727
override val commitResourcePath: String,
2828
@javax.validation.constraints.NotNull
2929
override val commitUrl: String,
3030
@javax.validation.constraints.NotNull
31-
committedDate: String,
32-
committedViaWeb: Boolean,
33-
committer: GitActor,
34-
deletions: Int,
35-
deployments: DeploymentConnection,
31+
val committedDate: String,
32+
val committedViaWeb: Boolean,
33+
val committer: GitActor,
34+
val deletions: Int,
35+
val deployments: DeploymentConnection,
3636
@javax.validation.constraints.NotNull
37-
history: CommitHistoryConnection,
37+
val history: CommitHistoryConnection,
3838
@javax.validation.constraints.NotNull
3939
override val id: String,
4040
@javax.validation.constraints.NotNull
41-
message: String,
41+
val message: String,
4242
@javax.validation.constraints.NotNull
43-
messageBody: String,
43+
val messageBody: String,
4444
@javax.validation.constraints.NotNull
45-
messageBodyHTML: String,
45+
val messageBodyHTML: String,
4646
@javax.validation.constraints.NotNull
47-
messageHeadline: String,
47+
val messageHeadline: String,
4848
@javax.validation.constraints.NotNull
49-
messageHeadlineHTML: String,
49+
val messageHeadlineHTML: String,
5050
@javax.validation.constraints.NotNull
5151
override val oid: String,
5252
@javax.validation.constraints.NotNull
53-
parents: CommitConnection,
54-
pushedDate: String,
53+
val parents: CommitConnection,
54+
val pushedDate: String,
5555
@javax.validation.constraints.NotNull
5656
override val repository: Repository,
5757
@javax.validation.constraints.NotNull
5858
override val resourcePath: String,
59-
signature: GitSignature,
60-
status: Status,
59+
val signature: GitSignature,
60+
val status: Status,
6161
@javax.validation.constraints.NotNull
62-
tarballUrl: String,
62+
val tarballUrl: String,
6363
@javax.validation.constraints.NotNull
64-
tree: Tree,
64+
val tree: Tree,
6565
@javax.validation.constraints.NotNull
66-
treeResourcePath: String,
66+
val treeResourcePath: String,
6767
@javax.validation.constraints.NotNull
68-
treeUrl: String,
68+
val treeUrl: String,
6969
@javax.validation.constraints.NotNull
7070
override val url: String,
7171
override val viewerCanSubscribe: Boolean,
7272
@com.fasterxml.jackson.module.scala.JsonScalaEnumeration(classOf[com.github.graphql.SubscriptionStateTypeRefer])
7373
override val viewerSubscription: SubscriptionState,
7474
@javax.validation.constraints.NotNull
75-
zipballUrl: String
75+
val zipballUrl: String
7676
) extends Closer with IssueTimelineItem with PullRequestTimelineItem with Subscribable with Node with GitObject with UniformResourceLocatable {
7777

7878
override def toString(): String = {

0 commit comments

Comments
 (0)