From 2716527e3bf55083e9d75ff27a09634208d63a66 Mon Sep 17 00:00:00 2001 From: Conor Gallagher Date: Wed, 26 Aug 2020 10:48:40 +0100 Subject: [PATCH] Issue #15 Fixing return type on Service interfaces to match the expected return type in generated controllers for query operations. Also adding in spring mvc as a test dependency so compilations issues are visible in the example resources --- build.gradle.kts | 1 + .../generators/service/SpringServiceInterfaceGenerator.kt | 7 +++---- .../kotlin/com/cjbooms/fabrikt/util/OperationUtils.kt | 6 ++---- .../com/cjbooms/fabrikt/generators/ModelGeneratorTest.kt | 2 +- .../fabrikt/generators/SpringControllerGeneratorTest.kt | 6 +++--- .../generators/SpringServiceInterfaceGeneratorTest.kt | 8 ++++---- .../examples/customerExampleApi/{open-api => }/api.yaml | 0 .../examples/customerExampleApi/service/Services.kt | 7 ++++--- .../resources/examples/githubApi/{open-api => }/api.yaml | 0 .../javaSerializableModels/{open-api => }/api.yaml | 0 .../resources/examples/putApi/{open-api => }/api.yaml | 0 .../examples/simpleRequestBody/{open-api => }/api.yaml | 0 12 files changed, 18 insertions(+), 19 deletions(-) rename src/test/resources/examples/customerExampleApi/{open-api => }/api.yaml (100%) rename src/test/resources/examples/githubApi/{open-api => }/api.yaml (100%) rename src/test/resources/examples/javaSerializableModels/{open-api => }/api.yaml (100%) rename src/test/resources/examples/putApi/{open-api => }/api.yaml (100%) rename src/test/resources/examples/simpleRequestBody/{open-api => }/api.yaml (100%) diff --git a/build.gradle.kts b/build.gradle.kts index 7e7469c0..d48adebb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -57,6 +57,7 @@ dependencies { testImplementation("org.assertj:assertj-core:3.14.0") // Below dependencies are solely present so code examples in the test resources dir compile testImplementation("javax.validation:validation-api:2.0.1.Final") + testImplementation("org.springframework:spring-webmvc:5.1.9.RELEASE") } tasks { diff --git a/src/main/kotlin/com/cjbooms/fabrikt/generators/service/SpringServiceInterfaceGenerator.kt b/src/main/kotlin/com/cjbooms/fabrikt/generators/service/SpringServiceInterfaceGenerator.kt index dea9a364..18f3a763 100644 --- a/src/main/kotlin/com/cjbooms/fabrikt/generators/service/SpringServiceInterfaceGenerator.kt +++ b/src/main/kotlin/com/cjbooms/fabrikt/generators/service/SpringServiceInterfaceGenerator.kt @@ -15,7 +15,6 @@ import com.cjbooms.fabrikt.util.KaizenParserExtensions.isSimpleType import com.cjbooms.fabrikt.util.KaizenParserExtensions.routeToPaths import com.cjbooms.fabrikt.util.KaizenParserExtensions.safeName import com.cjbooms.fabrikt.util.OperationUtils -import com.cjbooms.fabrikt.util.OperationUtils.maybeListResponseType import com.cjbooms.fabrikt.util.SupportedOperation import com.reprezen.kaizen.oasparser.model3.Operation import com.reprezen.kaizen.oasparser.model3.Path @@ -148,11 +147,11 @@ class SpringServiceInterfaceGenerator( ) } SupportedOperation.QUERY, SupportedOperation.QUERY_SUBRESOURCE, SupportedOperation.QUERY_TOP_LEVEL_SUBRESOURCE -> { - operation.maybeListResponseType() - ?.let { modelName -> + responseType(operation) + ?.let { responseType -> MethodSignature( name = MethodNames.QUERY, - returnType = queryResultOrListType(modelName, packages.base), + returnType = models.first { it.spec.name == responseType.safeName() }.className, parameters = operation.toIncomingParameters(packages.base) ) } diff --git a/src/main/kotlin/com/cjbooms/fabrikt/util/OperationUtils.kt b/src/main/kotlin/com/cjbooms/fabrikt/util/OperationUtils.kt index bd60fa69..d13cc3c1 100644 --- a/src/main/kotlin/com/cjbooms/fabrikt/util/OperationUtils.kt +++ b/src/main/kotlin/com/cjbooms/fabrikt/util/OperationUtils.kt @@ -78,14 +78,12 @@ object OperationUtils { verb ) && operation.isListResponseType() - fun Operation.isListResponseType(): Boolean = maybeListResponseType() != null - - fun Operation.maybeListResponseType(): String? = responses + fun Operation.isListResponseType(): Boolean = responses .filter { it.key != "default" } .flatMap { resp -> resp.value.contentMediaTypes.map { it.value.schema.properties["items"]?.itemsSchema?.safeName() } } .asSequence() .filterNotNull() - .firstOrNull() + .any() private fun isCreate(verb: String, path: Path): Boolean = isTopLevelResource(path) && !path.isSingleResource() && isPost( diff --git a/src/test/kotlin/com/cjbooms/fabrikt/generators/ModelGeneratorTest.kt b/src/test/kotlin/com/cjbooms/fabrikt/generators/ModelGeneratorTest.kt index 54304bdc..ce4adb67 100644 --- a/src/test/kotlin/com/cjbooms/fabrikt/generators/ModelGeneratorTest.kt +++ b/src/test/kotlin/com/cjbooms/fabrikt/generators/ModelGeneratorTest.kt @@ -85,7 +85,7 @@ class ModelGeneratorTest { @Test fun `serializable models are generated from a full API definition when the java-serialized option is set`() { val basePackage = "examples.javaSerializableModels" - val spec = javaClass.getResource("/examples/javaSerializableModels/open-api/api.yaml").readText() + val spec = javaClass.getResource("/examples/javaSerializableModels/api.yaml").readText() val expectedModels = javaClass.getResource("/examples/javaSerializableModels/models/Models.kt").readText() val models = JacksonModelGenerator(Packages(basePackage), SourceApi(spec), setOf(ModelCodeGenOptionType.JAVA_SERIALIZATION)) diff --git a/src/test/kotlin/com/cjbooms/fabrikt/generators/SpringControllerGeneratorTest.kt b/src/test/kotlin/com/cjbooms/fabrikt/generators/SpringControllerGeneratorTest.kt index c5d19251..94d8ebb6 100644 --- a/src/test/kotlin/com/cjbooms/fabrikt/generators/SpringControllerGeneratorTest.kt +++ b/src/test/kotlin/com/cjbooms/fabrikt/generators/SpringControllerGeneratorTest.kt @@ -31,7 +31,7 @@ class SpringControllerGeneratorTest { ) private fun setupGithubApiTestEnv() { - val api = SourceApi(javaClass.getResource("/examples/githubApi/open-api/api.yaml").readText()) + val api = SourceApi(javaClass.getResource("/examples/githubApi/api.yaml").readText()) val models = JacksonModelGenerator(Packages(basePackage), api).generate().models val services = SpringServiceInterfaceGenerator(Packages(basePackage), api, models).generate().services generated = SpringControllerGenerator(Packages(basePackage), api, services, models).generate().files @@ -117,7 +117,7 @@ class SpringControllerGeneratorTest { @Test fun `ensure that subresource specific controllers are created`() { - val api = SourceApi(javaClass.getResource("/examples/githubApi/open-api/api.yaml").readText()) + val api = SourceApi(javaClass.getResource("/examples/githubApi/api.yaml").readText()) val models = JacksonModelGenerator(Packages(basePackage), api).generate().models val services = SpringServiceInterfaceGenerator(Packages(basePackage), api, models).generate().services val controllers = SpringControllerGenerator(Packages(basePackage), api, services, models).generate() @@ -159,7 +159,7 @@ class SpringControllerGeneratorTest { @MethodSource("testCases") fun `correct models are generated for different OpenApi Specifications`(testCaseName: String) { val basePackage = "examples.$testCaseName" - val api = SourceApi(javaClass.getResource("/examples/$testCaseName/open-api/api.yaml").readText()) + val api = SourceApi(javaClass.getResource("/examples/$testCaseName/api.yaml").readText()) val expectedControllers = javaClass.getResource("/examples/$testCaseName/controllers/Controllers.kt").readText() val models = JacksonModelGenerator(Packages(basePackage), api).generate().models diff --git a/src/test/kotlin/com/cjbooms/fabrikt/generators/SpringServiceInterfaceGeneratorTest.kt b/src/test/kotlin/com/cjbooms/fabrikt/generators/SpringServiceInterfaceGeneratorTest.kt index 6272fe5e..1b24c4b9 100644 --- a/src/test/kotlin/com/cjbooms/fabrikt/generators/SpringServiceInterfaceGeneratorTest.kt +++ b/src/test/kotlin/com/cjbooms/fabrikt/generators/SpringServiceInterfaceGeneratorTest.kt @@ -21,7 +21,7 @@ import org.junit.jupiter.params.provider.MethodSource @TestInstance(TestInstance.Lifecycle.PER_CLASS) class SpringServiceInterfaceGeneratorTest { - val api = SourceApi(javaClass.getResource("/examples/githubApi/open-api/api.yaml").readText()) + val api = SourceApi(javaClass.getResource("/examples/githubApi/api.yaml").readText()) private fun testCases(): Stream = Stream.of( "putApi", @@ -70,7 +70,7 @@ class SpringServiceInterfaceGeneratorTest { @Test fun `test service operations map correctly`() { - val apiSeed = SourceApi(javaClass.getResource("/examples/githubApi/open-api/api.yaml").readText()) + val apiSeed = SourceApi(javaClass.getResource("/examples/githubApi/api.yaml").readText()) assertThat(supportedOperationFrom("get", "/contributors/{id}", apiSeed)) .isEqualTo(SupportedOperation.READ) @@ -226,7 +226,7 @@ class SpringServiceInterfaceGeneratorTest { val get = methods.first { it.name == "read" } val modelsPackage = modelsPackage(basePackage) - assertThat(list.returnType.toString()).isEqualTo("kotlin.collections.List<$modelsPackage.Contributor>") + assertThat(list.returnType.toString()).isEqualTo("$modelsPackage.ContributorQueryResult") assertThat(create.returnType.toString()).isEqualTo("kotlin.Pair") assertThat(get.returnType.toString()).isEqualTo("$modelsPackage.Contributor") assertThat(update.returnType.toString()).isEqualTo("$modelsPackage.Contributor") @@ -241,7 +241,7 @@ class SpringServiceInterfaceGeneratorTest { @MethodSource("testCases") fun `correct services are generated for different OpenApi Specifications`(testCaseName: String) { val basePackage = "examples.${testCaseName.decapitalize()}" - val api = SourceApi(javaClass.getResource("/examples/$testCaseName/open-api/api.yaml").readText()) + val api = SourceApi(javaClass.getResource("/examples/$testCaseName/api.yaml").readText()) val expectedServices = javaClass.getResource("/examples/$testCaseName/service/Services.kt").readText() val jackson = JacksonModelGenerator(Packages(basePackage), api).generate() diff --git a/src/test/resources/examples/customerExampleApi/open-api/api.yaml b/src/test/resources/examples/customerExampleApi/api.yaml similarity index 100% rename from src/test/resources/examples/customerExampleApi/open-api/api.yaml rename to src/test/resources/examples/customerExampleApi/api.yaml diff --git a/src/test/resources/examples/customerExampleApi/service/Services.kt b/src/test/resources/examples/customerExampleApi/service/Services.kt index e37e63d6..8c448f00 100644 --- a/src/test/resources/examples/customerExampleApi/service/Services.kt +++ b/src/test/resources/examples/customerExampleApi/service/Services.kt @@ -2,12 +2,13 @@ package examples.customerExampleApi.service import examples.customerExampleApi.models.Customer import examples.customerExampleApi.models.DataAccessRequest +import examples.customerExampleApi.models.DataAccessRequestQueryResult import examples.customerExampleApi.models.DeleteRequest +import examples.customerExampleApi.models.DeleteRequestQueryResult import java.net.URI import kotlin.Int import kotlin.Pair import kotlin.String -import kotlin.collections.List interface CustomersService { fun read(customerNumber: String, xFlowId: String?): Customer @@ -19,7 +20,7 @@ interface DeleteRequestsService { xFlowId: String?, customerNumber: String?, cursor: String? - ): List + ): DeleteRequestQueryResult fun create(deleteRequest: DeleteRequest, xFlowId: String?): Pair @@ -38,7 +39,7 @@ interface DataAccessRequestsService { xFlowId: String?, customerNumber: String?, cursor: String? - ): List + ): DataAccessRequestQueryResult fun create(dataAccessRequest: DataAccessRequest, xFlowId: String?): Pair diff --git a/src/test/resources/examples/githubApi/open-api/api.yaml b/src/test/resources/examples/githubApi/api.yaml similarity index 100% rename from src/test/resources/examples/githubApi/open-api/api.yaml rename to src/test/resources/examples/githubApi/api.yaml diff --git a/src/test/resources/examples/javaSerializableModels/open-api/api.yaml b/src/test/resources/examples/javaSerializableModels/api.yaml similarity index 100% rename from src/test/resources/examples/javaSerializableModels/open-api/api.yaml rename to src/test/resources/examples/javaSerializableModels/api.yaml diff --git a/src/test/resources/examples/putApi/open-api/api.yaml b/src/test/resources/examples/putApi/api.yaml similarity index 100% rename from src/test/resources/examples/putApi/open-api/api.yaml rename to src/test/resources/examples/putApi/api.yaml diff --git a/src/test/resources/examples/simpleRequestBody/open-api/api.yaml b/src/test/resources/examples/simpleRequestBody/api.yaml similarity index 100% rename from src/test/resources/examples/simpleRequestBody/open-api/api.yaml rename to src/test/resources/examples/simpleRequestBody/api.yaml