From a46d5f6f4e63b6ccfee93734a525a7dca81ceaec Mon Sep 17 00:00:00 2001 From: matsudamper Date: Mon, 1 May 2023 21:33:02 +0900 Subject: [PATCH 1/8] support placeholder at apiReturnType --- .../graphql/codegen/java/JavaGraphQLTypeMapper.java | 8 +++++++- .../graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java | 8 +++++++- .../graphql/codegen/model/MappingConfigConstants.java | 1 + .../graphql/codegen/scala/ScalaGraphQLTypeMapper.java | 8 +++++++- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/java/JavaGraphQLTypeMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/java/JavaGraphQLTypeMapper.java index 177e74fe5..f874cff82 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/java/JavaGraphQLTypeMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/java/JavaGraphQLTypeMapper.java @@ -2,6 +2,7 @@ import com.kobylynskyi.graphql.codegen.mapper.DataModelMapper; import com.kobylynskyi.graphql.codegen.mapper.GraphQLTypeMapper; +import com.kobylynskyi.graphql.codegen.model.MappingConfigConstants; import com.kobylynskyi.graphql.codegen.model.MappingContext; import com.kobylynskyi.graphql.codegen.model.NamedDefinition; import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperation; @@ -69,7 +70,12 @@ public String wrapApiReturnTypeIfRequired(MappingContext mappingContext, } if (Utils.isNotBlank(mappingContext.getApiReturnType())) { // in case it is query/mutation and apiReturnType is set - return getGenericsString(mappingContext, mappingContext.getApiReturnType(), computedTypeName); + if (mappingContext.getApiReturnType().contains(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER)) { + return mappingContext.getApiReturnType() + .replace(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER, computedTypeName); + } else { + return getGenericsString(mappingContext, mappingContext.getApiReturnType(), computedTypeName); + } } return getTypeConsideringPrimitive(mappingContext, namedDefinition, computedTypeName); } diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java index f4dcce3e8..02befbc2f 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java @@ -2,6 +2,7 @@ import com.kobylynskyi.graphql.codegen.mapper.DataModelMapper; import com.kobylynskyi.graphql.codegen.mapper.GraphQLTypeMapper; +import com.kobylynskyi.graphql.codegen.model.MappingConfigConstants; import com.kobylynskyi.graphql.codegen.model.MappingContext; import com.kobylynskyi.graphql.codegen.model.NamedDefinition; import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperation; @@ -96,7 +97,12 @@ public String wrapApiReturnTypeIfRequired(MappingContext mappingContext, } if (Utils.isNotBlank(mappingContext.getApiReturnType())) { // in case it is query/mutation and apiReturnType is set - return getGenericsString(mappingContext, mappingContext.getApiReturnType(), computedTypeName); + if (mappingContext.getApiReturnType().contains(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER)) { + return mappingContext.getApiReturnType() + .replace(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER, computedTypeName); + } else { + return getGenericsString(mappingContext, mappingContext.getApiReturnType(), computedTypeName); + } } return getTypeConsideringPrimitive(mappingContext, namedDefinition, computedTypeName); } diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfigConstants.java b/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfigConstants.java index d940f0fab..8a0ef020e 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfigConstants.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfigConstants.java @@ -9,6 +9,7 @@ public class MappingConfigConstants { public static final String DEFAULT_VALIDATION_ANNOTATION = "javax.validation.constraints.NotNull"; public static final String PARENT_INTERFACE_TYPE_PLACEHOLDER = "{{TYPE}}"; public static final String TYPE_NAME_PLACEHOLDER = "{{TYPE_NAME}}"; + public static final String API_RETURN_NAME_PLACEHOLDER = "{{TYPE}}"; public static final boolean DEFAULT_GENERATE_APIS = true; public static final String DEFAULT_GENERATE_APIS_STRING = "true"; diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaGraphQLTypeMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaGraphQLTypeMapper.java index 2967d9a26..3146ecb84 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaGraphQLTypeMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaGraphQLTypeMapper.java @@ -1,6 +1,7 @@ package com.kobylynskyi.graphql.codegen.scala; import com.kobylynskyi.graphql.codegen.mapper.GraphQLTypeMapper; +import com.kobylynskyi.graphql.codegen.model.MappingConfigConstants; import com.kobylynskyi.graphql.codegen.model.MappingContext; import com.kobylynskyi.graphql.codegen.model.NamedDefinition; import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperation; @@ -76,7 +77,12 @@ public String wrapApiReturnTypeIfRequired(MappingContext mappingContext, } if (Utils.isNotBlank(mappingContext.getApiReturnType())) { // in case it is query/mutation and apiReturnType is set - return getGenericsString(mappingContext, mappingContext.getApiReturnType(), computedTypeName); + if (mappingContext.getApiReturnType().contains(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER)) { + return mappingContext.getApiReturnType() + .replace(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER, computedTypeName); + } else { + return getGenericsString(mappingContext, mappingContext.getApiReturnType(), computedTypeName); + } } return getTypeConsideringPrimitive(mappingContext, namedDefinition, computedTypeName); } From 1c0914c643548a40bebe7547ac717ae38915b6e8 Mon Sep 17 00:00:00 2001 From: matsudamper Date: Thu, 4 May 2023 02:00:38 +0900 Subject: [PATCH 2/8] Add apiReturnType and apiReturnListType placeholder test --- .../GraphQLCodegenApiReturnTypeTest.java | 74 +++++++++++++++++++ .../GraphQLCodegenApiReturnTypeTest.java | 72 ++++++++++++++++++ .../GraphQLCodegenApiReturnTypeTest.java | 74 +++++++++++++++++++ 3 files changed, 220 insertions(+) create mode 100644 src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenApiReturnTypeTest.java create mode 100644 src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenApiReturnTypeTest.java create mode 100644 src/test/java/com/kobylynskyi/graphql/codegen/scala/GraphQLCodegenApiReturnTypeTest.java diff --git a/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenApiReturnTypeTest.java b/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenApiReturnTypeTest.java new file mode 100644 index 000000000..f674988e1 --- /dev/null +++ b/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenApiReturnTypeTest.java @@ -0,0 +1,74 @@ +package com.kobylynskyi.graphql.codegen; + +import com.kobylynskyi.graphql.codegen.java.JavaGraphQLCodegen; +import com.kobylynskyi.graphql.codegen.model.GeneratedLanguage; +import com.kobylynskyi.graphql.codegen.model.MappingConfig; +import com.kobylynskyi.graphql.codegen.scala.ScalaGraphQLCodegen; +import com.kobylynskyi.graphql.codegen.utils.Utils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.IOException; +import java.util.Objects; + +import static com.kobylynskyi.graphql.codegen.TestUtils.assertFileContainsElements; +import static java.util.Collections.singletonList; + +class GraphQLCodegenApiReturnTypeTest { + + private final File outputBuildDir = new File("build/generated"); + private final File outputJavaClassesDir = new File("build/generated/com/kobylynskyi/graphql/test1"); + + private MappingConfig mappingConfig; + + @BeforeEach + void init() { + mappingConfig = new MappingConfig(); + mappingConfig.setPackageName("com.kobylynskyi.graphql.test1"); + mappingConfig.setGeneratedLanguage(GeneratedLanguage.JAVA); + } + + @AfterEach + void cleanup() { + Utils.deleteDir(outputBuildDir); + } + + @Test + void generate_ApiReturnType_WithPlaceHolder() throws Exception { + mappingConfig.setApiReturnType("java.util.concurrent.CompletionStage>"); + + generate("src/test/resources/schemas/test.graphqls"); + + File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles()); + + assertFileContainsElements(files, "EventPropertyResolver.java", + "java.util.concurrent.CompletionStage>> child(EventProperty eventProperty, Integer first, Integer last) throws Exception;"); + + assertFileContainsElements(files, "EventPropertyResolver.java", + "java.util.concurrent.CompletionStage> parent(EventProperty eventProperty, EventStatus withStatus, String createdAfter) throws Exception;"); + } + + @Test + void generate_ApiReturnType_And_ApiReturnListType_WithPlaceHolder() throws Exception { + mappingConfig.setApiReturnType("java.util.concurrent.CompletionStage>"); + mappingConfig.setApiReturnListType("reactor.core.publisher.Mono>"); + + generate("src/test/resources/schemas/test.graphqls"); + + File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles()); + + assertFileContainsElements(files, "EventPropertyResolver.java", + "reactor.core.publisher.Mono> child(EventProperty eventProperty, Integer first, Integer last) throws Exception;"); + + assertFileContainsElements(files, "EventPropertyResolver.java", + "java.util.concurrent.CompletionStage> parent(EventProperty eventProperty, EventStatus withStatus, String createdAfter) throws Exception;"); + } + + private void generate(String path) throws IOException { + new JavaGraphQLCodegen(singletonList(path), outputBuildDir, mappingConfig, + TestUtils.getStaticGeneratedInfo(mappingConfig)).generate(); + } + +} diff --git a/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenApiReturnTypeTest.java b/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenApiReturnTypeTest.java new file mode 100644 index 000000000..a40d6fc1e --- /dev/null +++ b/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenApiReturnTypeTest.java @@ -0,0 +1,72 @@ +package com.kobylynskyi.graphql.codegen.kotlin; + +import com.kobylynskyi.graphql.codegen.TestUtils; +import com.kobylynskyi.graphql.codegen.model.GeneratedLanguage; +import com.kobylynskyi.graphql.codegen.model.MappingConfig; +import com.kobylynskyi.graphql.codegen.utils.Utils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.*; +import java.util.Objects; + +import static com.kobylynskyi.graphql.codegen.TestUtils.assertFileContainsElements; +import static java.util.Collections.*; + +class GraphQLCodegenApiReturnTypeTest { + + private final File outputBuildDir = new File("build/generated"); + private final File outputJavaClassesDir = new File("build/generated/com/kobylynskyi/graphql/test1"); + + private MappingConfig mappingConfig; + + @BeforeEach + void init() { + mappingConfig = new MappingConfig(); + mappingConfig.setPackageName("com.kobylynskyi.graphql.test1"); + mappingConfig.setGeneratedLanguage(GeneratedLanguage.KOTLIN); + } + + @AfterEach + void cleanup() { + Utils.deleteDir(outputBuildDir); + } + + @Test + void generate_ApiReturnType_WithPlaceHolder() throws Exception { + mappingConfig.setApiReturnType("java.util.concurrent.CompletionStage>"); + + generate("src/test/resources/schemas/test.graphqls"); + + File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles()); + + assertFileContainsElements(files, "EventPropertyResolver.kt", + "fun child(eventProperty: EventProperty, first: Int?, last: Int?): java.util.concurrent.CompletionStage?>>"); + + assertFileContainsElements(files, "EventPropertyResolver.kt", + "fun parent(eventProperty: EventProperty, withStatus: EventStatus?, createdAfter: String?): java.util.concurrent.CompletionStage>"); + } + + @Test + void generate_ApiReturnType_And_ApiReturnListType_WithPlaceHolder() throws Exception { + mappingConfig.setApiReturnType("java.util.concurrent.CompletionStage>"); + mappingConfig.setApiReturnListType("reactor.core.publisher.Mono>"); + + generate("src/test/resources/schemas/test.graphqls"); + + File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles()); + + assertFileContainsElements(files, "EventPropertyResolver.kt", + "fun child(eventProperty: EventProperty, first: Int?, last: Int?): reactor.core.publisher.Mono>"); + + assertFileContainsElements(files, "EventPropertyResolver.kt", + "fun parent(eventProperty: EventProperty, withStatus: EventStatus?, createdAfter: String?): java.util.concurrent.CompletionStage>"); + } + + private void generate(String path) throws IOException { + new KotlinGraphQLCodegen(singletonList(path), outputBuildDir, mappingConfig, + TestUtils.getStaticGeneratedInfo(mappingConfig)).generate(); + } + +} diff --git a/src/test/java/com/kobylynskyi/graphql/codegen/scala/GraphQLCodegenApiReturnTypeTest.java b/src/test/java/com/kobylynskyi/graphql/codegen/scala/GraphQLCodegenApiReturnTypeTest.java new file mode 100644 index 000000000..b5ec7447b --- /dev/null +++ b/src/test/java/com/kobylynskyi/graphql/codegen/scala/GraphQLCodegenApiReturnTypeTest.java @@ -0,0 +1,74 @@ +package com.kobylynskyi.graphql.codegen.scala; + +import com.kobylynskyi.graphql.codegen.TestUtils; +import com.kobylynskyi.graphql.codegen.kotlin.KotlinGraphQLCodegen; +import com.kobylynskyi.graphql.codegen.model.GeneratedLanguage; +import com.kobylynskyi.graphql.codegen.model.MappingConfig; +import com.kobylynskyi.graphql.codegen.utils.Utils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.IOException; +import java.util.Objects; + +import static com.kobylynskyi.graphql.codegen.TestUtils.assertFileContainsElements; +import static java.util.Collections.singletonList; + +class GraphQLCodegenApiReturnTypeTest { + + private final File outputBuildDir = new File("build/generated"); + private final File outputJavaClassesDir = new File("build/generated/com/kobylynskyi/graphql/test1"); + + private MappingConfig mappingConfig; + + @BeforeEach + void init() { + mappingConfig = new MappingConfig(); + mappingConfig.setPackageName("com.kobylynskyi.graphql.test1"); + mappingConfig.setGeneratedLanguage(GeneratedLanguage.SCALA); + } + + @AfterEach + void cleanup() { + Utils.deleteDir(outputBuildDir); + } + + @Test + void generate_ApiReturnType_WithPlaceHolder() throws Exception { + mappingConfig.setApiReturnType("java.util.concurrent.CompletionStage[graphql.execution.DataFetcherResult[{{TYPE}}]]"); + + generate("src/test/resources/schemas/test.graphqls"); + + File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles()); + + assertFileContainsElements(files, "EventPropertyResolver.scala", + "def child(eventProperty: EventProperty, first: scala.Option[Int], last: scala.Option[Int]): java.util.concurrent.CompletionStage[graphql.execution.DataFetcherResult[scala.Seq[EventProperty]]]"); + + assertFileContainsElements(files, "EventPropertyResolver.scala", + "def parent(eventProperty: EventProperty, withStatus: EventStatus, createdAfter: String): java.util.concurrent.CompletionStage[graphql.execution.DataFetcherResult[Event]]"); + } + + @Test + void generate_ApiReturnType_And_ApiReturnListType_WithPlaceHolder() throws Exception { + mappingConfig.setApiReturnType("java.util.concurrent.CompletionStage[graphql.execution.DataFetcherResult[{{TYPE}}]]"); + mappingConfig.setApiReturnListType("reactor.core.publisher.Mono[graphql.execution.DataFetcherResult[{{TYPE}}]]"); + + generate("src/test/resources/schemas/test.graphqls"); + + File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles()); + + assertFileContainsElements(files, "EventPropertyResolver.scala", + "def child(eventProperty: EventProperty, first: scala.Option[Int], last: scala.Option[Int]): reactor.core.publisher.Mono[graphql.execution.DataFetcherResult[EventProperty]]"); + + assertFileContainsElements(files, "EventPropertyResolver.scala", + "def parent(eventProperty: EventProperty, withStatus: EventStatus, createdAfter: String): java.util.concurrent.CompletionStage[graphql.execution.DataFetcherResult[Event]]"); + } + + private void generate(String path) throws IOException { + new ScalaGraphQLCodegen(singletonList(path), outputBuildDir, mappingConfig, + TestUtils.getStaticGeneratedInfo(mappingConfig)).generate(); + } + +} From 85c839223eb0a2e76e43a6a09d0485d37a06bf11 Mon Sep 17 00:00:00 2001 From: matsudamper Date: Thu, 4 May 2023 03:26:07 +0900 Subject: [PATCH 3/8] support placeholder at apiReturnListType --- .../graphql/codegen/java/JavaGraphQLTypeMapper.java | 12 +++++++++++- .../codegen/kotlin/KotlinGraphQLTypeMapper.java | 13 ++++++++++++- .../codegen/scala/ScalaGraphQLTypeMapper.java | 13 ++++++++++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/java/JavaGraphQLTypeMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/java/JavaGraphQLTypeMapper.java index f874cff82..a1d362f05 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/java/JavaGraphQLTypeMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/java/JavaGraphQLTypeMapper.java @@ -11,6 +11,8 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static java.util.Arrays.asList; @@ -20,6 +22,7 @@ public class JavaGraphQLTypeMapper extends GraphQLTypeMapper { public static final String JAVA_UTIL_LIST = "java.util.List"; + public static final Pattern JAVA_UTIL_LIST_ELEMENT_REGEX = Pattern.compile("java\\.util\\.List<(.+)>"); private static final String JAVA_UTIL_OPTIONAL = "java.util.Optional"; private static final Set JAVA_PRIMITIVE_TYPES = new HashSet<>(asList( "byte", "short", "int", "long", "float", "double", "char", "boolean")); @@ -66,7 +69,14 @@ public String wrapApiReturnTypeIfRequired(MappingContext mappingContext, if (computedTypeName.startsWith(JAVA_UTIL_LIST) && Utils.isNotBlank(mappingContext.getApiReturnListType())) { // in case it is query/mutation, return type is list and apiReturnListType is set - return computedTypeName.replace(JAVA_UTIL_LIST, mappingContext.getApiReturnListType()); + if (mappingContext.getApiReturnListType().contains(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER)) { + Matcher matcher = JAVA_UTIL_LIST_ELEMENT_REGEX.matcher(computedTypeName); + matcher.find(); + String listElement = matcher.group(1); + return mappingContext.getApiReturnListType().replace(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER, listElement); + } else { + return computedTypeName.replace(JAVA_UTIL_LIST, mappingContext.getApiReturnListType()); + } } if (Utils.isNotBlank(mappingContext.getApiReturnType())) { // in case it is query/mutation and apiReturnType is set diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java index 02befbc2f..2572b975c 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java @@ -10,6 +10,8 @@ import java.util.HashSet; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static java.util.Arrays.asList; @@ -22,6 +24,7 @@ public class KotlinGraphQLTypeMapper extends GraphQLTypeMapper { private static final String KOTLIN_UTIL_LIST = "List"; + public static final Pattern KOTLIN_UTIL_LIST_ELEMENT_REGEX = Pattern.compile("List<(.+?)>"); private static final String KOTLIN_UTIL_NULLABLE = "?"; // Char Boolean are not primitive type, but non null equivalent jvm primitive types. private static final Set KOTLIN_PRIMITIVE_TYPES = new HashSet<>( @@ -93,7 +96,15 @@ public String wrapApiReturnTypeIfRequired(MappingContext mappingContext, if (computedTypeName.startsWith(KOTLIN_UTIL_LIST) && Utils.isNotBlank(mappingContext.getApiReturnListType())) { // in case it is query/mutation, return type is list and apiReturnListType is set - return computedTypeName.replace(KOTLIN_UTIL_LIST, mappingContext.getApiReturnListType()); + if (mappingContext.getApiReturnListType().contains(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER)) { + System.out.println("computedTypeName = " + computedTypeName); + Matcher matcher = KOTLIN_UTIL_LIST_ELEMENT_REGEX.matcher(computedTypeName); + matcher.find(); + String listElement = matcher.group(1); + return mappingContext.getApiReturnListType().replace(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER, listElement); + } else { + return computedTypeName.replace(KOTLIN_UTIL_LIST, mappingContext.getApiReturnListType()); + } } if (Utils.isNotBlank(mappingContext.getApiReturnType())) { // in case it is query/mutation and apiReturnType is set diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaGraphQLTypeMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaGraphQLTypeMapper.java index 3146ecb84..4ba6a0048 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaGraphQLTypeMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaGraphQLTypeMapper.java @@ -9,6 +9,8 @@ import java.util.HashSet; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static com.kobylynskyi.graphql.codegen.java.JavaGraphQLTypeMapper.JAVA_UTIL_LIST; import static java.util.Arrays.asList; @@ -19,6 +21,7 @@ public class ScalaGraphQLTypeMapper extends GraphQLTypeMapper { private static final String SCALA_UTIL_LIST = "scala.Seq"; + private static final Pattern SCALA_UTIL_LIST_ELEMENT_REGEX = Pattern.compile("scala\\.Seq\\[(.+)]"); private static final String SCALA_UTIL_OPTIONAL = "scala.Option"; private static final Set SCALA_PRIMITIVE_TYPES = new HashSet<>(asList( "Byte", "Short", "Int", "Long", "Float", "Double", "Char", "Boolean")); @@ -73,7 +76,15 @@ public String wrapApiReturnTypeIfRequired(MappingContext mappingContext, if (computedTypeName.startsWith(SCALA_UTIL_LIST) && Utils.isNotBlank(mappingContext.getApiReturnListType())) { // in case it is query/mutation, return type is list and apiReturnListType is set - return computedTypeName.replace(SCALA_UTIL_LIST, mappingContext.getApiReturnListType()); + if (mappingContext.getApiReturnListType().contains(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER)) { + System.out.println("computedTypeName = " + computedTypeName); + Matcher matcher = SCALA_UTIL_LIST_ELEMENT_REGEX.matcher(computedTypeName); + matcher.find(); + String listElement = matcher.group(1); + return mappingContext.getApiReturnListType().replace(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER, listElement); + } else { + return computedTypeName.replace(SCALA_UTIL_LIST, mappingContext.getApiReturnListType()); + } } if (Utils.isNotBlank(mappingContext.getApiReturnType())) { // in case it is query/mutation and apiReturnType is set From 25afb7809998011467e367db24f6959a0f3f05cb Mon Sep 17 00:00:00 2001 From: matsudamper Date: Thu, 4 May 2023 04:27:59 +0900 Subject: [PATCH 4/8] fix style --- .../codegen/java/JavaGraphQLTypeMapper.java | 4 +- .../kotlin/KotlinGraphQLTypeMapper.java | 4 +- .../codegen/scala/ScalaGraphQLTypeMapper.java | 4 +- .../GraphQLCodegenApiReturnTypeTest.java | 59 ++++++++++++---- .../GraphQLCodegenApiReturnTypeTest.java | 67 +++++++++++++++---- .../GraphQLCodegenApiReturnTypeTest.java | 63 +++++++++++++---- 6 files changed, 159 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/java/JavaGraphQLTypeMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/java/JavaGraphQLTypeMapper.java index a1d362f05..6775179e2 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/java/JavaGraphQLTypeMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/java/JavaGraphQLTypeMapper.java @@ -73,7 +73,9 @@ public String wrapApiReturnTypeIfRequired(MappingContext mappingContext, Matcher matcher = JAVA_UTIL_LIST_ELEMENT_REGEX.matcher(computedTypeName); matcher.find(); String listElement = matcher.group(1); - return mappingContext.getApiReturnListType().replace(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER, listElement); + return mappingContext.getApiReturnListType().replace( + MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER, + listElement); } else { return computedTypeName.replace(JAVA_UTIL_LIST, mappingContext.getApiReturnListType()); } diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java index 2572b975c..39c074b03 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java @@ -101,7 +101,9 @@ public String wrapApiReturnTypeIfRequired(MappingContext mappingContext, Matcher matcher = KOTLIN_UTIL_LIST_ELEMENT_REGEX.matcher(computedTypeName); matcher.find(); String listElement = matcher.group(1); - return mappingContext.getApiReturnListType().replace(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER, listElement); + return mappingContext.getApiReturnListType().replace( + MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER, + listElement); } else { return computedTypeName.replace(KOTLIN_UTIL_LIST, mappingContext.getApiReturnListType()); } diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaGraphQLTypeMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaGraphQLTypeMapper.java index 4ba6a0048..99504094d 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaGraphQLTypeMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaGraphQLTypeMapper.java @@ -81,7 +81,9 @@ public String wrapApiReturnTypeIfRequired(MappingContext mappingContext, Matcher matcher = SCALA_UTIL_LIST_ELEMENT_REGEX.matcher(computedTypeName); matcher.find(); String listElement = matcher.group(1); - return mappingContext.getApiReturnListType().replace(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER, listElement); + return mappingContext.getApiReturnListType().replace( + MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER, + listElement); } else { return computedTypeName.replace(SCALA_UTIL_LIST, mappingContext.getApiReturnListType()); } diff --git a/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenApiReturnTypeTest.java b/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenApiReturnTypeTest.java index f674988e1..1e1fce449 100644 --- a/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenApiReturnTypeTest.java +++ b/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenApiReturnTypeTest.java @@ -3,7 +3,6 @@ import com.kobylynskyi.graphql.codegen.java.JavaGraphQLCodegen; import com.kobylynskyi.graphql.codegen.model.GeneratedLanguage; import com.kobylynskyi.graphql.codegen.model.MappingConfig; -import com.kobylynskyi.graphql.codegen.scala.ScalaGraphQLCodegen; import com.kobylynskyi.graphql.codegen.utils.Utils; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -37,33 +36,69 @@ void cleanup() { @Test void generate_ApiReturnType_WithPlaceHolder() throws Exception { - mappingConfig.setApiReturnType("java.util.concurrent.CompletionStage>"); + mappingConfig.setApiReturnType( + "java.util.concurrent.CompletionStage>" + ); generate("src/test/resources/schemas/test.graphqls"); File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles()); + String requireChildText = getChildText( + "java.util.concurrent.CompletionStage>>" + ); + assertFileContainsElements( + files, + "EventPropertyResolver.java", + requireChildText + ); + + String requireParentText = getParentText( + "java.util.concurrent.CompletionStage>" + ); assertFileContainsElements(files, "EventPropertyResolver.java", - "java.util.concurrent.CompletionStage>> child(EventProperty eventProperty, Integer first, Integer last) throws Exception;"); - - assertFileContainsElements(files, "EventPropertyResolver.java", - "java.util.concurrent.CompletionStage> parent(EventProperty eventProperty, EventStatus withStatus, String createdAfter) throws Exception;"); + requireParentText + ); } @Test void generate_ApiReturnType_And_ApiReturnListType_WithPlaceHolder() throws Exception { - mappingConfig.setApiReturnType("java.util.concurrent.CompletionStage>"); - mappingConfig.setApiReturnListType("reactor.core.publisher.Mono>"); + mappingConfig.setApiReturnType( + "java.util.concurrent.CompletionStage>" + ); + mappingConfig.setApiReturnListType( + "reactor.core.publisher.Mono>" + ); generate("src/test/resources/schemas/test.graphqls"); File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles()); - assertFileContainsElements(files, "EventPropertyResolver.java", - "reactor.core.publisher.Mono> child(EventProperty eventProperty, Integer first, Integer last) throws Exception;"); + assertFileContainsElements( + files, + "EventPropertyResolver.java", + getChildText( + "reactor.core.publisher.Mono>" + ) + ); + + assertFileContainsElements( + files, + "EventPropertyResolver.java", + getParentText( + "java.util.concurrent.CompletionStage>" + ) + ); + } - assertFileContainsElements(files, "EventPropertyResolver.java", - "java.util.concurrent.CompletionStage> parent(EventProperty eventProperty, EventStatus withStatus, String createdAfter) throws Exception;"); + private String getChildText(String returnType) { + return returnType + " child(EventProperty eventProperty, Integer first, Integer last) throws Exception;"; + } + + private String getParentText(String returnType) { + return returnType + + " parent(EventProperty eventProperty, EventStatus withStatus, String createdAfter) throws Exception;"; } private void generate(String path) throws IOException { diff --git a/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenApiReturnTypeTest.java b/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenApiReturnTypeTest.java index a40d6fc1e..818c7cdbf 100644 --- a/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenApiReturnTypeTest.java +++ b/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenApiReturnTypeTest.java @@ -8,11 +8,12 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.io.*; +import java.io.File; +import java.io.IOException; import java.util.Objects; import static com.kobylynskyi.graphql.codegen.TestUtils.assertFileContainsElements; -import static java.util.Collections.*; +import static java.util.Collections.singletonList; class GraphQLCodegenApiReturnTypeTest { @@ -35,33 +36,71 @@ void cleanup() { @Test void generate_ApiReturnType_WithPlaceHolder() throws Exception { - mappingConfig.setApiReturnType("java.util.concurrent.CompletionStage>"); + mappingConfig.setApiReturnType( + "java.util.concurrent.CompletionStage>" + ); generate("src/test/resources/schemas/test.graphqls"); File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles()); - assertFileContainsElements(files, "EventPropertyResolver.kt", - "fun child(eventProperty: EventProperty, first: Int?, last: Int?): java.util.concurrent.CompletionStage?>>"); - - assertFileContainsElements(files, "EventPropertyResolver.kt", - "fun parent(eventProperty: EventProperty, withStatus: EventStatus?, createdAfter: String?): java.util.concurrent.CompletionStage>"); + String requireChildText = getChildFunction( + "java.util.concurrent.CompletionStage?>>" + ); + assertFileContainsElements( + files, + "EventPropertyResolver.kt", + requireChildText + ); + + String requireParentText = getParentFunction( + "java.util.concurrent.CompletionStage>" + ); + assertFileContainsElements( + files, + "EventPropertyResolver.kt", + requireParentText + ); } @Test void generate_ApiReturnType_And_ApiReturnListType_WithPlaceHolder() throws Exception { - mappingConfig.setApiReturnType("java.util.concurrent.CompletionStage>"); - mappingConfig.setApiReturnListType("reactor.core.publisher.Mono>"); + mappingConfig.setApiReturnType( + "java.util.concurrent.CompletionStage>" + ); + mappingConfig.setApiReturnListType( + "reactor.core.publisher.Mono>" + ); generate("src/test/resources/schemas/test.graphqls"); File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles()); - assertFileContainsElements(files, "EventPropertyResolver.kt", - "fun child(eventProperty: EventProperty, first: Int?, last: Int?): reactor.core.publisher.Mono>"); + assertFileContainsElements( + files, + "EventPropertyResolver.kt", + getChildFunction( + "reactor.core.publisher.Mono>" + ) + ); + + assertFileContainsElements( + files, + "EventPropertyResolver.kt", + getParentFunction( + "java.util.concurrent.CompletionStage>" + ) + ); + } + + private String getChildFunction(String returnType) { + return "fun child(eventProperty: EventProperty, first: Int?, last: Int?)" + + ": " + returnType; + } - assertFileContainsElements(files, "EventPropertyResolver.kt", - "fun parent(eventProperty: EventProperty, withStatus: EventStatus?, createdAfter: String?): java.util.concurrent.CompletionStage>"); + private String getParentFunction(String returnType) { + return "fun parent(eventProperty: EventProperty, withStatus: EventStatus?, createdAfter: String?)" + + ": " + returnType; } private void generate(String path) throws IOException { diff --git a/src/test/java/com/kobylynskyi/graphql/codegen/scala/GraphQLCodegenApiReturnTypeTest.java b/src/test/java/com/kobylynskyi/graphql/codegen/scala/GraphQLCodegenApiReturnTypeTest.java index b5ec7447b..70d054216 100644 --- a/src/test/java/com/kobylynskyi/graphql/codegen/scala/GraphQLCodegenApiReturnTypeTest.java +++ b/src/test/java/com/kobylynskyi/graphql/codegen/scala/GraphQLCodegenApiReturnTypeTest.java @@ -1,7 +1,6 @@ package com.kobylynskyi.graphql.codegen.scala; import com.kobylynskyi.graphql.codegen.TestUtils; -import com.kobylynskyi.graphql.codegen.kotlin.KotlinGraphQLCodegen; import com.kobylynskyi.graphql.codegen.model.GeneratedLanguage; import com.kobylynskyi.graphql.codegen.model.MappingConfig; import com.kobylynskyi.graphql.codegen.utils.Utils; @@ -37,33 +36,71 @@ void cleanup() { @Test void generate_ApiReturnType_WithPlaceHolder() throws Exception { - mappingConfig.setApiReturnType("java.util.concurrent.CompletionStage[graphql.execution.DataFetcherResult[{{TYPE}}]]"); + mappingConfig.setApiReturnType( + "java.util.concurrent.CompletionStage[graphql.execution.DataFetcherResult[{{TYPE}}]]" + ); generate("src/test/resources/schemas/test.graphqls"); File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles()); - assertFileContainsElements(files, "EventPropertyResolver.scala", - "def child(eventProperty: EventProperty, first: scala.Option[Int], last: scala.Option[Int]): java.util.concurrent.CompletionStage[graphql.execution.DataFetcherResult[scala.Seq[EventProperty]]]"); - - assertFileContainsElements(files, "EventPropertyResolver.scala", - "def parent(eventProperty: EventProperty, withStatus: EventStatus, createdAfter: String): java.util.concurrent.CompletionStage[graphql.execution.DataFetcherResult[Event]]"); + String childRequireText = getChildFunction( + "java.util.concurrent.CompletionStage[graphql.execution.DataFetcherResult[scala.Seq[EventProperty]]]" + ); + assertFileContainsElements( + files, + "EventPropertyResolver.scala", + childRequireText + ); + + String parentRequireText = getParentFunction( + "java.util.concurrent.CompletionStage[graphql.execution.DataFetcherResult[Event]]" + ); + assertFileContainsElements( + files, + "EventPropertyResolver.scala", + parentRequireText + ); } @Test void generate_ApiReturnType_And_ApiReturnListType_WithPlaceHolder() throws Exception { - mappingConfig.setApiReturnType("java.util.concurrent.CompletionStage[graphql.execution.DataFetcherResult[{{TYPE}}]]"); - mappingConfig.setApiReturnListType("reactor.core.publisher.Mono[graphql.execution.DataFetcherResult[{{TYPE}}]]"); + mappingConfig.setApiReturnType( + "java.util.concurrent.CompletionStage[graphql.execution.DataFetcherResult[{{TYPE}}]]" + ); + mappingConfig.setApiReturnListType( + "reactor.core.publisher.Mono[graphql.execution.DataFetcherResult[{{TYPE}}]]" + ); generate("src/test/resources/schemas/test.graphqls"); File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles()); - assertFileContainsElements(files, "EventPropertyResolver.scala", - "def child(eventProperty: EventProperty, first: scala.Option[Int], last: scala.Option[Int]): reactor.core.publisher.Mono[graphql.execution.DataFetcherResult[EventProperty]]"); + assertFileContainsElements( + files, + "EventPropertyResolver.scala", + getChildFunction( + "reactor.core.publisher.Mono[graphql.execution.DataFetcherResult[EventProperty]]" + ) + ); + + assertFileContainsElements( + files, + "EventPropertyResolver.scala", + getParentFunction( + "java.util.concurrent.CompletionStage[graphql.execution.DataFetcherResult[Event]]" + ) + ); + } + + private String getChildFunction(String returnType) { + return "def child(eventProperty: EventProperty, first: scala.Option[Int], last: scala.Option[Int])" + + ": " + returnType; + } - assertFileContainsElements(files, "EventPropertyResolver.scala", - "def parent(eventProperty: EventProperty, withStatus: EventStatus, createdAfter: String): java.util.concurrent.CompletionStage[graphql.execution.DataFetcherResult[Event]]"); + private String getParentFunction(String returnType) { + return "def parent(eventProperty: EventProperty, withStatus: EventStatus, createdAfter: String)" + + ": " + returnType; } private void generate(String path) throws IOException { From 8cca33013d0053f9a111515677ea5e1512740d70 Mon Sep 17 00:00:00 2001 From: matsudamper Date: Thu, 4 May 2023 10:25:24 +0900 Subject: [PATCH 5/8] remove debug print --- .../graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java | 1 - .../graphql/codegen/scala/ScalaGraphQLTypeMapper.java | 1 - 2 files changed, 2 deletions(-) diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java index 39c074b03..ccd33aa4d 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java @@ -97,7 +97,6 @@ public String wrapApiReturnTypeIfRequired(MappingContext mappingContext, Utils.isNotBlank(mappingContext.getApiReturnListType())) { // in case it is query/mutation, return type is list and apiReturnListType is set if (mappingContext.getApiReturnListType().contains(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER)) { - System.out.println("computedTypeName = " + computedTypeName); Matcher matcher = KOTLIN_UTIL_LIST_ELEMENT_REGEX.matcher(computedTypeName); matcher.find(); String listElement = matcher.group(1); diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaGraphQLTypeMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaGraphQLTypeMapper.java index 99504094d..6501ddc69 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaGraphQLTypeMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaGraphQLTypeMapper.java @@ -77,7 +77,6 @@ public String wrapApiReturnTypeIfRequired(MappingContext mappingContext, Utils.isNotBlank(mappingContext.getApiReturnListType())) { // in case it is query/mutation, return type is list and apiReturnListType is set if (mappingContext.getApiReturnListType().contains(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER)) { - System.out.println("computedTypeName = " + computedTypeName); Matcher matcher = SCALA_UTIL_LIST_ELEMENT_REGEX.matcher(computedTypeName); matcher.find(); String listElement = matcher.group(1); From eebc64ad14dbc78dc0547c03a1fb72e0dc3e9699 Mon Sep 17 00:00:00 2001 From: matsudamper Date: Thu, 4 May 2023 10:30:09 +0900 Subject: [PATCH 6/8] consider kotlin nullable --- .../codegen/kotlin/KotlinGraphQLTypeMapper.java | 13 ++++++++++--- .../kotlin/GraphQLCodegenApiReturnTypeTest.java | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java index ccd33aa4d..9dee2db44 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java @@ -97,12 +97,19 @@ public String wrapApiReturnTypeIfRequired(MappingContext mappingContext, Utils.isNotBlank(mappingContext.getApiReturnListType())) { // in case it is query/mutation, return type is list and apiReturnListType is set if (mappingContext.getApiReturnListType().contains(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER)) { + boolean isNullable = computedTypeName.endsWith(KOTLIN_UTIL_NULLABLE); + Matcher matcher = KOTLIN_UTIL_LIST_ELEMENT_REGEX.matcher(computedTypeName); matcher.find(); String listElement = matcher.group(1); - return mappingContext.getApiReturnListType().replace( - MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER, - listElement); + computedTypeName = mappingContext.getApiReturnListType() + .replace(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER, listElement); + + if (isNullable) { + return computedTypeName + "?"; + } else { + return computedTypeName; + } } else { return computedTypeName.replace(KOTLIN_UTIL_LIST, mappingContext.getApiReturnListType()); } diff --git a/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenApiReturnTypeTest.java b/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenApiReturnTypeTest.java index 818c7cdbf..ca9df3c27 100644 --- a/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenApiReturnTypeTest.java +++ b/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenApiReturnTypeTest.java @@ -80,7 +80,7 @@ void generate_ApiReturnType_And_ApiReturnListType_WithPlaceHolder() throws Excep files, "EventPropertyResolver.kt", getChildFunction( - "reactor.core.publisher.Mono>" + "reactor.core.publisher.Mono>?" ) ); From 45f0afd93ede4d43947954f891900537a31e3631 Mon Sep 17 00:00:00 2001 From: matsudamper Date: Thu, 4 May 2023 20:19:24 +0900 Subject: [PATCH 7/8] fix regex --- .../graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java index 9dee2db44..cfe767b42 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java @@ -24,7 +24,7 @@ public class KotlinGraphQLTypeMapper extends GraphQLTypeMapper { private static final String KOTLIN_UTIL_LIST = "List"; - public static final Pattern KOTLIN_UTIL_LIST_ELEMENT_REGEX = Pattern.compile("List<(.+?)>"); + public static final Pattern KOTLIN_UTIL_LIST_ELEMENT_REGEX = Pattern.compile("List<(.+)>"); private static final String KOTLIN_UTIL_NULLABLE = "?"; // Char Boolean are not primitive type, but non null equivalent jvm primitive types. private static final Set KOTLIN_PRIMITIVE_TYPES = new HashSet<>( From 2542b8f5de5bc5660372d7424c60c83b44e2b0c3 Mon Sep 17 00:00:00 2001 From: matsudamper Date: Fri, 12 May 2023 16:41:43 +0900 Subject: [PATCH 8/8] throw errors properly --- .../codegen/java/JavaGraphQLTypeMapper.java | 13 ++++++++----- .../kotlin/KotlinGraphQLTypeMapper.java | 19 +++++++++++-------- .../codegen/scala/ScalaGraphQLTypeMapper.java | 13 ++++++++----- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/java/JavaGraphQLTypeMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/java/JavaGraphQLTypeMapper.java index 6775179e2..223aa953a 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/java/JavaGraphQLTypeMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/java/JavaGraphQLTypeMapper.java @@ -71,11 +71,14 @@ public String wrapApiReturnTypeIfRequired(MappingContext mappingContext, // in case it is query/mutation, return type is list and apiReturnListType is set if (mappingContext.getApiReturnListType().contains(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER)) { Matcher matcher = JAVA_UTIL_LIST_ELEMENT_REGEX.matcher(computedTypeName); - matcher.find(); - String listElement = matcher.group(1); - return mappingContext.getApiReturnListType().replace( - MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER, - listElement); + if (matcher.find()) { + String listElement = matcher.group(1); + return mappingContext.getApiReturnListType().replace( + MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER, + listElement); + } else { + throw new IllegalStateException(); + } } else { return computedTypeName.replace(JAVA_UTIL_LIST, mappingContext.getApiReturnListType()); } diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java index cfe767b42..313d40034 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLTypeMapper.java @@ -100,15 +100,18 @@ public String wrapApiReturnTypeIfRequired(MappingContext mappingContext, boolean isNullable = computedTypeName.endsWith(KOTLIN_UTIL_NULLABLE); Matcher matcher = KOTLIN_UTIL_LIST_ELEMENT_REGEX.matcher(computedTypeName); - matcher.find(); - String listElement = matcher.group(1); - computedTypeName = mappingContext.getApiReturnListType() - .replace(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER, listElement); - - if (isNullable) { - return computedTypeName + "?"; + if (matcher.find()) { + String listElement = matcher.group(1); + computedTypeName = mappingContext.getApiReturnListType() + .replace(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER, listElement); + + if (isNullable) { + return computedTypeName + "?"; + } else { + return computedTypeName; + } } else { - return computedTypeName; + throw new IllegalStateException(); } } else { return computedTypeName.replace(KOTLIN_UTIL_LIST, mappingContext.getApiReturnListType()); diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaGraphQLTypeMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaGraphQLTypeMapper.java index 6501ddc69..15b68d616 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaGraphQLTypeMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaGraphQLTypeMapper.java @@ -78,11 +78,14 @@ public String wrapApiReturnTypeIfRequired(MappingContext mappingContext, // in case it is query/mutation, return type is list and apiReturnListType is set if (mappingContext.getApiReturnListType().contains(MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER)) { Matcher matcher = SCALA_UTIL_LIST_ELEMENT_REGEX.matcher(computedTypeName); - matcher.find(); - String listElement = matcher.group(1); - return mappingContext.getApiReturnListType().replace( - MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER, - listElement); + if (matcher.find()) { + String listElement = matcher.group(1); + return mappingContext.getApiReturnListType().replace( + MappingConfigConstants.API_RETURN_NAME_PLACEHOLDER, + listElement); + } else { + throw new IllegalStateException(); + } } else { return computedTypeName.replace(SCALA_UTIL_LIST, mappingContext.getApiReturnListType()); }