Skip to content

Commit

Permalink
Handle Kotlin Flow as a wrapper type (#2107)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar authored Dec 11, 2024
1 parent 5c69aa0 commit 1fc8e44
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class KotlinConstants {
public static final DotName JETBRAINS_NOT_NULL = DotName
.createSimple("org.jetbrains.annotations.NotNull");

public static final DotName FLOW = DotName
.createSimple("kotlinx.coroutines.flow.Flow");

private KotlinConstants() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ public class TypeUtil {
jdkIndex = indexer.complete();

wrapperTypes.addAll(JaxbConstants.JAXB_ELEMENT);
wrapperTypes.add(KotlinConstants.FLOW);
wrapperTypes.add(MutinyConstants.UNI_TYPE.name());
wrapperTypes.add(JDKConstants.COMPLETION_STAGE_NAME);
wrapperTypes.add(JDKConstants.COMPLETABLE_FUTURE_NAME);
Expand Down
30 changes: 29 additions & 1 deletion testsuite/data/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<properties>
<eclipse.microprofile.openapi.version>4.0.2</eclipse.microprofile.openapi.version>
<kotlin.version>2.1.0</kotlin.version>
<kotlin.version>2.0.21</kotlin.version>
<java.version>17</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
Expand Down Expand Up @@ -99,6 +99,10 @@
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
Expand Down Expand Up @@ -190,7 +194,31 @@
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<compilerPlugins>
<plugin>all-open</plugin>
</compilerPlugins>

<pluginOptions>
<!-- Each annotation is placed on its own line -->
<option>all-open:annotation=jakarta.ws.rs.Path</option>
</pluginOptions>
</configuration>

<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.smallrye.openapi.testdata.kotlin

import jakarta.ws.rs.GET
import jakarta.ws.rs.Path
import jakarta.ws.rs.Produces
import jakarta.ws.rs.core.MediaType

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import org.jboss.resteasy.reactive.RestStreamElementType

@Path("kotlin")
class KotlinResource {
@Path("hello")
@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
@RestStreamElementType(MediaType.APPLICATION_JSON)
fun hello(): Flow<String> {
return flow {
Foobar("Hello")
}
}
}

data class Foobar(val data: String)
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ void testRecordWithPojoPrefixedRecordComponents() throws Exception {
assertJsonEquals("components.schemas.prefixed-record-component-names.json", result);
}

@Test
void testKotlinResourceWithUnwrappedFlowSSE() throws Exception {
Index index = Index.of(io.smallrye.openapi.testdata.kotlin.KotlinResource.class);
OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index);

OpenAPI result = scanner.scan();

printToConsole(result);
assertJsonEquals("components.schemas.kotlin-flow-unwrapped.json", result);
}

@Test
void testSyntheticClassesAndInterfacesIgnoredByDefault() throws Exception {
try (InputStream source = getClass().getResourceAsStream("/smallrye-open-api-testsuite-data.idx")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"openapi" : "3.1.0",
"paths" : {
"/kotlin/hello" : {
"get" : {
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"text/event-stream" : {
"schema" : {
"type" : "string"
}
}
}
}
}
}
}
}
}

0 comments on commit 1fc8e44

Please sign in to comment.