Skip to content

Commit

Permalink
Merge pull request #16 from cjbooms/ISSUE-15
Browse files Browse the repository at this point in the history
Issue #15 Fixing return type on Service interfaces
  • Loading branch information
cjbooms authored Aug 26, 2020
2 parents 5351a09 + 2716527 commit 0432ba3
Show file tree
Hide file tree
Showing 12 changed files with 18 additions and 19 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
)
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/kotlin/com/cjbooms/fabrikt/util/OperationUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = Stream.of(
"putApi",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<java.net.URI, $modelsPackage.Contributor?>")
assertThat(get.returnType.toString()).isEqualTo("$modelsPackage.Contributor")
assertThat(update.returnType.toString()).isEqualTo("$modelsPackage.Contributor")
Expand All @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,7 +20,7 @@ interface DeleteRequestsService {
xFlowId: String?,
customerNumber: String?,
cursor: String?
): List<DeleteRequest>
): DeleteRequestQueryResult

fun create(deleteRequest: DeleteRequest, xFlowId: String?): Pair<URI, DeleteRequest?>

Expand All @@ -38,7 +39,7 @@ interface DataAccessRequestsService {
xFlowId: String?,
customerNumber: String?,
cursor: String?
): List<DataAccessRequest>
): DataAccessRequestQueryResult

fun create(dataAccessRequest: DataAccessRequest, xFlowId: String?): Pair<URI, DataAccessRequest?>

Expand Down
File renamed without changes.

0 comments on commit 0432ba3

Please sign in to comment.