Skip to content

Commit e7d5668

Browse files
Bump org.jmailen.kotlinter from 3.12.0 to 4.3.0 (#333)
* Bump org.jmailen.kotlinter from 3.12.0 to 4.3.0 Bumps org.jmailen.kotlinter from 3.12.0 to 4.3.0. --- updated-dependencies: - dependency-name: org.jmailen.kotlinter dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <[email protected]> * Format Kotlin * Format Kotlin * Fix linting * Fix linting * Fix linting * Fix linting * Fix linting * Fix linting * Fix linting --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Yannick Block <[email protected]>
1 parent 7126439 commit e7d5668

35 files changed

+1274
-1021
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ plugins {
1414
`maven-publish`
1515
jacoco
1616
id("com.github.kt3k.coveralls") version "2.12.2"
17-
id("org.jmailen.kotlinter") version "3.12.0"
17+
id("org.jmailen.kotlinter") version "4.3.0"
1818
}
1919

2020
group = "com.github.moia-dev"

router-openapi-request-validator/src/main/kotlin/io/moia/router/openapi/OpenApiValidator.kt

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,25 @@ import org.slf4j.LoggerFactory
1313
class OpenApiValidator(val specUrlOrPayload: String) {
1414
val validator = OpenApiInteractionValidator.createFor(specUrlOrPayload).build()
1515

16-
fun validate(request: APIGatewayProxyRequestEvent, response: APIGatewayProxyResponseEvent): ValidationReport {
16+
fun validate(
17+
request: APIGatewayProxyRequestEvent,
18+
response: APIGatewayProxyResponseEvent,
19+
): ValidationReport {
1720
return validator.validate(request.toRequest(), response.toResponse())
1821
.also { if (it.hasErrors()) log.error("error validating request and response against $specUrlOrPayload - $it") }
1922
}
2023

21-
fun assertValid(request: APIGatewayProxyRequestEvent, response: APIGatewayProxyResponseEvent) {
24+
fun assertValid(
25+
request: APIGatewayProxyRequestEvent,
26+
response: APIGatewayProxyResponseEvent,
27+
) {
2228
return validate(request, response).let {
2329
if (it.hasErrors()) {
2430
throw ApiInteractionInvalid(
2531
specUrlOrPayload,
2632
request,
2733
response,
28-
it
34+
it,
2935
)
3036
}
3137
}
@@ -37,38 +43,45 @@ class OpenApiValidator(val specUrlOrPayload: String) {
3743
throw ApiInteractionInvalid(
3844
spec = specUrlOrPayload,
3945
request = request,
40-
validationReport = it
46+
validationReport = it,
4147
)
4248
}
4349
}
4450

45-
fun assertValidResponse(request: APIGatewayProxyRequestEvent, response: APIGatewayProxyResponseEvent) =
46-
request.toRequest().let { r ->
47-
validator.validateResponse(r.path, r.method, response.toResponse()).let {
48-
if (it.hasErrors()) {
49-
throw ApiInteractionInvalid(
50-
spec = specUrlOrPayload,
51-
request = request,
52-
validationReport = it
53-
)
54-
}
51+
fun assertValidResponse(
52+
request: APIGatewayProxyRequestEvent,
53+
response: APIGatewayProxyResponseEvent,
54+
) = request.toRequest().let { r ->
55+
validator.validateResponse(r.path, r.method, response.toResponse()).let {
56+
if (it.hasErrors()) {
57+
throw ApiInteractionInvalid(
58+
spec = specUrlOrPayload,
59+
request = request,
60+
validationReport = it,
61+
)
5562
}
5663
}
64+
}
5765

58-
class ApiInteractionInvalid(val spec: String, val request: APIGatewayProxyRequestEvent, val response: APIGatewayProxyResponseEvent? = null, val validationReport: ValidationReport) :
59-
RuntimeException("Error validating request and response against $spec - $validationReport")
66+
class ApiInteractionInvalid(
67+
val spec: String,
68+
val request: APIGatewayProxyRequestEvent,
69+
val response: APIGatewayProxyResponseEvent? = null,
70+
val validationReport: ValidationReport,
71+
) : RuntimeException("Error validating request and response against $spec - $validationReport")
6072

6173
private fun APIGatewayProxyRequestEvent.toRequest(): Request {
62-
val builder = when (httpMethod.toLowerCase()) {
63-
"get" -> SimpleRequest.Builder.get(path)
64-
"post" -> SimpleRequest.Builder.post(path)
65-
"put" -> SimpleRequest.Builder.put(path)
66-
"patch" -> SimpleRequest.Builder.patch(path)
67-
"delete" -> SimpleRequest.Builder.delete(path)
68-
"options" -> SimpleRequest.Builder.options(path)
69-
"head" -> SimpleRequest.Builder.head(path)
70-
else -> throw IllegalArgumentException("Unsupported method $httpMethod")
71-
}
74+
val builder =
75+
when (httpMethod.toLowerCase()) {
76+
"get" -> SimpleRequest.Builder.get(path)
77+
"post" -> SimpleRequest.Builder.post(path)
78+
"put" -> SimpleRequest.Builder.put(path)
79+
"patch" -> SimpleRequest.Builder.patch(path)
80+
"delete" -> SimpleRequest.Builder.delete(path)
81+
"options" -> SimpleRequest.Builder.options(path)
82+
"head" -> SimpleRequest.Builder.head(path)
83+
else -> throw IllegalArgumentException("Unsupported method $httpMethod")
84+
}
7285
headers?.forEach { builder.withHeader(it.key, it.value) }
7386
queryStringParameters?.forEach { builder.withQueryParam(it.key, it.value) }
7487
builder.withBody(body)

router-openapi-request-validator/src/main/kotlin/io/moia/router/openapi/ValidatingRequestRouterWrapper.kt

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,33 @@ class ValidatingRequestRouterWrapper(
2020
val delegate: RequestHandler,
2121
specUrlOrPayload: String,
2222
private val additionalRequestValidationFunctions: List<(APIGatewayProxyRequestEvent) -> Unit> = emptyList(),
23-
private val additionalResponseValidationFunctions: List<(APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent) -> Unit> = emptyList()
23+
private val additionalResponseValidationFunctions: List<
24+
(
25+
APIGatewayProxyRequestEvent,
26+
APIGatewayProxyResponseEvent,
27+
) -> Unit,
28+
> = emptyList(),
2429
) {
2530
private val openApiValidator = OpenApiValidator(specUrlOrPayload)
2631

27-
fun handleRequest(input: APIGatewayProxyRequestEvent, context: Context): APIGatewayProxyResponseEvent =
32+
fun handleRequest(
33+
input: APIGatewayProxyRequestEvent,
34+
context: Context,
35+
): APIGatewayProxyResponseEvent =
2836
handleRequest(input = input, context = context, skipRequestValidation = false, skipResponseValidation = false)
2937

30-
fun handleRequestSkippingRequestAndResponseValidation(input: APIGatewayProxyRequestEvent, context: Context): APIGatewayProxyResponseEvent =
38+
fun handleRequestSkippingRequestAndResponseValidation(
39+
input: APIGatewayProxyRequestEvent,
40+
context: Context,
41+
): APIGatewayProxyResponseEvent =
3142
handleRequest(input = input, context = context, skipRequestValidation = true, skipResponseValidation = true)
3243

33-
private fun handleRequest(input: APIGatewayProxyRequestEvent, context: Context, skipRequestValidation: Boolean, skipResponseValidation: Boolean): APIGatewayProxyResponseEvent {
44+
private fun handleRequest(
45+
input: APIGatewayProxyRequestEvent,
46+
context: Context,
47+
skipRequestValidation: Boolean,
48+
skipResponseValidation: Boolean,
49+
): APIGatewayProxyResponseEvent {
3450
if (!skipRequestValidation) {
3551
try {
3652
openApiValidator.assertValidRequest(input)
@@ -58,7 +74,10 @@ class ValidatingRequestRouterWrapper(
5874
additionalRequestValidationFunctions.forEach { it(requestEvent) }
5975
}
6076

61-
private fun runAdditionalResponseValidations(requestEvent: APIGatewayProxyRequestEvent, responseEvent: APIGatewayProxyResponseEvent) {
77+
private fun runAdditionalResponseValidations(
78+
requestEvent: APIGatewayProxyRequestEvent,
79+
responseEvent: APIGatewayProxyResponseEvent,
80+
) {
6281
additionalResponseValidationFunctions.forEach { it(requestEvent, responseEvent) }
6382
}
6483

router-openapi-request-validator/src/test/kotlin/io/moia/router/openapi/OpenApiValidatorTest.kt

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import org.assertj.core.api.BDDAssertions.thenThrownBy
1010
import org.junit.jupiter.api.Test
1111

1212
class OpenApiValidatorTest {
13-
1413
val testHandler = TestRequestHandler()
1514

1615
val validator = OpenApiValidator("openapi.yml")
1716

1817
@Test
1918
fun `should handle and validate request`() {
20-
val request = GET("/tests")
21-
.withHeaders(mapOf("Accept" to "application/json"))
19+
val request =
20+
GET("/tests")
21+
.withHeaders(mapOf("Accept" to "application/json"))
2222

2323
val response = testHandler.handleRequest(request, mockk())
2424

@@ -29,8 +29,9 @@ class OpenApiValidatorTest {
2929

3030
@Test
3131
fun `should fail on undocumented request`() {
32-
val request = GET("/tests-not-documented")
33-
.withHeaders(mapOf("Accept" to "application/json"))
32+
val request =
33+
GET("/tests-not-documented")
34+
.withHeaders(mapOf("Accept" to "application/json"))
3435

3536
val response = testHandler.handleRequest(request, mockk())
3637

@@ -40,37 +41,39 @@ class OpenApiValidatorTest {
4041

4142
@Test
4243
fun `should fail on invalid schema`() {
43-
val request = GET("/tests")
44-
.withHeaders(mapOf("Accept" to "application/json"))
44+
val request =
45+
GET("/tests")
46+
.withHeaders(mapOf("Accept" to "application/json"))
4547

46-
val response = TestInvalidRequestHandler()
47-
.handleRequest(request, mockk())
48+
val response =
49+
TestInvalidRequestHandler()
50+
.handleRequest(request, mockk())
4851

4952
thenThrownBy { validator.assertValid(request, response) }.isInstanceOf(OpenApiValidator.ApiInteractionInvalid::class.java)
5053
}
5154

5255
class TestRequestHandler : RequestHandler() {
53-
5456
data class TestResponse(val name: String)
5557

56-
override val router = Router.router {
57-
GET("/tests") { _: Request<Unit> ->
58-
ResponseEntity.ok(TestResponse("Hello"))
58+
override val router =
59+
Router.router {
60+
GET("/tests") { _: Request<Unit> ->
61+
ResponseEntity.ok(TestResponse("Hello"))
62+
}
63+
GET("/tests-not-documented") { _: Request<Unit> ->
64+
ResponseEntity.ok(TestResponse("Hello"))
65+
}
5966
}
60-
GET("/tests-not-documented") { _: Request<Unit> ->
61-
ResponseEntity.ok(TestResponse("Hello"))
62-
}
63-
}
6467
}
6568

6669
class TestInvalidRequestHandler : RequestHandler() {
67-
6870
data class TestResponseInvalid(val invalid: String)
6971

70-
override val router = Router.router {
71-
GET("/tests") { _: Request<Unit> ->
72-
ResponseEntity.ok(TestResponseInvalid("Hello"))
72+
override val router =
73+
Router.router {
74+
GET("/tests") { _: Request<Unit> ->
75+
ResponseEntity.ok(TestResponseInvalid("Hello"))
76+
}
7377
}
74-
}
7578
}
7679
}

router-openapi-request-validator/src/test/kotlin/io/moia/router/openapi/ValidatingRequestRouterWrapperTest.kt

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import org.assertj.core.api.BDDAssertions.thenThrownBy
1212
import org.junit.jupiter.api.Test
1313

1414
class ValidatingRequestRouterWrapperTest {
15-
1615
@Test
1716
fun `should return response on successful validation`() {
18-
val response = ValidatingRequestRouterWrapper(TestRequestHandler(), "openapi.yml")
19-
.handleRequest(GET("/tests").withAcceptHeader("application/json"), mockk())
17+
val response =
18+
ValidatingRequestRouterWrapper(TestRequestHandler(), "openapi.yml")
19+
.handleRequest(GET("/tests").withAcceptHeader("application/json"), mockk())
2020

2121
then(response.statusCode).isEqualTo(200)
2222
}
@@ -43,8 +43,12 @@ class ValidatingRequestRouterWrapperTest {
4343

4444
@Test
4545
fun `should skip validation`() {
46-
val response = ValidatingRequestRouterWrapper(InvalidTestRequestHandler(), "openapi.yml")
47-
.handleRequestSkippingRequestAndResponseValidation(GET("/path-not-documented").withAcceptHeader("application/json"), mockk())
46+
val response =
47+
ValidatingRequestRouterWrapper(InvalidTestRequestHandler(), "openapi.yml")
48+
.handleRequestSkippingRequestAndResponseValidation(
49+
GET("/path-not-documented").withAcceptHeader("application/json"),
50+
mockk(),
51+
)
4852
then(response.statusCode).isEqualTo(404)
4953
}
5054

@@ -54,7 +58,7 @@ class ValidatingRequestRouterWrapperTest {
5458
ValidatingRequestRouterWrapper(
5559
delegate = OpenApiValidatorTest.TestRequestHandler(),
5660
specUrlOrPayload = "openapi.yml",
57-
additionalRequestValidationFunctions = listOf({ _ -> throw RequestValidationFailedException() })
61+
additionalRequestValidationFunctions = listOf({ _ -> throw RequestValidationFailedException() }),
5862
)
5963
.handleRequest(GET("/tests").withAcceptHeader("application/json"), mockk())
6064
}
@@ -67,29 +71,32 @@ class ValidatingRequestRouterWrapperTest {
6771
ValidatingRequestRouterWrapper(
6872
delegate = OpenApiValidatorTest.TestRequestHandler(),
6973
specUrlOrPayload = "openapi.yml",
70-
additionalResponseValidationFunctions = listOf({ _, _ -> throw ResponseValidationFailedException() })
74+
additionalResponseValidationFunctions = listOf({ _, _ -> throw ResponseValidationFailedException() }),
7175
)
7276
.handleRequest(GET("/tests").withAcceptHeader("application/json"), mockk())
7377
}
7478
.isInstanceOf(ResponseValidationFailedException::class.java)
7579
}
7680

7781
private class RequestValidationFailedException : RuntimeException("request validation failed")
82+
7883
private class ResponseValidationFailedException : RuntimeException("request validation failed")
7984

8085
private class TestRequestHandler : RequestHandler() {
81-
override val router = router {
82-
GET("/tests") { _: Request<Unit> ->
83-
ResponseEntity.ok("""{"name": "some"}""")
86+
override val router =
87+
router {
88+
GET("/tests") { _: Request<Unit> ->
89+
ResponseEntity.ok("""{"name": "some"}""")
90+
}
8491
}
85-
}
8692
}
8793

8894
private class InvalidTestRequestHandler : RequestHandler() {
89-
override val router = router {
90-
GET("/tests") { _: Request<Unit> ->
91-
ResponseEntity.notFound(Unit)
95+
override val router =
96+
router {
97+
GET("/tests") { _: Request<Unit> ->
98+
ResponseEntity.notFound(Unit)
99+
}
92100
}
93-
}
94101
}
95102
}

router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoBufUtils.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ object ProtoBufUtils {
1515

1616
fun removeWrapperObjects(json: String): String {
1717
return removeWrapperObjects(
18-
jacksonObjectMapper().readTree(json)
18+
jacksonObjectMapper().readTree(json),
1919
).toString()
2020
}
2121

@@ -38,7 +38,7 @@ object ProtoBufUtils {
3838
if (entry.value.size() > 0) {
3939
result.replace(
4040
entry.key,
41-
removeWrapperObjects(entry.value)
41+
removeWrapperObjects(entry.value),
4242
)
4343
}
4444
} else {

router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoDeserializationHandler.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ class ProtoDeserializationHandler : DeserializationHandler {
2222
MediaType.parse(input.contentType()).let { proto.isCompatibleWith(it) || protoStructuredSuffixWildcard.isCompatibleWith(it) }
2323
}
2424

25-
override fun deserialize(input: APIGatewayProxyRequestEvent, target: KType?): Any {
25+
override fun deserialize(
26+
input: APIGatewayProxyRequestEvent,
27+
target: KType?,
28+
): Any {
2629
val bytes = Base64.getDecoder().decode(input.body)
2730
val parser = (target?.classifier as KClass<*>).staticFunctions.first { it.name == "parser" }.call() as Parser<*>
2831
return parser.parseFrom(bytes)

router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoEnabledRequestHandler.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@ import io.moia.router.RequestHandler
66
import io.moia.router.ResponseEntity
77

88
abstract class ProtoEnabledRequestHandler : RequestHandler() {
9+
override fun serializationHandlers() = listOf(ProtoSerializationHandler()) + super.serializationHandlers()
910

10-
override fun serializationHandlers() =
11-
listOf(ProtoSerializationHandler()) + super.serializationHandlers()
12-
13-
override fun deserializationHandlers() =
14-
listOf(ProtoDeserializationHandler()) + super.deserializationHandlers()
11+
override fun deserializationHandlers() = listOf(ProtoDeserializationHandler()) + super.deserializationHandlers()
1512

1613
override fun <T> createResponse(
1714
contentType: MediaType,
18-
response: ResponseEntity<T>
15+
response: ResponseEntity<T>,
1916
): APIGatewayProxyResponseEvent {
2017
return super.createResponse(contentType, response).withIsBase64Encoded(true)
2118
}

router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoSerializationHandler.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ import isCompatibleWith
77
import java.util.Base64
88

99
class ProtoSerializationHandler : SerializationHandler {
10-
1110
private val json = MediaType.parse("application/json")
1211
private val jsonStructuredSuffixWildcard = MediaType.parse("application/*+json")
1312

14-
override fun supports(acceptHeader: MediaType, body: Any): Boolean =
15-
body is GeneratedMessageV3
13+
override fun supports(
14+
acceptHeader: MediaType,
15+
body: Any,
16+
): Boolean = body is GeneratedMessageV3
1617

17-
override fun serialize(acceptHeader: MediaType, body: Any): String {
18+
override fun serialize(
19+
acceptHeader: MediaType,
20+
body: Any,
21+
): String {
1822
val message = body as GeneratedMessageV3
1923
return if (json.isCompatibleWith(acceptHeader) || jsonStructuredSuffixWildcard.isCompatibleWith(acceptHeader)) {
2024
ProtoBufUtils.toJsonWithoutWrappers(message)

0 commit comments

Comments
 (0)