Skip to content

Commit 9a9c0d8

Browse files
committed
create a application call extension in order to make it more easier to return the correct content type
1 parent a86aa1f commit 9a9c0d8

File tree

3 files changed

+62
-47
lines changed

3 files changed

+62
-47
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ install(StatusPages) {
3838
cause.message,
3939
"https://example.org"
4040
)
41-
call.respondText(
42-
problem.toJson(),
43-
ContentType.Application.ProblemJson,
44-
httpStatus,
45-
)
41+
call.respondProblem(httpStatus, problem)
4642
}
4743
}
4844
```

build.gradle.kts

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,73 @@ val ktorVersion: String by project
22
val kotestVersion: String by project
33

44
plugins {
5-
kotlin("jvm") version "2.0.0"
6-
id("org.jetbrains.kotlin.plugin.serialization") version "2.0.0"
7-
id("maven-publish")
8-
id("jacoco")
5+
kotlin("jvm") version "2.0.0"
6+
id("org.jetbrains.kotlin.plugin.serialization") version "2.0.0"
7+
id("maven-publish")
8+
id("jacoco")
99
}
1010

1111
group = "schwarz.it"
1212
version = "1.0.0"
1313

1414
repositories {
15-
mavenCentral()
15+
mavenCentral()
1616
}
1717

1818
dependencies {
19-
implementation(kotlin("reflect"))
20-
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
19+
implementation(kotlin("reflect"))
20+
implementation("io.ktor:ktor-server-core:$ktorVersion")
21+
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
2122

22-
testImplementation(kotlin("test"))
23-
testImplementation("io.kotest:kotest-runner-junit5:$kotestVersion")
24-
testImplementation("io.kotest:kotest-assertions-core:$kotestVersion")
23+
testImplementation(kotlin("test"))
24+
testImplementation("io.kotest:kotest-runner-junit5:$kotestVersion")
25+
testImplementation("io.kotest:kotest-assertions-core:$kotestVersion")
2526
}
2627

2728
tasks.test {
28-
useJUnitPlatform()
29+
useJUnitPlatform()
2930
}
3031
kotlin {
31-
jvmToolchain(21)
32+
jvmToolchain(21)
3233
}
3334

3435
publishing {
35-
publications {
36-
create<MavenPublication>("mavenPublication") {
37-
from(components["java"])
38-
}
39-
}
40-
repositories {
41-
maven {
42-
name = "artifactory"
43-
credentials {
44-
username = project.findProperty("artifactoryUsername") as String? ?: System.getenv("ARTIFACTORY_USER")
45-
password = project.findProperty("artifactoryPassword") as String? ?: System.getenv("ARTIFACTORY_PASSWORD")
46-
}
47-
url = uri("https://schwarzit.jfrog.io/artifactory/xx-sit-odj-psftp-maven-release-local/")
48-
}
49-
}
36+
publications {
37+
create<MavenPublication>("mavenPublication") {
38+
from(components["java"])
39+
}
40+
}
41+
repositories {
42+
maven {
43+
name = "artifactory"
44+
credentials {
45+
username = project.findProperty("artifactoryUsername") as String? ?: System.getenv("ARTIFACTORY_USER")
46+
password =
47+
project.findProperty("artifactoryPassword") as String? ?: System.getenv("ARTIFACTORY_PASSWORD")
48+
}
49+
url = uri("https://schwarzit.jfrog.io/artifactory/xx-sit-odj-psftp-maven-release-local/")
50+
}
51+
}
5052
}
5153

5254
jacoco {
53-
reportsDirectory.set(layout.buildDirectory.dir("reports/jacoco/"))
55+
reportsDirectory.set(layout.buildDirectory.dir("reports/jacoco/"))
5456
}
5557

5658
with(tasks) {
57-
test {
58-
useJUnitPlatform()
59-
finalizedBy(jacocoTestReport)
60-
}
59+
test {
60+
useJUnitPlatform()
61+
finalizedBy(jacocoTestReport)
62+
}
6163

62-
jacocoTestReport {
63-
dependsOn(test)
64-
reports {
65-
xml.required.set(true)
66-
csv.required.set(false)
67-
html.required.set(true)
68-
xml.outputLocation.set(layout.buildDirectory.file("reports/jacoco/report.xml"))
69-
html.outputLocation.set(layout.buildDirectory.dir("reports/jacoco/html"))
70-
}
71-
}
64+
jacocoTestReport {
65+
dependsOn(test)
66+
reports {
67+
xml.required.set(true)
68+
csv.required.set(false)
69+
html.required.set(true)
70+
xml.outputLocation.set(layout.buildDirectory.file("reports/jacoco/report.xml"))
71+
html.outputLocation.set(layout.buildDirectory.dir("reports/jacoco/html"))
72+
}
73+
}
7274
}

src/main/kotlin/schwarz/it/problem/details/Problem.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package schwarz.it.problem.details
22

3+
import io.ktor.http.ContentType
34
import io.ktor.http.HttpStatusCode
5+
import io.ktor.server.application.ApplicationCall
6+
import io.ktor.server.response.respondText
47
import kotlinx.serialization.json.Json
58

69
/**
@@ -13,6 +16,20 @@ private const val TYPE_DEFAULT = "about:blank"
1316
*/
1417
public fun problem(block: ProblemBuilder.() -> Unit): Problem = ProblemBuilder().apply(block).build()
1518

19+
/**
20+
* Application Call extension in order to return problem details with correct content type in ktor
21+
*/
22+
suspend fun ApplicationCall.respondProblem(
23+
status: HttpStatusCode,
24+
problem: Problem,
25+
) {
26+
respondText(
27+
problem.toJson(),
28+
ContentType.Application.ProblemJson,
29+
status,
30+
)
31+
}
32+
1633
/**
1734
* Builder for a problem detail including all mutable components
1835
* Problem implements RFC 9457 (https://datatracker.ietf.org/doc/rfc9457/)

0 commit comments

Comments
 (0)