From 25ef17400eb1e258ba5531f3cdcbc4f49300e19a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=A6=E5=A2=83=E8=BF=B7=E7=A6=BB?= Date: Sun, 21 Mar 2021 20:47:44 +0800 Subject: [PATCH] Kotlin, Scala: Fix Parametrized Input serialization #566 (#605) --- .../kotlinClassGraphqlParametrizedInput.ftl | 4 +-- .../scalaClassGraphqlParametrizedInput.ftl | 4 +-- ...nRestrictedWordsAndParameterInputTest.java | 8 +++-- ...odegenRestrictedWordsAndToStringTest.java} | 4 +-- .../QueryPrivateParametrizedInput.kt.txt | 36 +++++++++++++++++++ .../kt/restricted-words/Super.kt.txt | 6 ++-- .../QueryPrivateParametrizedInput.scala.txt | 6 ++-- .../scala/tostring/Synchronized.scala.txt | 4 +-- .../schemas/kt/restricted-words.graphqls | 2 +- .../schemas/scala/restricted-words.graphqls | 2 +- 10 files changed, 58 insertions(+), 18 deletions(-) rename src/test/java/com/kobylynskyi/graphql/codegen/scala/{GraphQLCodegenReactorToStringTest.java => GraphQLCodegenRestrictedWordsAndToStringTest.java} (97%) create mode 100644 src/test/resources/expected-classes/kt/restricted-words/QueryPrivateParametrizedInput.kt.txt diff --git a/src/main/resources/templates/kotlin-lang/kotlinClassGraphqlParametrizedInput.ftl b/src/main/resources/templates/kotlin-lang/kotlinClassGraphqlParametrizedInput.ftl index 942519dd..7d11b654 100755 --- a/src/main/resources/templates/kotlin-lang/kotlinClassGraphqlParametrizedInput.ftl +++ b/src/main/resources/templates/kotlin-lang/kotlinClassGraphqlParametrizedInput.ftl @@ -43,10 +43,10 @@ data class ${className}( <#list fields as field> <#if field.type?ends_with("?")> if (${field.name} != null) { - joiner.add("${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name})) + joiner.add("${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name}<#if field.serializeUsingObjectMapper>, true)) } <#else> - joiner.add("${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name})) + joiner.add("${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name}<#if field.serializeUsingObjectMapper>, true)) return joiner.toString() diff --git a/src/main/resources/templates/scala-lang/scalaClassGraphqlParametrizedInput.ftl b/src/main/resources/templates/scala-lang/scalaClassGraphqlParametrizedInput.ftl index b18ad99a..71103289 100644 --- a/src/main/resources/templates/scala-lang/scalaClassGraphqlParametrizedInput.ftl +++ b/src/main/resources/templates/scala-lang/scalaClassGraphqlParametrizedInput.ftl @@ -57,8 +57,8 @@ case class ${className}( override def toString(): String = {<#--There is no Option[Seq[T]], Format is not supported in the generated code, so it is very difficult to write template for this format.--> <#if fields?has_content> scala.Seq(<#list fields as field><#assign getMethod = ".get"><#assign asJava = ".asJava"> - <#if MapperUtil.isScalaPrimitive(field.type)>"${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name})<#if field_has_next>,<#elseif MapperUtil.isScalaOption(field.type)>if (${field.name}.isDefined) "${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name}${getMethod}) else ""<#if field_has_next>,<#else>if (${field.name} != null)<#if MapperUtil.isScalaCollection(field.type)> "${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name}${asJava}) else ""<#if field_has_next>,<#else> "${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name}) else ""<#if field_has_next>, - ).filter(_ != "").mkString("(", ",", ")") + <#if MapperUtil.isScalaPrimitive(field.type)>"${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name}<#if field.serializeUsingObjectMapper>, true)<#if field_has_next>,<#elseif MapperUtil.isScalaOption(field.type)>if (${field.name}.isDefined) "${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name}${getMethod}<#if field.serializeUsingObjectMapper>, true) else ""<#if field_has_next>,<#else>if (${field.name} != null)<#if MapperUtil.isScalaCollection(field.type)> "${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name}${asJava}<#if field.serializeUsingObjectMapper>, true) else ""<#if field_has_next>,<#else> "${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name}<#if field.serializeUsingObjectMapper>, true) else ""<#if field_has_next>, + ).filter(_ != "").mkString("(", ",", ")")<#--Copy it out and modify the code to one line after modification.--> <#else> "()" diff --git a/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenRestrictedWordsAndParameterInputTest.java b/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenRestrictedWordsAndParameterInputTest.java index b1d3df2b..04d6dd34 100644 --- a/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenRestrictedWordsAndParameterInputTest.java +++ b/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenRestrictedWordsAndParameterInputTest.java @@ -13,6 +13,7 @@ import java.util.List; import java.util.Objects; import static com.kobylynskyi.graphql.codegen.TestUtils.assertSameTrimmedContent; +import static java.util.Collections.singleton; import static java.util.Collections.singletonList; import static java.util.stream.Collectors.toList; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -39,19 +40,20 @@ class GraphQLCodegenRestrictedWordsAndParameterInputTest { mappingConfig.setGenerateToString(true); mappingConfig.setGenerateModelsForRootTypes(true); mappingConfig.setApiNameSuffix("API"); - + mappingConfig.putCustomTypeMappingIfAbsent("DateTime", "java.time.ZonedDateTime"); + mappingConfig.setUseObjectMapperForRequestSerialization(singleton("DateTime")); new KotlinGraphQLCodegen(singletonList("src/test/resources/schemas/kt/restricted-words.graphqls"), outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate(); File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles()); List filters = Arrays .asList("Char.kt", "CharResponseProjection.kt", "FunQueryRequest.kt", "FunQueryResponse.kt", - "QueryFunParametrizedInput.kt", + "QueryFunParametrizedInput.kt", "QueryPrivateParametrizedInput.kt", "Super.kt", "TestEnum.kt", "WhenQueryAPI.kt"); List generatedFileNames = Arrays.stream(files).map(File::getName).filter(f -> filters.contains(f)) .sorted().collect(toList()); assertEquals(Arrays.asList("Char.kt", "CharResponseProjection.kt", "FunQueryRequest.kt", "FunQueryResponse.kt", - "QueryFunParametrizedInput.kt", + "QueryFunParametrizedInput.kt", "QueryPrivateParametrizedInput.kt", "Super.kt", "TestEnum.kt", "WhenQueryAPI.kt"), generatedFileNames); for (File file : files) { diff --git a/src/test/java/com/kobylynskyi/graphql/codegen/scala/GraphQLCodegenReactorToStringTest.java b/src/test/java/com/kobylynskyi/graphql/codegen/scala/GraphQLCodegenRestrictedWordsAndToStringTest.java similarity index 97% rename from src/test/java/com/kobylynskyi/graphql/codegen/scala/GraphQLCodegenReactorToStringTest.java rename to src/test/java/com/kobylynskyi/graphql/codegen/scala/GraphQLCodegenRestrictedWordsAndToStringTest.java index f5d0cd58..8c2f04dc 100644 --- a/src/test/java/com/kobylynskyi/graphql/codegen/scala/GraphQLCodegenReactorToStringTest.java +++ b/src/test/java/com/kobylynskyi/graphql/codegen/scala/GraphQLCodegenRestrictedWordsAndToStringTest.java @@ -19,7 +19,7 @@ import static java.util.Collections.singletonList; import static java.util.stream.Collectors.toList; import static org.junit.jupiter.api.Assertions.assertEquals; -class GraphQLCodegenReactorToStringTest { +class GraphQLCodegenRestrictedWordsAndToStringTest { private final MappingConfig mappingConfig = new MappingConfig(); @@ -73,7 +73,7 @@ class GraphQLCodegenReactorToStringTest { mappingConfig.setGenerateApis(true); mappingConfig.setGenerateModelsForRootTypes(true); mappingConfig.setApiNameSuffix("API"); - + mappingConfig.setUseObjectMapperForRequestSerialization(singleton("DateTime")); new ScalaGraphQLCodegen(singletonList("src/test/resources/schemas/scala/restricted-words.graphqls"), outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate(); diff --git a/src/test/resources/expected-classes/kt/restricted-words/QueryPrivateParametrizedInput.kt.txt b/src/test/resources/expected-classes/kt/restricted-words/QueryPrivateParametrizedInput.kt.txt new file mode 100644 index 00000000..63367fbb --- /dev/null +++ b/src/test/resources/expected-classes/kt/restricted-words/QueryPrivateParametrizedInput.kt.txt @@ -0,0 +1,36 @@ +package com.kobylynskyi.graphql.codegen.prot + +import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLParametrizedInput +import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLRequestSerializer +import java.util.StringJoiner +/** + * Parametrized input for field private in type Query + */ +@javax.annotation.Generated( + value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"], + date = "2020-12-31T23:59:59-0500" +) +data class QueryPrivateParametrizedInput( + val int: Int?, + val new: String?, + val enum: TestEnum? = TestEnum.long, + val createdAfter: java.time.ZonedDateTime? +) : GraphQLParametrizedInput { + + override fun toString(): String { + val joiner = StringJoiner(", ", "( ", " )") + if (int != null) { + joiner.add("int: " + GraphQLRequestSerializer.getEntry(int)) + } + if (new != null) { + joiner.add("new: " + GraphQLRequestSerializer.getEntry(new)) + } + if (enum != null) { + joiner.add("enum: " + GraphQLRequestSerializer.getEntry(enum)) + } + if (createdAfter != null) { + joiner.add("createdAfter: " + GraphQLRequestSerializer.getEntry(createdAfter, true)) + } + return joiner.toString() + } +} \ No newline at end of file diff --git a/src/test/resources/expected-classes/kt/restricted-words/Super.kt.txt b/src/test/resources/expected-classes/kt/restricted-words/Super.kt.txt index cdb0f999..368e5c49 100644 --- a/src/test/resources/expected-classes/kt/restricted-words/Super.kt.txt +++ b/src/test/resources/expected-classes/kt/restricted-words/Super.kt.txt @@ -12,7 +12,7 @@ data class Super( val `is`: String?, val `in`: Char?, val Int: Super?, - val date: String? + val date: java.time.ZonedDateTime? ) { companion object { @@ -42,7 +42,7 @@ data class Super( private var `is`: String? = null private var `in`: Char? = null private var Int: Super? = null - private var date: String? = null + private var date: java.time.ZonedDateTime? = null @Deprecated(message = "We have decided that this is not canon") fun setIs(`is`: String?): Builder { @@ -60,7 +60,7 @@ data class Super( return this } - fun setDate(date: String?): Builder { + fun setDate(date: java.time.ZonedDateTime?): Builder { this.date = date return this } diff --git a/src/test/resources/expected-classes/scala/tostring/QueryPrivateParametrizedInput.scala.txt b/src/test/resources/expected-classes/scala/tostring/QueryPrivateParametrizedInput.scala.txt index 53357d43..4d9c1d10 100644 --- a/src/test/resources/expected-classes/scala/tostring/QueryPrivateParametrizedInput.scala.txt +++ b/src/test/resources/expected-classes/scala/tostring/QueryPrivateParametrizedInput.scala.txt @@ -18,7 +18,8 @@ case class QueryPrivateParametrizedInput( seq1: scala.Seq[scala.Option[Int]], seq2: scala.Seq[Int], `new`: String, - enum: TestEnum = TestEnum.long + enum: TestEnum = TestEnum.long, + createdAfter: java.time.ZonedDateTime ) extends GraphQLParametrizedInput { override def toString(): String = { @@ -28,7 +29,8 @@ case class QueryPrivateParametrizedInput( if (seq1 != null) "seq1: " + GraphQLRequestSerializer.getEntry(seq1.asJava) else "", if (seq2 != null) "seq2: " + GraphQLRequestSerializer.getEntry(seq2.asJava) else "", if (`new` != null) "new: " + GraphQLRequestSerializer.getEntry(`new`) else "", - if (enum != null) "enum: " + GraphQLRequestSerializer.getEntry(enum) else "" + if (enum != null) "enum: " + GraphQLRequestSerializer.getEntry(enum) else "", + if (createdAfter != null) "createdAfter: " + GraphQLRequestSerializer.getEntry(createdAfter, true) else "" ).filter(_ != "").mkString("(", ",", ")") } diff --git a/src/test/resources/expected-classes/scala/tostring/Synchronized.scala.txt b/src/test/resources/expected-classes/scala/tostring/Synchronized.scala.txt index 17650355..87995c11 100644 --- a/src/test/resources/expected-classes/scala/tostring/Synchronized.scala.txt +++ b/src/test/resources/expected-classes/scala/tostring/Synchronized.scala.txt @@ -30,9 +30,9 @@ case class Synchronized( "private: " + GraphQLRequestSerializer.getEntry(`private`), "native: " + GraphQLRequestSerializer.getEntry(native), "that: " + GraphQLRequestSerializer.getEntry(that), - if (enum != null) "enum: " + GraphQLRequestSerializer.getEntry(enum, true) else "", + if (enum != null) "enum: " + GraphQLRequestSerializer.getEntry(enum) else "", if (Synchronized != null) "Synchronized: " + GraphQLRequestSerializer.getEntry(Synchronized) else "", - if (date != null) "date: " + GraphQLRequestSerializer.getEntry(date) else "" + if (date != null) "date: " + GraphQLRequestSerializer.getEntry(date, true) else "" ).filter(_ != "").mkString("{", ",", "}") } } diff --git a/src/test/resources/schemas/kt/restricted-words.graphqls b/src/test/resources/schemas/kt/restricted-words.graphqls index ab081cda..7fc80dd9 100644 --- a/src/test/resources/schemas/kt/restricted-words.graphqls +++ b/src/test/resources/schemas/kt/restricted-words.graphqls @@ -4,7 +4,7 @@ schema { type Query { open: String - private(int: Int, new: String, enum: TestEnum = long): super + private(int: Int, new: String, enum: TestEnum = long, createdAfter: DateTime): super when(final: [char]): ID fun(final: Int): char super: String diff --git a/src/test/resources/schemas/scala/restricted-words.graphqls b/src/test/resources/schemas/scala/restricted-words.graphqls index 5f0674d5..ef36c85b 100644 --- a/src/test/resources/schemas/scala/restricted-words.graphqls +++ b/src/test/resources/schemas/scala/restricted-words.graphqls @@ -4,7 +4,7 @@ schema { type Query { native: String - private(int: Int!, intOpt:Int, seq1:[Int], seq2:[Int!], new: String, enum: TestEnum = long): Synchronized + private(int: Int!, intOpt:Int, seq1:[Int], seq2:[Int!], new: String, enum: TestEnum = long, createdAfter: DateTime): Synchronized case(final: [char]): ID int(final: Int): char super: String -- GitLab