-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split into a separate processor with new annotation to handle finding…
… the correct classes.
- Loading branch information
Charles Bazeley
committed
Dec 10, 2024
1 parent
c45fba1
commit e6c5479
Showing
9 changed files
with
132 additions
and
87 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
...otation/src/main/kotlin/org/openfolder/kotlinasyncapi/annotation/AsyncApiDocumentation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package org.openfolder.kotlinasyncapi.annotation | ||
|
||
@Target(AnnotationTarget.CLASS) | ||
annotation class AsyncApiDocumentation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
.../openfolder/kotlinasyncapi/context/annotation/processor/AsyncApiDocumentationProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.openfolder.kotlinasyncapi.context.annotation.processor | ||
|
||
import org.openfolder.kotlinasyncapi.annotation.AsyncApiDocumentation | ||
import org.openfolder.kotlinasyncapi.annotation.channel.Channel | ||
import org.openfolder.kotlinasyncapi.annotation.channel.Publish | ||
import org.openfolder.kotlinasyncapi.annotation.channel.Subscribe | ||
import org.openfolder.kotlinasyncapi.model.component.Components | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KFunction | ||
import kotlin.reflect.full.findAnnotation | ||
import kotlin.reflect.full.functions | ||
import kotlin.reflect.full.hasAnnotation | ||
|
||
class AsyncApiDocumentationProcessor : AnnotationProcessor<AsyncApiDocumentation, KClass<*>> { | ||
override fun process(annotation: AsyncApiDocumentation, context: KClass<*>): Components { | ||
return Components().apply { | ||
channels { | ||
context.functions.filter { it.hasAnnotation<Channel>() }.forEach { currentFunction -> | ||
currentFunction.findAnnotation<Channel>()!!.toChannel() | ||
.apply { | ||
subscribe = subscribe ?: currentFunction.findAnnotation<Subscribe>()?.toOperation() | ||
publish = publish ?: currentFunction.findAnnotation<Publish>()?.toOperation() | ||
} | ||
.also { | ||
put(currentFunction.name, it) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...nfolder/kotlinasyncapi/context/annotation/processor/AsyncApiDocumentationProcessorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.openfolder.kotlinasyncapi.context.annotation.processor | ||
|
||
import org.junit.jupiter.api.Test | ||
import org.openfolder.kotlinasyncapi.annotation.AsyncApiDocumentation | ||
import org.openfolder.kotlinasyncapi.annotation.channel.Channel | ||
import org.openfolder.kotlinasyncapi.annotation.channel.Message | ||
import org.openfolder.kotlinasyncapi.annotation.channel.Parameter | ||
import org.openfolder.kotlinasyncapi.annotation.channel.SecurityRequirement | ||
import org.openfolder.kotlinasyncapi.annotation.channel.Subscribe | ||
import org.openfolder.kotlinasyncapi.context.TestUtils.assertJsonEquals | ||
import org.openfolder.kotlinasyncapi.context.TestUtils.json | ||
import kotlin.reflect.full.findAnnotation | ||
|
||
internal class AsyncApiDocumentationProcessorTest { | ||
|
||
private val processor = AsyncApiDocumentationProcessor() | ||
|
||
@Test | ||
fun `should process async api documentation annotation on class`() { | ||
val payload = TestChannelFunction::class | ||
val annotation = payload.findAnnotation<AsyncApiDocumentation>()!! | ||
|
||
val expected = json("annotation/async_api_documentation_component.json") | ||
val actual = json(processor.process(annotation, payload)) | ||
|
||
assertJsonEquals(expected, actual) | ||
} | ||
|
||
|
||
@AsyncApiDocumentation | ||
class TestChannelFunction { | ||
@Channel( | ||
value = "some/{parameter}/channel", | ||
description = "testDescription", | ||
servers = ["dev"], | ||
parameters = [ | ||
Parameter( | ||
value = "parameter", | ||
description = "testDescription" | ||
) | ||
] | ||
) | ||
@Subscribe( | ||
operationId = "testOperationId", | ||
security = [ | ||
SecurityRequirement( | ||
key = "petstore_auth", | ||
values = ["write:pets", "read:pets"] | ||
) | ||
], | ||
message = Message(TestSubscribeMessage::class) | ||
) | ||
fun testSubscribe() {} | ||
} | ||
|
||
@Message | ||
data class TestSubscribeMessage( | ||
val id: Int = 0, | ||
val name: String, | ||
val isTest: Boolean | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...nnotation/channel_component_function.json → ...on/async_api_documentation_component.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters