Skip to content

Commit 5031049

Browse files
committed
fix: fail fast when mandatory AsyncApi fields are missing
1 parent 8484b81 commit 5031049

File tree

2 files changed

+59
-9
lines changed
  • kotlin-asyncapi-core/src

2 files changed

+59
-9
lines changed

kotlin-asyncapi-core/src/main/kotlin/com/asyncapi/kotlinasyncapi/model/AsyncApi.kt

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,53 @@ class AsyncApi {
2424
fun id(value: String): String =
2525
value.also { id = it }
2626

27-
inline fun info(build: Info.() -> Unit): Info =
27+
fun info(build: Info.() -> Unit): Info =
2828
Info().apply(build).also { info = it }
2929

30-
inline fun servers(build: ReferencableServersMap.() -> Unit): ReferencableServersMap =
30+
fun servers(build: ReferencableServersMap.() -> Unit): ReferencableServersMap =
3131
ReferencableServersMap().apply(build).also { servers = it }
3232

33-
inline fun channels(build: ReferencableChannelsMap.() -> Unit): ReferencableChannelsMap =
33+
fun channels(build: ReferencableChannelsMap.() -> Unit): ReferencableChannelsMap =
3434
ReferencableChannelsMap().apply(build).also { channels = it }
3535

3636
fun defaultContentType(value: String): String =
3737
value.also { defaultContentType = it }
3838

39-
inline fun components(build: Components.() -> Unit): Components =
39+
fun components(build: Components.() -> Unit): Components =
4040
Components().apply(build).also { components = it }
4141

42-
inline fun tags(build: TagsList.() -> Unit): TagsList =
42+
fun tags(build: TagsList.() -> Unit): TagsList =
4343
TagsList().apply(build).also { tags = it }
4444

45-
inline fun externalDocs(build: ExternalDocumentation.() -> Unit): ExternalDocumentation =
45+
fun externalDocs(build: ExternalDocumentation.() -> Unit): ExternalDocumentation =
4646
ExternalDocumentation().apply(build).also { externalDocs = it }
4747

4848
companion object {
4949
const val VERSION = "2.4.0"
5050

51-
inline fun asyncApi(build: AsyncApi.() -> Unit): AsyncApi =
52-
AsyncApi().apply(build)
51+
fun asyncApi(build: AsyncApi.() -> Unit): AsyncApi =
52+
AsyncApi()
53+
.apply(build)
54+
.checkInitialized(
55+
"info" to { info },
56+
"channels" to { channels }
57+
)
58+
}
59+
}
60+
61+
fun <T> T.checkInitialized(
62+
vararg checks: Pair<String, T.() -> Unit>
63+
): T = also {
64+
val missing = checks.mapNotNull { (name, check) ->
65+
try {
66+
it.check()
67+
null
68+
} catch (_: UninitializedPropertyAccessException) {
69+
name
70+
}
71+
}
72+
73+
check(missing.isEmpty()) {
74+
"Missing required properties: ${missing.joinToString()}"
5375
}
5476
}

kotlin-asyncapi-core/src/test/kotlin/com/asyncapi/kotlinasyncapi/model/AsyncApiTest.kt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.asyncapi.kotlinasyncapi.model
22

3+
import com.asyncapi.kotlinasyncapi.model.info.Info
34
import io.mockk.clearConstructorMockk
45
import io.mockk.every
56
import io.mockk.mockkConstructor
67
import org.junit.jupiter.api.AfterEach
78
import org.junit.jupiter.api.Test
8-
import com.asyncapi.kotlinasyncapi.model.info.Info
9+
import org.junit.jupiter.api.Assertions.assertTrue
10+
import org.junit.jupiter.api.Assertions.assertNotNull
11+
import org.junit.jupiter.api.Assertions.assertThrows
912

1013
internal class AsyncApiTest {
1114

@@ -43,4 +46,29 @@ internal class AsyncApiTest {
4346

4447
TestUtils.assertJsonEquals(expected, actual)
4548
}
49+
50+
@Test
51+
fun `should fail fast when mandatory fields are missing`() {
52+
val exception = assertThrows(IllegalStateException::class.java) {
53+
AsyncApi.asyncApi {
54+
}
55+
}
56+
57+
assertTrue(exception.message!!.contains("Missing required properties"))
58+
assertTrue(exception.message!!.contains("info"))
59+
assertTrue(exception.message!!.contains("channels"))
60+
}
61+
62+
@Test
63+
fun `should succeed when mandatory fields are initialized`() {
64+
val api = AsyncApi.asyncApi {
65+
info {
66+
title = "Test API"
67+
version = "1.0.0"
68+
}
69+
channels { }
70+
}
71+
72+
assertNotNull(api)
73+
}
4674
}

0 commit comments

Comments
 (0)