未验证 提交 600c902c 编写于 作者: 梦境迷离's avatar 梦境迷离 提交者: GitHub

Fix Scala and Kotlin minor code generation issues (#558)

* Optimize template

* add test
上级 c8df4854
......@@ -31,6 +31,12 @@ public class ScalaDataModelMapper implements DataModelMapper {
if (SCALA_RESTRICTED_KEYWORDS.contains(fieldName)) {
return wrapString(fieldName, RESTRICTED_WORDS_WRAP_WITH);
}
// Scala case class's Fields cannot also use the names of these methods.
if (SCALA_RESTRICTED_METHOD_NAMES.contains(fieldName)) {
return Utils.capitalize(fieldName);
}
return fieldName;
}
......
......@@ -91,9 +91,9 @@ open class ${className}()<#if implements?has_content> : <#list implements as int
joiner.add("${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name}))
<#else>
<#if field.type == "String?">
joiner.add("${field.originalName}: \"${field.name}\"")
joiner.add("${field.originalName}: \"" + ${field.name} + "\"");
<#else>
joiner.add(${field.originalName}: ${field.name}")
joiner.add("${field.originalName}: " + ${field.name});
</#if>
</#if>
}
......@@ -102,9 +102,9 @@ open class ${className}()<#if implements?has_content> : <#list implements as int
joiner.add("${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name}))
<#else>
<#if field.type == "String">
joiner.add("${field.originalName}: \"${field.name}\"")
joiner.add("${field.originalName}: \"" + ${field.name} + "\"");
<#else>
joiner.add(${field.originalName}: ${field.name}")
joiner.add("${field.originalName}: " + ${field.name});
</#if>
</#if>
</#if>
......
......@@ -85,4 +85,25 @@ class GraphQLCodegenNullableTest {
new File("src/test/resources/expected-classes/kt/nullable/QueryResolver_WITH_Prefix_Suffix.kt.txt"),
getFileByName(files, "QueryResolver.kt"));
}
}
\ No newline at end of file
/**
* Verify Int to override Int?
*/
@Test
void generate_OptionalFieldInInterfaceAndMandatoryInType() throws Exception {
mappingConfig.setGenerateBuilder(true);
mappingConfig.setGenerateToString(true);
mappingConfig.setGenerateEqualsAndHashCode(true);
schemaFinder.setIncludePattern("optional-vs-mandatory-types.graphqls");
new KotlinGraphQLCodegen(schemaFinder.findSchemas(), outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo())
.generate();
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertSameTrimmedContent(new File("src/test/resources/expected-classes/kt/optional/InterfaceWithOptionalField.kt.txt"),
getFileByName(files, "InterfaceWithOptionalField.kt"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/kt/optional/TypeWithMandatoryField.kt.txt"),
getFileByName(files, "TypeWithMandatoryField.kt"));
}
}
......@@ -11,10 +11,7 @@ import org.junit.jupiter.api.Test;
import java.io.File;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import static com.kobylynskyi.graphql.codegen.TestUtils.assertSameTrimmedContent;
import static com.kobylynskyi.graphql.codegen.TestUtils.getFileByName;
......@@ -217,4 +214,29 @@ class GraphQLCodegenExtendTest {
new File("src/test/resources/expected-classes/scala/extend/nullable/QueryResolver.scala.txt"),
getFileByName(files, "QueryResolver.scala"));
}
/**
* Verify Int! to override Int in graphql.
* In scala, Int type cannot be null, So, option Int is Option[Int]. Can not override from Option[Int] to Int.
*/
@Test
void generate_OptionalFieldInInterfaceAndMandatoryInType() throws Exception {
mappingConfig.setGenerateBuilder(true);
mappingConfig.setGenerateToString(true);
mappingConfig.setGenerateEqualsAndHashCode(true);
Map<String, String> maps = new HashMap<>();
maps.put("Int", "Int");// Now, That's the only way to do it.
mappingConfig.setCustomTypesMapping(maps);
schemaFinder.setIncludePattern("optional-vs-mandatory-types.graphqls");
new ScalaGraphQLCodegen(schemaFinder.findSchemas(), outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo())
.generate();
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertSameTrimmedContent(new File("src/test/resources/expected-classes/scala/optional/InterfaceWithOptionalField.scala.txt"),
getFileByName(files, "InterfaceWithOptionalField.scala"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/scala/optional/TypeWithMandatoryField.scala.txt"),
getFileByName(files, "TypeWithMandatoryField.scala"));
}
}
\ No newline at end of file
......@@ -87,4 +87,32 @@ class GraphQLCodegenReactorToStringTest {
}
}
@Test
void generate_SetGenerateClient_False() throws Exception {
mappingConfig.setPackageName("com.kobylynskyi.graphql.codegen.prot");
mappingConfig.setGenerateEqualsAndHashCode(true);
mappingConfig.setGenerateClient(false);
mappingConfig.setGenerateToString(true);
mappingConfig.setUseObjectMapperForRequestSerialization(singleton("TestEnum"));
mappingConfig.putCustomTypeMappingIfAbsent("DateTime", "java.time.ZonedDateTime");
mappingConfig.setGenerateApis(true);
mappingConfig.setGenerateModelsForRootTypes(true);
mappingConfig.setApiNameSuffix("API");
new ScalaGraphQLCodegen(singletonList("src/test/resources/schemas/scala/restricted-words.graphqls"),
outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
List<String> generatedFileNames = Arrays.stream(files).map(File::getName).filter(f -> Objects.equals("Synchronized.scala", f)).sorted().collect(toList());
assertEquals(singletonList("Synchronized.scala"), generatedFileNames);
for (File file : files) {
if (Objects.equals("Synchronized.scala", file.getName())) {
assertSameTrimmedContent(
new File(String.format("src/test/resources/expected-classes/scala/tostring/%s.txt", "TOSTRING_Synchronized.scala")),
file);
}
}
}
}
@javax.annotation.Generated(
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
date = "2020-12-31T23:59:59-0500"
)
interface InterfaceWithOptionalField {
val test: Int?
}
\ No newline at end of file
import java.util.StringJoiner
@javax.annotation.Generated(
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
date = "2020-12-31T23:59:59-0500"
)
data class TypeWithMandatoryField(
override
val test: Int
) : InterfaceWithOptionalField {
companion object {
fun builder(): Builder = Builder()
}
// In the future, it maybe change.
override fun toString(): String {
val joiner = StringJoiner(", ", "{ ", " }")
joiner.add("test: " + test);
return joiner.toString()
}
class Builder {
private var test: Int = 0
fun setTest(test: Int): Builder {
this.test = test
return this
}
fun build(): TypeWithMandatoryField {
return TypeWithMandatoryField(test)
}
}
}
\ No newline at end of file
@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
trait InterfaceWithOptionalField {
val test: Int
}
\ No newline at end of file
import scala.collection.JavaConverters._
@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
case class TypeWithMandatoryField(
override val test: Int
) extends InterfaceWithOptionalField {
override def toString(): String = {
Seq(
"test: " + test
).filter(_ != "").mkString("{", ",", "}")
}
}
object TypeWithMandatoryField {
def builder(): TypeWithMandatoryField.Builder = new Builder()
class Builder {
private var test: Int = _
def setTest(test: Int): Builder = {
this.test = test
this
}
def build(): TypeWithMandatoryField = TypeWithMandatoryField(test)
}
}
\ No newline at end of file
......@@ -10,7 +10,7 @@ import TestEnum._
)
case class Synchronized(
void: String,
wait: Char,
Wait: Char,
`this`: Char,
`super`: Char,
`private`: Char,
......@@ -24,7 +24,7 @@ case class Synchronized(
override def toString(): String = {
Seq(
if (void != null) "void: " + GraphQLRequestSerializer.getEntry(void) else "",
"wait: " + GraphQLRequestSerializer.getEntry(wait),
"wait: " + GraphQLRequestSerializer.getEntry(Wait),
"this: " + GraphQLRequestSerializer.getEntry(`this`),
"super: " + GraphQLRequestSerializer.getEntry(`super`),
"private: " + GraphQLRequestSerializer.getEntry(`private`),
......@@ -44,7 +44,7 @@ object Synchronized {
class Builder {
private var void: String = _
private var wait: Char = _
private var Wait: Char = _
private var `this`: Char = _
private var `super`: Char = _
private var `private`: Char = _
......@@ -59,8 +59,8 @@ object Synchronized {
this
}
def setWait(wait: Char): Builder = {
this.wait = wait
def setWait(Wait: Char): Builder = {
this.Wait = Wait
this
}
......@@ -104,7 +104,7 @@ object Synchronized {
this
}
def build(): Synchronized = Synchronized(void, wait, `this`, `super`, `private`, native, that, enum, Synchronized, date)
def build(): Synchronized = Synchronized(void, Wait, `this`, `super`, `private`, native, that, enum, Synchronized, date)
}
}
\ No newline at end of file
package com.kobylynskyi.graphql.codegen.prot
import scala.collection.JavaConverters._
import TestEnum._
@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
case class Synchronized(
void: String,
Wait: Char,
`this`: Char,
`super`: Char,
`private`: Char,
native: Char,
that: Char,
enum: TestEnum,
Synchronized: Synchronized,
date: java.time.ZonedDateTime
) {
override def toString(): String = {
Seq(
if (void != null) "void: \"void\"" else "",
"wait: " + Wait,
"this: " + `this`,
"super: " + `super`,
"private: " + `private`,
"native: " + native,
"that: " + that,
if (enum != null) "enum: enum" else "",
if (Synchronized != null) "Synchronized: Synchronized" else "",
if (date != null) "date: date" else ""
).filter(_ != "").mkString("{", ",", "}")
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册