Skip to content

Commit

Permalink
fix: Fixed serialization issue and Open Api Generator bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoe Maas committed Jul 24, 2024
1 parent 3e8e7c4 commit c5cb2c0
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 57 deletions.
48 changes: 34 additions & 14 deletions modules/openapi/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile

plugins {
kotlin("multiplatform") version "2.0.0"
kotlin("plugin.serialization") version "2.0.0"
id("org.openapi.generator") version "7.7.0"
id("maven-publish")
}
Expand All @@ -23,11 +24,26 @@ repositories {

kotlin {
tasks {
// Temporary fix for this issue: https://github.com/OpenAPITools/openapi-generator/issues/17658
register<Copy>("fixOpenApiGeneratorIssue") {
from(
"$projectDir/build/generated/src/commonMain/kotlin/com/sphereon/oid/fed/openapi"
)
into(
"$projectDir/build/copy/src/commonMain/kotlin/com/sphereon/oid/fed/openapi"
)
filter { line: String ->
line.replace(
"kotlin.collections.Map<kotlin.String, kotlin.Any>",
"kotlinx.serialization.json.JsonObject")
}
}

withType<KotlinCompileCommon> {
dependsOn("openApiGenerate")
dependsOn("fixOpenApiGeneratorIssue")
}
named("sourcesJar") {
dependsOn("openApiGenerate")
dependsOn("fixOpenApiGeneratorIssue")
}
}
jvm {
Expand Down Expand Up @@ -58,19 +74,23 @@ kotlin {
}
}

named<KotlinJvmCompile>("compileKotlinJvm") {
named<Copy>("fixOpenApiGeneratorIssue") {
dependsOn("openApiGenerate")
}

named<KotlinJvmCompile>("compileKotlinJvm") {
dependsOn("fixOpenApiGeneratorIssue")
compilerOptions {
jvmTarget.set(JvmTarget.JVM_11)
}
}

named("jvmSourcesJar") {
dependsOn("openApiGenerate")
dependsOn("fixOpenApiGeneratorIssue")
}

named<Jar>("jvmJar") {
dependsOn("compileKotlinJvm")
dependsOn("fixOpenApiGeneratorIssue")
archiveBaseName.set("openapi")
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
from(configurations.kotlinCompilerClasspath.get().map { if (it.isDirectory) it else zipTree(it) })
Expand All @@ -82,10 +102,10 @@ kotlin {
js {
tasks {
named("compileKotlinJs") {
dependsOn("openApiGenerate")
dependsOn("fixOpenApiGeneratorIssue")
}
named("jsSourcesJar") {
dependsOn("openApiGenerate")
dependsOn("fixOpenApiGeneratorIssue")
}
}
nodejs()
Expand All @@ -94,37 +114,37 @@ kotlin {
iosX64 {
tasks {
named("compileKotlinIosX64") {
dependsOn("openApiGenerate")
dependsOn("fixOpenApiGeneratorIssue")
}
named("iosX64SourcesJar") {
dependsOn("openApiGenerate")
dependsOn("fixOpenApiGeneratorIssue")
}
}
}
iosArm64 {
tasks {
named("compileKotlinIosArm64") {
dependsOn("openApiGenerate")
dependsOn("fixOpenApiGeneratorIssue")
}
named("iosArm64SourcesJar") {
dependsOn("openApiGenerate")
dependsOn("fixOpenApiGeneratorIssue")
}
}
}
iosSimulatorArm64 {
tasks {
named("compileKotlinIosSimulatorArm64") {
dependsOn("openApiGenerate")
dependsOn("fixOpenApiGeneratorIssue")
}
named("iosSimulatorArm64SourcesJar") {
dependsOn("openApiGenerate")
dependsOn("fixOpenApiGeneratorIssue")
}
}
}

sourceSets {
val commonMain by getting {
kotlin.srcDir("build/generated/src/commonMain/kotlin")
kotlin.srcDir("build/copy/src/commonMain/kotlin")
dependencies {
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
Expand Down
2 changes: 1 addition & 1 deletion modules/openapi/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
kotlin.code.style=official
profiles=models-only
#profiles=models-only
82 changes: 43 additions & 39 deletions modules/openid-federation-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,21 @@ kotlin {
}
}

iosX64()
iosArm64()
iosSimulatorArm64()
// iosX64()
// iosArm64()
// iosSimulatorArm64()

jvm()

sourceSets {
val commonMain by getting {
dependencies {
implementation("com.sphereon.oid.fed:openapi:0.1.0-SNAPSHOT")
runtimeOnly("io.ktor:ktor-client-core:$ktorVersion")
runtimeOnly("io.ktor:ktor-client-logging:$ktorVersion")
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.ktor:ktor-client-logging:$ktorVersion")
implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
implementation("io.ktor:ktor-client-auth:$ktorVersion")
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.7.0")
implementation(libs.kermit.logging)
Expand All @@ -61,6 +64,7 @@ kotlin {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
implementation("io.ktor:ktor-client-mock:$ktorVersion")
}
}
val jvmMain by getting {
Expand All @@ -87,40 +91,40 @@ kotlin {
}
}

val iosMain by creating {
dependsOn(commonMain)
dependencies {

}
}
val iosX64Main by getting {
//dependsOn(iosMain)
dependencies {
implementation("io.ktor:ktor-client-core-iosx64:$ktorVersion")
implementation("io.ktor:ktor-client-cio-iosx64:$ktorVersion")
}
}
val iosArm64Main by getting {
dependsOn(iosX64Main)
dependencies {
implementation("io.ktor:ktor-client-core-iosarm64:$ktorVersion")
implementation("io.ktor:ktor-client-cio-iosarm64:$ktorVersion")
}
}
val iosSimulatorArm64Main by getting {
dependsOn(iosX64Main)
dependencies {
implementation("io.ktor:ktor-client-core-iossimulatorarm64:$ktorVersion")
implementation("io.ktor:ktor-client-cio-iossimulatorarm64:$ktorVersion")
}
}

val iosTest by creating {
dependsOn(commonTest)
dependencies {
implementation(kotlin("test"))
}
}
// val iosMain by creating {
// dependsOn(commonMain)
// dependencies {
// implementation("io.ktor:ktor-client-core-ios:$ktorVersion")
// }
// }
// val iosX64Main by getting {
// dependsOn(iosMain)
// dependencies {
// implementation("io.ktor:ktor-client-core-iosx64:$ktorVersion")
// implementation("io.ktor:ktor-client-cio-iosx64:$ktorVersion")
// }
// }
// val iosArm64Main by getting {
// dependsOn(iosMain)
// dependencies {
// implementation("io.ktor:ktor-client-core-iosarm64:$ktorVersion")
// implementation("io.ktor:ktor-client-cio-iosarm64:$ktorVersion")
// }
// }
// val iosSimulatorArm64Main by getting {
// dependsOn(iosMain)
// dependencies {
// implementation("io.ktor:ktor-client-core-iossimulatorarm64:$ktorVersion")
// implementation("io.ktor:ktor-client-cio-iossimulatorarm64:$ktorVersion")
// }
// }

// val iosTest by creating {
// dependsOn(commonTest)
// dependencies {
// implementation(kotlin("test"))
// }
// }

val jsMain by getting {
dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,51 @@
package nl.zoe.httpclient
package com.sphereon.oid.fed.common.httpclient

import com.sphereon.oid.fed.openapi.models.EntityStatement
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.*
import io.ktor.client.plugins.auth.*
import io.ktor.client.plugins.auth.providers.*
import io.ktor.client.plugins.cache.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.logging.*
import io.ktor.client.request.*
import io.ktor.client.request.forms.*
import io.ktor.http.*
import io.ktor.http.HttpMethod.Companion.Get
import io.ktor.http.HttpMethod.Companion.Post
import io.ktor.serialization.kotlinx.json.*
import io.ktor.utils.io.core.*

class OidFederationClient(
private val client: HttpClient
engine: HttpClientEngine,
isRequestAuthenticated: Boolean = false,
isRequestCached: Boolean = false
) {
private val client: HttpClient = HttpClient(engine) {
install(HttpCache)
install(ContentNegotiation) {
json()
}
install(Logging) {
logger = Logger.DEFAULT
level = LogLevel.INFO
}
if (isRequestAuthenticated) {
install(Auth) {
bearer {
loadTokens {
//TODO add correct implementation later
BearerTokens("accessToken", "refreshToken")
}
}
}
}
if (isRequestCached) {
install(HttpCache)
}
}

suspend fun fetchEntityStatement(url: String, httpMethod: HttpMethod = Get, parameters: Parameters = Parameters.Empty): EntityStatement {
return when (httpMethod) {
Get -> getEntityStatement(url)
Expand All @@ -31,4 +65,4 @@ class OidFederationClient(
}.body<EntityStatement>()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.sphereon.oid.fed.common.httpclient

import com.sphereon.oid.fed.openapi.models.EntityStatement
import com.sphereon.oid.fed.openapi.models.FederationEntityMetadata
import com.sphereon.oid.fed.openapi.models.Metadata
import io.ktor.client.engine.mock.*
import io.ktor.http.*
import io.ktor.utils.io.*
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlin.test.Test

class OidFederationClientTest {

private val entityStatement = EntityStatement(
iss = "test_iss",
sub = "test_sub",
metadata = Metadata(
federationEntity = FederationEntityMetadata(
federationListEndpoint = "http://www.example.com/list",
federationResolveEndpoint = "http://www.example.com/resolve",
organizationName = "test organization",
homepageUri = "http://www.example.com",
federationFetchEndpoint = "http://www.example.com/fetch",
)
)
)

private val mockEngine = MockEngine {
respond(
content = ByteReadChannel(Json.encodeToString(entityStatement)),
status = HttpStatusCode.OK,
headers = headersOf(HttpHeaders.ContentType, "application/json")
)
}

@Test
fun testGetEntityStatement() {
runBlocking {
val client = OidFederationClient(mockEngine)
val response = client.fetchEntityStatement("test_iss", HttpMethod.Get)
assert(response == entityStatement)
}
}
}

0 comments on commit c5cb2c0

Please sign in to comment.