Skip to content

Commit

Permalink
Bump to K2, start implementing union name generation
Browse files Browse the repository at this point in the history
  • Loading branch information
nomisRev committed May 28, 2024
1 parent eff3d65 commit eee222e
Show file tree
Hide file tree
Showing 22 changed files with 748 additions and 637 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
plugins {
kotlin("multiplatform") version "1.9.23" apply false
kotlin("multiplatform") version "2.0.0" apply false
}
3 changes: 2 additions & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ plugins {
kotlin {
explicitApi()

// TODO Setup targets

jvm()
macosArm64()
linuxX64()

sourceSets {
commonMain {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.nomisrev.openapi

import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KeepGeneratedSerializer
import io.github.nomisrev.openapi.Example.Companion.Serializer as ExampleSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive

// TODO support extensions ????
//@Serializable
//public data class Paths(
// val paths: Map<String, PathItem>,
// public val extensions: Map<String, JsonElement> = emptyMap()
//)

@Serializable
public data class PathItem(
/**
Expand Down
14 changes: 0 additions & 14 deletions core/src/commonMain/kotlin/io/github/nomisrev/openapi/Responses.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public data class Responses(
private val responseSerializer = ReferenceOr.serializer(Response.serializer())
private val responsesSerializer = MapSerializer(Int.serializer(), responseSerializer)

// TODO add support for extensions
override fun deserialize(decoder: Decoder): Responses {
val json = decoder.decodeSerializableValue(JsonElement.serializer()).jsonObject
val default = if (json.contains("default")) Json.decodeFromJsonElement(responseSerializer, json.getValue("default"))
Expand All @@ -68,26 +67,13 @@ public data class Responses(
return Responses(default, responses, extensions)
}

// TODO add support for extensions
override fun serialize(encoder: Encoder, value: Responses) {
val default = value.default?.let {
Json.encodeToJsonElement(ReferenceOr.serializer(Response.serializer()), it).jsonObject
}
val responses = Json.encodeToJsonElement(responsesSerializer, value.responses).jsonObject
val json = JsonObject((default ?: emptyMap()) + responses + value.extensions)
encoder.encodeSerializableValue(JsonElement.serializer(), json)
// val size = value.responses.size + (value.default?.let { 1 } ?: 0)
// val composite = encoder.beginCollection(descriptor, size)
// var index = 0
// value.default?.let {
// composite.encodeStringElement(descriptor, index++, "default")
// composite.encodeSerializableElement(descriptor, index++, elementSerializer, it)
// }
// value.responses.forEach { (statusCode, response) ->
// composite.encodeIntElement(descriptor, index++, statusCode)
// composite.encodeSerializableElement(descriptor, index++, elementSerializer, response)
// }
// composite.endStructure(descriptor)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,107 +1,107 @@
package io.github.nomisrev.openapi

import io.github.nomisrev.openapi.OpenAPI.Companion.Json
import java.io.BufferedReader
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals

class OpenAPISerializerEnd2EndTest {

fun resourceText(path: String): String =
requireNotNull(
OpenAPISerializerEnd2EndTest::class.java.classLoader
.getResourceAsStream(path)
?.bufferedReader()
?.use(BufferedReader::readText)
) { "Resource not found: $path" }

@Test
fun petstoreJson() {
Json.decodeFromString(
OpenAPI.serializer(),
resourceText("petstore.json"),
)
}

@Test
// TODO Has default response, without default key??
@Ignore
fun petstoreJsonIsomorphic() {
val openAPI = Json.decodeFromString(
OpenAPI.serializer(),
resourceText("petstore.json"),
)
val json = Json.encodeToString(OpenAPI.serializer(), openAPI)
assertEquals(
openAPI,
Json.decodeFromString(OpenAPI.serializer(), json)
)
}

@Test
fun petStoreMore() {
Json.decodeFromString(
OpenAPI.serializer(),
resourceText("petstore_more.json"),
)
}

@Test
fun issue1801() {
Json.decodeFromString(
OpenAPI.serializer(),
resourceText("issue-1801.json"),
)
}

@Test
@Ignore
// TODO check https://github.com/swagger-api/swagger-parser/issues/1821
fun issue1821() {
Json.decodeFromString(
OpenAPI.serializer(),
resourceText("issue-1821.json"),
)
}

@Test
fun issue1975() {
Json.decodeFromString(
OpenAPI.serializer(),
resourceText("issue-1975.json"),
)
}

@Test
fun oas31() {
Json.decodeFromString(
OpenAPI.serializer(),
resourceText("oas_3_1_0.json"),
)
}

@Test
fun basic() {
Json.decodeFromString(
OpenAPI.serializer(),
resourceText("basic.json"),
)
}

@Test
fun schemaSiblings() {
Json.decodeFromString(
OpenAPI.serializer(),
resourceText("schemaSiblings.json"),
)
}

@Test
fun securitySchemes() {
Json.decodeFromString(
OpenAPI.serializer(),
resourceText("securitySchemes_3_1_0.json"),
)
}
}
//package io.github.nomisrev.openapi
//
//import io.github.nomisrev.openapi.OpenAPI.Companion.Json
//import java.io.BufferedReader
//import kotlin.test.Ignore
//import kotlin.test.Test
//import kotlin.test.assertEquals
//
//class OpenAPISerializerEnd2EndTest {
//
// fun resourceText(path: String): String =
// requireNotNull(
// OpenAPISerializerEnd2EndTest::class.java.classLoader
// .getResourceAsStream(path)
// ?.bufferedReader()
// ?.use(BufferedReader::readText)
// ) { "Resource not found: $path" }
//
// @Test
// fun petstoreJson() {
// Json.decodeFromString(
// OpenAPI.serializer(),
// resourceText("petstore.json"),
// )
// }
//
// @Test
// // TODO Has default response, without default key??
// @Ignore
// fun petstoreJsonIsomorphic() {
// val openAPI = Json.decodeFromString(
// OpenAPI.serializer(),
// resourceText("petstore.json"),
// )
// val json = Json.encodeToString(OpenAPI.serializer(), openAPI)
// assertEquals(
// openAPI,
// Json.decodeFromString(OpenAPI.serializer(), json)
// )
// }
//
// @Test
// fun petStoreMore() {
// Json.decodeFromString(
// OpenAPI.serializer(),
// resourceText("petstore_more.json"),
// )
// }
//
// @Test
// fun issue1801() {
// Json.decodeFromString(
// OpenAPI.serializer(),
// resourceText("issue-1801.json"),
// )
// }
//
// @Test
// @Ignore
// // TODO check https://github.com/swagger-api/swagger-parser/issues/1821
// fun issue1821() {
// Json.decodeFromString(
// OpenAPI.serializer(),
// resourceText("issue-1821.json"),
// )
// }
//
// @Test
// fun issue1975() {
// Json.decodeFromString(
// OpenAPI.serializer(),
// resourceText("issue-1975.json"),
// )
// }
//
// @Test
// fun oas31() {
// Json.decodeFromString(
// OpenAPI.serializer(),
// resourceText("oas_3_1_0.json"),
// )
// }
//
// @Test
// fun basic() {
// Json.decodeFromString(
// OpenAPI.serializer(),
// resourceText("basic.json"),
// )
// }
//
// @Test
// fun schemaSiblings() {
// Json.decodeFromString(
// OpenAPI.serializer(),
// resourceText("schemaSiblings.json"),
// )
// }
//
// @Test
// fun securitySchemes() {
// Json.decodeFromString(
// OpenAPI.serializer(),
// resourceText("securitySchemes_3_1_0.json"),
// )
// }
//}
5 changes: 5 additions & 0 deletions core/src/jvmTest/resources/openai.json
Original file line number Diff line number Diff line change
Expand Up @@ -3727,7 +3727,11 @@
"ChatCompletionToolChoiceOption": {
"description": "Controls which (if any) function is called by the model.\n`none` means the model will not call a function and instead generates a message.\n`auto` means the model can pick between generating a message or calling a function.\nSpecifying a particular function via `{\"type\": \"function\", \"function\": {\"name\": \"my_function\"}}` forces the model to call that function.\n\n`none` is the default when no functions are present. `auto` is the default if functions are present.\n",
"oneOf": [
// "type": String
{
// CaseString
// CaseEnum
// CaseNoneOrAuto
"type": "string",
"description": "`none` means the model will not call a function and instead generates a message. `auto` means the model can pick between generating a message or calling a function.\n",
"enum": [
Expand All @@ -3736,6 +3740,7 @@
]
},
{
// CaseChatCompletionNamedToolChoice
"$ref": "#/components/schemas/ChatCompletionNamedToolChoice"
}
],
Expand Down
35 changes: 20 additions & 15 deletions generation/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi

plugins {
kotlin("multiplatform")
kotlin("plugin.serialization") version "1.9.23"
id("com.bnorm.power.kotlin-power-assert") version "0.13.0"
kotlin("plugin.serialization") version "2.0.0"
kotlin("plugin.power-assert") version "2.0.0"
}

configure<com.bnorm.power.PowerAssertGradleExtension> {
functions = listOf("kotlin.assert", "kotlin.test.assertTrue")
@Suppress("OPT_IN_USAGE")
powerAssert {
functions = listOf("kotlin.test.assertEquals", "kotlin.test.assertTrue")
}

kotlin {
// explicitApi()
// TODO re-enable platforms after finishing core / generation
// Not worth dealing with all extra platforms during initial phase
jvm()
// macosArm64 {
// binaries {
// executable { entryPoint = "main" }
// }
// }
jvm {
@OptIn(ExperimentalKotlinGradlePluginApi::class)
mainRun { mainClass.set("io.github.nomisrev.openapi.MainKt") }
}
macosArm64 {
binaries {
executable { entryPoint = "main" }
}
}
linuxX64()

sourceSets {
commonMain {
// kotlin.srcDir(project.file("build/generated/openapi/src/commonMain/kotlin"))
kotlin.srcDir(project.file("build/generated/openapi/src/commonMain/kotlin"))

dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-io-core:0.3.5")
implementation("net.pearx.kasechange:kasechange:1.4.1")
implementation(project(":core"))
implementation("com.squareup.okio:okio:3.9.0")
// for build debugging example
implementation("io.exoquery:pprint-kotlin-kmp:2.0.2")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
Expand Down Expand Up @@ -58,4 +63,4 @@ task("runMacosArm64") {
dependsOn("linkDebugExecutableMacosArm64")
dependsOn("runDebugExecutableMacosArm64")
group = "run"
}
}
Loading

0 comments on commit eee222e

Please sign in to comment.