Skip to content

Commit

Permalink
Merge pull request #80 from outfoxx/fix/swift-types
Browse files Browse the repository at this point in the history
Fix issues with Swift type generation
  • Loading branch information
kdubb committed Jun 26, 2023
2 parents 7da2295 + dec0bcb commit 5c9e5b2
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,8 @@ class SwiftSundayGenerator(
}
} else {

val returnRefType = functionBuilder.build().returnType?.let { typeRegistry.getReferenceType(it) }

val requestOnly = operation.findBoolAnnotation(RequestOnly, null) == true
val responseOnly = operation.findBoolAnnotation(ResponseOnly, null) == true

Expand All @@ -562,11 +564,19 @@ class SwiftSundayGenerator(
else -> "result"
}

builder.add("return try await self.requestFactory.%L(%>\n", factoryMethod)
if (returnRefType != null) {
builder.add("return try await (self.requestFactory.%L(%>\n", factoryMethod)
} else {
builder.add("return try await self.requestFactory.%L(%>\n", factoryMethod)
}

builder.add(reqGen())

builder.add("%<\n)\n")
if (returnRefType != null) {
builder.add("%<\n) as %T).value\n", returnRefType)
} else {
builder.add("%<\n)\n")
}

if (!requestOnly && !responseOnly) {
addNullifyMethod(operation, functionBuilder.build(), problemTypes, typeBuilder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -848,20 +848,22 @@ class SwiftTypeRegistry(
}

if (isRoot || localDeclaredProperties.isNotEmpty() || discriminatorProperty != null) {

val codingKeysBuilder =
TypeSpec.enumBuilder(codingKeysTypeName)
.addModifiers(FILEPRIVATE)
.apply {
if (localDeclaredProperties.isNotEmpty() || discriminatorProperty != null) {
addSuperType(STRING)
}
}
.addSuperType(CODING_KEY)

if (isRoot && discriminatorProperty != null) {
codingKeysBuilder.addSuperType(STRING)
codingKeysBuilder.addEnumCase(discriminatorProperty.swiftIdentifierName, discriminatorProperty.name!!)
}

if (localDeclaredProperties.any { it.name != discriminatorPropertyName }) {
codingKeysBuilder.addSuperType(STRING)
}

codingKeysBuilder.addSuperType(CODING_KEY)

localDeclaredProperties
.filter { it.name != discriminatorProperty?.name }
.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ class RequestCoroutineMethodsTest {
@GET
@Path(value = "/tests")
public suspend fun fetchTest(): Response
@GET
@Path(value = "/tests/derived")
public suspend fun fetchDerivedTest(): Response
}
""".trimIndent(),
Expand Down Expand Up @@ -122,6 +126,7 @@ class RequestCoroutineMethodsTest {
"""
package io.test.service
import io.test.Base
import io.test.Test
import javax.ws.rs.Consumes
import javax.ws.rs.GET
Expand All @@ -134,6 +139,10 @@ class RequestCoroutineMethodsTest {
@GET
@Path(value = "/tests")
public suspend fun fetchTest(): Test
@GET
@Path(value = "/tests/derived")
public suspend fun fetchDerivedTest(): Base
}
""".trimIndent(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ class RequestReactiveMethodsTest {
@GET
@Path(value = "/tests")
public fun fetchTest(): CompletionStage<Response>
@GET
@Path(value = "/tests/derived")
public fun fetchDerivedTest(): CompletionStage<Response>
}
""".trimIndent(),
Expand Down Expand Up @@ -129,6 +133,7 @@ class RequestReactiveMethodsTest {
"""
package io.test.service
import io.test.Base
import io.test.Test
import java.util.concurrent.CompletionStage
import javax.ws.rs.Consumes
Expand All @@ -142,6 +147,10 @@ class RequestReactiveMethodsTest {
@GET
@Path(value = "/tests")
public fun fetchTest(): CompletionStage<Test>
@GET
@Path(value = "/tests/derived")
public fun fetchDerivedTest(): CompletionStage<Base>
}
""".trimIndent(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class ResponseBodyContentTest {
@GET
@Path(value = "/tests")
public fun fetchTest(): Response
@GET
@Path(value = "/tests/derived")
public fun fetchDerivedTest(): Response
}
""".trimIndent(),
Expand Down Expand Up @@ -148,6 +152,7 @@ class ResponseBodyContentTest {
"""
package io.test.service
import io.test.Base
import io.test.Test
import javax.ws.rs.Consumes
import javax.ws.rs.GET
Expand All @@ -160,6 +165,10 @@ class ResponseBodyContentTest {
@GET
@Path(value = "/tests")
public fun fetchTest(): Test
@GET
@Path(value = "/tests/derived")
public fun fetchDerivedTest(): Base
}
""".trimIndent(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class ResponseBodyContentTest {
import io.outfoxx.sunday.MediaType
import io.outfoxx.sunday.RequestFactory
import io.outfoxx.sunday.http.Method
import io.test.Base
import io.test.Test
import kotlin.collections.List
Expand All @@ -74,6 +75,13 @@ class ResponseBodyContentTest {
pathTemplate = "/tests",
acceptTypes = this.defaultAcceptTypes
)
public suspend fun fetchDerivedTest(): Base = this.requestFactory
.result(
method = Method.Get,
pathTemplate = "/tests/derived",
acceptTypes = this.defaultAcceptTypes
)
}
""".trimIndent(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ class ResponseBodyContentTest {
)
}
public func fetchDerivedTest() async throws -> Base {
return try await (self.requestFactory.result(
method: .get,
pathTemplate: "/tests/derived",
pathParameters: nil,
queryParameters: nil,
body: Empty.none,
contentTypes: nil,
acceptTypes: self.defaultAcceptTypes,
headers: nil
) as Base.AnyRef).value
}
}
""".trimIndent(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class ResponseBodyContentTest {

assertEquals(
"""
import {Base} from './base';
import {Test} from './test';
import {AnyType, MediaType, RequestFactory} from '@outfoxx/sunday';
import {Observable} from 'rxjs';
Expand Down Expand Up @@ -87,9 +88,21 @@ class ResponseBodyContentTest {
);
}
fetchDerivedTest(): Observable<Base> {
return this.requestFactory.result(
{
method: 'GET',
pathTemplate: '/tests/derived',
acceptTypes: this.defaultAcceptTypes
},
fetchDerivedTestReturnType
);
}
}
const fetchTestReturnType: AnyType = [Test];
const fetchDerivedTestReturnType: AnyType = [Base];
""".trimIndent(),
buildString {
Expand Down
18 changes: 18 additions & 0 deletions generator/src/test/resources/raml/resource-gen/res-body-param.raml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,28 @@ types:
properties:
value: string

Base:
type: object
discriminator: type
properties:
type: string

Derived:
type: Base
discriminatorValue: derived
properties:
value: string

/tests:
get:
displayName: fetchTest
responses:
200:
body: Test

/tests/derived:
get:
displayName: fetchDerivedTest
responses:
200:
body: Base

0 comments on commit 5c9e5b2

Please sign in to comment.