Skip to content

Commit

Permalink
fix: kotlin warnings (#14398)
Browse files Browse the repository at this point in the history
  • Loading branch information
colesnodgrass committed Oct 18, 2024
1 parent e5c3c54 commit b8c9ed4
Show file tree
Hide file tree
Showing 67 changed files with 1,068 additions and 892 deletions.
79 changes: 43 additions & 36 deletions airbyte-api/connector-builder-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ dependencies {
testImplementation(libs.kotlin.test.runner.junit5)
}

val connectorBuilderServerSpecFile = project(":oss:airbyte-connector-builder-server").file("src/main/openapi/openapi.yaml").path
val connectorBuilderServerSpecFile: String = project(":oss:airbyte-connector-builder-server").file("src/main/openapi/openapi.yaml").path

val genConnectorBuilderServerApiClient = tasks.register<GenerateTask>("genConnectorBuilderServerApiClient") {
val genConnectorBuilderServerApiClient =
tasks.register<GenerateTask>("genConnectorBuilderServerApiClient") {
val clientOutputDir = "${getLayout().buildDirectory.get()}/generated/connectorbuilderserverapi/client"

inputs.file(connectorBuilderServerSpecFile)
Expand All @@ -58,33 +59,35 @@ val genConnectorBuilderServerApiClient = tasks.register<GenerateTask>("genConnec
invokerPackage = "io.airbyte.connectorbuilderserver.api.client.invoker.generated"
modelPackage = "io.airbyte.connectorbuilderserver.api.client.model.generated"

schemaMappings = mapOf(
"ConnectorConfig" to "com.fasterxml.jackson.databind.JsonNode",
"ConnectorManifest" to "com.fasterxml.jackson.databind.JsonNode",
"ConnectorBuilderProjectTestingValues" to "com.fasterxml.jackson.databind.JsonNode",
)
schemaMappings =
mapOf(
"ConnectorConfig" to "com.fasterxml.jackson.databind.JsonNode",
"ConnectorManifest" to "com.fasterxml.jackson.databind.JsonNode",
"ConnectorBuilderProjectTestingValues" to "com.fasterxml.jackson.databind.JsonNode",
)

generateApiDocumentation = false

configOptions = mapOf(
"enumPropertyNaming" to "UPPERCASE",
"generatePom" to "false",
"interfaceOnly" to "true",
"serializationLibrary" to "jackson",
)
configOptions =
mapOf(
"enumPropertyNaming" to "UPPERCASE",
"generatePom" to "false",
"interfaceOnly" to "true",
"serializationLibrary" to "jackson",
)

doLast {
// Delete file generated by the client task
delete(file("${outputDir.get()}/src/main/kotlin/org"))
// Delete file generated by the client task
delete(file("${outputDir.get()}/src/main/kotlin/org"))

val generatedDomainClientsPath = "${outputDir.get()}/src/main/kotlin/io/airbyte/connectorbuilderserver/api/client/generated"
updateDomainClientsWithFailsafe(generatedDomainClientsPath)
// the kotlin client (as opposed to the java client) doesn't include the response body in the exception message.
updateDomainClientsToIncludeHttpResponseBodyOnClientException(generatedDomainClientsPath)
val generatedDomainClientsPath = "${outputDir.get()}/src/main/kotlin/io/airbyte/connectorbuilderserver/api/client/generated"
updateDomainClientsWithFailsafe(generatedDomainClientsPath)
// the kotlin client (as opposed to the java client) doesn't include the response body in the exception message.
updateDomainClientsToIncludeHttpResponseBodyOnClientException(generatedDomainClientsPath)
}

dependsOn(":oss:airbyte-api:server-api:genApiClient")
}
}

sourceSets {
main {
Expand All @@ -105,7 +108,7 @@ tasks.withType<JavaCompile>().configureEach {
}

tasks.named("compileKotlin") {
dependsOn(genConnectorBuilderServerApiClient)
dependsOn(genConnectorBuilderServerApiClient)
}

// uses afterEvaluate because at configuration time, the kspKotlin task does not exist.
Expand All @@ -119,7 +122,7 @@ afterEvaluate {
// still runs into spotbug issues. Working theory is that
// generated code is being picked up. Disable as a short-term fix.
tasks.named("spotbugsMain") {
enabled = false
enabled = false
}

private fun updateDomainClientsWithFailsafe(clientPath: String) {
Expand All @@ -132,13 +135,15 @@ private fun updateDomainClientsWithFailsafe(clientPath: String) {
var domainClientFileText = domainClient.readText()

// replace class declaration
domainClientFileText = domainClientFileText.replace(
"class (\\S+)\\(basePath: kotlin.String = defaultBasePath, client: OkHttpClient = ApiClient.defaultClient\\) : ApiClient\\(basePath, client\\)".toRegex(),
"class $1(basePath: kotlin.String = defaultBasePath, client: OkHttpClient = ApiClient.defaultClient, policy : RetryPolicy<okhttp3.Response> = RetryPolicy.ofDefaults()) : ApiClient(basePath, client, policy)"
)
domainClientFileText =
domainClientFileText.replace(
"class (\\S+)\\(basePath: kotlin.String = defaultBasePath, client: OkHttpClient = ApiClient.defaultClient\\) : ApiClient\\(basePath, client\\)"
.toRegex(),
"class $1(basePath: kotlin.String = defaultBasePath, client: OkHttpClient = ApiClient.defaultClient, policy : RetryPolicy<okhttp3.Response> = RetryPolicy.ofDefaults()) : ApiClient(basePath, client, policy)",
)

// add imports if not exist
if(!domainClientFileText.contains("import dev.failsafe.RetryPolicy")) {
if (!domainClientFileText.contains("import dev.failsafe.RetryPolicy")) {
val newImports = "import dev.failsafe.RetryPolicy"
domainClientFileText = domainClientFileText.replaceFirst("import ", "$newImports\nimport ")
}
Expand All @@ -149,14 +154,16 @@ private fun updateDomainClientsWithFailsafe(clientPath: String) {
}

private fun updateDomainClientsToIncludeHttpResponseBodyOnClientException(clientPath: String) {
val dir = file(clientPath)
dir.walk().forEach { domainClient ->
if (domainClient.name.endsWith(".kt")) {
val domainClientFileText = domainClient.readText().replace(
"throw ClientException(\"Client error : \${localVarError.statusCode} \${localVarError.message.orEmpty()}\", localVarError.statusCode, localVarResponse)",
"throw ClientException(\"Client error : \${localVarError.statusCode} \${localVarError.message.orEmpty()} \${localVarError.body ?: \"\"}\", localVarError.statusCode, localVarResponse)")

domainClient.writeText(domainClientFileText)
}
val dir = file(clientPath)
dir.walk().forEach { domainClient ->
if (domainClient.name.endsWith(".kt")) {
val domainClientFileText =
domainClient.readText().replace(
"throw ClientException(\"Client error : \${localVarError.statusCode} \${localVarError.message.orEmpty()}\", localVarError.statusCode, localVarResponse)",
"throw ClientException(\"Client error : \${localVarError.statusCode} \${localVarError.message.orEmpty()} \${localVarError.body ?: \"\"}\", localVarError.statusCode, localVarResponse)",
)

domainClient.writeText(domainClientFileText)
}
}
}
62 changes: 32 additions & 30 deletions airbyte-api/public-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ dependencies {
testImplementation(libs.kotlin.test.runner.junit5)
}

val internalApiSpecFile = project(":oss:airbyte-api:server-api").file("src/main/openapi/config.yaml").getPath()
val internalApiSpecFile = project(":oss:airbyte-api:server-api").file("src/main/openapi/config.yaml").path

val genPublicApiServer = tasks.register<GenerateTask>("generatePublicApiServer") {
val genPublicApiServer =
tasks.register<GenerateTask>("generatePublicApiServer") {
val serverOutputDir = "${getLayout().buildDirectory.get()}/generated/public_api/server"

inputs.file(internalApiSpecFile)
Expand All @@ -59,30 +60,32 @@ val genPublicApiServer = tasks.register<GenerateTask>("generatePublicApiServer")

generateApiDocumentation = false

configOptions = mapOf(
"dateLibrary" to "java8",
"enumPropertyNaming" to "UPPERCASE",
"generatePom" to "false",
"interfaceOnly" to "true",
"library" to "jaxrs-spec",
"returnResponse" to "true",
"useBeanValidation" to "true",
"performBeanValidation" to "true",
"additionalModelTypeAnnotations" to "@io.micronaut.core.annotation.Introspected",
"additionalEnumTypeAnnotations" to "@io.micronaut.core.annotation.Introspected",
"useTags" to "true",
"useJakartaEe" to "true",
)

schemaMappings = mapOf(
"SourceConfiguration" to "com.fasterxml.jackson.databind.JsonNode",
"OAuthInputConfiguration" to "com.fasterxml.jackson.databind.JsonNode",
"OAuthCredentialsConfiguration" to "com.fasterxml.jackson.databind.JsonNode",
"DestinationConfiguration" to "com.fasterxml.jackson.databind.JsonNode",
"MapperConfiguration" to "com.fasterxml.jackson.databind.JsonNode",
"ConnectorBuilderProjectTestingValues" to "com.fasterxml.jackson.databind.JsonNode",
)
}
configOptions =
mapOf(
"dateLibrary" to "java8",
"enumPropertyNaming" to "UPPERCASE",
"generatePom" to "false",
"interfaceOnly" to "true",
"library" to "jaxrs-spec",
"returnResponse" to "true",
"useBeanValidation" to "true",
"performBeanValidation" to "true",
"additionalModelTypeAnnotations" to "@io.micronaut.core.annotation.Introspected",
"additionalEnumTypeAnnotations" to "@io.micronaut.core.annotation.Introspected",
"useTags" to "true",
"useJakartaEe" to "true",
)

schemaMappings =
mapOf(
"SourceConfiguration" to "com.fasterxml.jackson.databind.JsonNode",
"OAuthInputConfiguration" to "com.fasterxml.jackson.databind.JsonNode",
"OAuthCredentialsConfiguration" to "com.fasterxml.jackson.databind.JsonNode",
"DestinationConfiguration" to "com.fasterxml.jackson.databind.JsonNode",
"MapperConfiguration" to "com.fasterxml.jackson.databind.JsonNode",
"ConnectorBuilderProjectTestingValues" to "com.fasterxml.jackson.databind.JsonNode",
)
}

sourceSets {
main {
Expand All @@ -104,13 +107,12 @@ sourceSets {
}
}


tasks.withType<JavaCompile>().configureEach {
options.compilerArgs = listOf("-parameters")
}

tasks.named("compileKotlin") {
dependsOn(genPublicApiServer)
dependsOn(genPublicApiServer)
}

// uses afterEvaluate because at configuration time, the kspKotlin task does not exist.
Expand All @@ -124,5 +126,5 @@ afterEvaluate {
// still runs into spotbug issues. Working theory is that
// generated code is being picked up. Disable as a short-term fix.
tasks.named("spotbugsMain") {
enabled = false
}
enabled = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ internal class IntentSecurityRuleTest {

@Test
fun `order is below micronaut security @Secured annotation`() {
assert(intentSecurityRule.getOrder() < SecuredAnnotationRule.ORDER)
assert(intentSecurityRule.order < SecuredAnnotationRule.ORDER)
}

@Test
fun `check returns UKNOWN if annotation not present on route`() {
every { routeMatch.isAnnotationPresent(RequiresIntent::class.java) } returns false

val result = intentSecurityRule.check(request, authentication)
StepVerifier.create(result)
StepVerifier
.create(result)
.expectNext(SecurityRuleResult.UNKNOWN)
.verifyComplete()
}
Expand All @@ -66,7 +67,8 @@ internal class IntentSecurityRuleTest {
every { authentication.roles } returns null

val result = intentSecurityRule.check(request, authentication)
StepVerifier.create(result)
StepVerifier
.create(result)
.expectNext(SecurityRuleResult.UNKNOWN)
.verifyComplete()
}
Expand All @@ -76,7 +78,8 @@ internal class IntentSecurityRuleTest {
every { Intent.UploadCustomConnector.roles } returns emptySet()

val result = intentSecurityRule.check(request, authentication)
StepVerifier.create(result)
StepVerifier
.create(result)
.expectNext(SecurityRuleResult.REJECTED)
.verifyComplete()
}
Expand All @@ -87,7 +90,8 @@ internal class IntentSecurityRuleTest {
every { authentication.roles } returns listOf(validRole)

val result = intentSecurityRule.check(request, authentication)
StepVerifier.create(result)
StepVerifier
.create(result)
.expectNext(SecurityRuleResult.ALLOWED)
.verifyComplete()
}
Expand All @@ -97,7 +101,8 @@ internal class IntentSecurityRuleTest {
every { authentication.roles } returns listOf("useless-role-1", "useless-role-2")

val result = intentSecurityRule.check(request, authentication)
StepVerifier.create(result)
StepVerifier
.create(result)
.expectNext(SecurityRuleResult.REJECTED)
.verifyComplete()
}
Expand All @@ -107,7 +112,8 @@ internal class IntentSecurityRuleTest {
every { request.getAttribute(HttpAttributes.ROUTE_MATCH, RouteMatch::class.java) } returns Optional.empty()

val result = intentSecurityRule.check(request, authentication)
StepVerifier.create(result)
StepVerifier
.create(result)
.expectNext(SecurityRuleResult.UNKNOWN)
.verifyComplete()
}
Expand Down
Loading

0 comments on commit b8c9ed4

Please sign in to comment.