Skip to content

Commit

Permalink
Support overriding the type string with format binary
Browse files Browse the repository at this point in the history
  • Loading branch information
acanda committed Sep 20, 2024
1 parent 5a63915 commit 133b6b1
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ fabrikt {
quarkusReflectionConfig = enabled
typeOverrides {
datetime = OffsetDateTime
binary = ByteArray
}
client {
generate = disabled
Expand Down Expand Up @@ -125,6 +126,7 @@ fabrikt {
| sourcesPath | The path for generated source files, interpreted relative to the output directory. | `src/main/kotlin` |
| resourcesPath | The path for generated resource files, interpreted relative to the output directory. | `src/main/resources` |
| typeOverrides.datetime | Specifies the Kotlin type for the OAS type `datetime`.<br/>Values: `OffsetDateTime`, `Instant`, `LocalDateTime`. | `OffsetDateTime` |
| typeOverrides.binary | Specifies the Kotlin type for the OAS type `string` with format `binary`.<br/>Values: `ByteArray`, `InputStream`. | `ByteArray` |
| validationLibrary | Specifies the validation library used for annotations in generated model classes.<br/>Values: `Javax`, `Jakarta`, `NoValidation`. | `Jakarta` |
| quarkusReflectionConfig | Enables generating the reflection-config.json file for quarkus integration projects.<br/>Values: `enabled`, `disabled`, `true`, `false`. | `enabled` |
| client.generate | Enables generating the http client code.<br/>Values: `enabled`, `disabled`, `true`, `false`. | `disabled` |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ internal fun initializeWithDefaults(
validationLibrary.assign(source.validationLibrary, defaults.validationLibrary)
// same as model.quarkusReflection?
quarkusReflectionConfig.assign(source.quarkusReflectionConfig, defaults.quarkusReflectionConfig)
with(typeOverrides) {
datetime.assign(source.typeOverrides.datetime, defaults.typeOverrides.datetime)
binary.assign(source.typeOverrides.binary, defaults.typeOverrides.binary)
}
typeOverrides.datetime.assign(source.typeOverrides.datetime, defaults.typeOverrides.datetime)
with(client) {
val s = source.client
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/ch/acanda/gradle/fabrikt/FabriktExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ open class TypeOverridesExtension @Inject constructor(objects: ObjectFactory) {
@Suppress("VariableNaming")
val LocalDateTime: DateTimeOverrideOption = DateTimeOverrideOption.LocalDateTime

val binary: Property<BinaryOverrideOption> = objects.property(BinaryOverrideOption::class.java)

@Suppress("VariableNaming")
val ByteArray: BinaryOverrideOption = BinaryOverrideOption.ByteArray

@Suppress("VariableNaming")
val InputStream: BinaryOverrideOption = BinaryOverrideOption.InputStream

}

open class GenerateClientExtension @Inject constructor(objects: ObjectFactory) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/ch/acanda/gradle/fabrikt/FabriktOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ enum class DateTimeOverrideOption(
LocalDateTime(CodeGenTypeOverride.DATETIME_AS_LOCALDATETIME),
}

enum class BinaryOverrideOption(
override val fabriktOption: CodeGenTypeOverride?,
) : FabriktOption {
ByteArray(null),
InputStream(CodeGenTypeOverride.BYTEARRAY_AS_INPUTSTREAM),
}

enum class ValidationLibraryOption(
override val fabriktOption: ValidationLibrary,
) : FabriktOption {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ open class TypeOverridesConfiguration @Inject constructor(objects: ObjectFactory
@Suppress("VariableNaming")
val LocalDateTime: DateTimeOverrideOption = DateTimeOverrideOption.LocalDateTime

@get:Input
@get:Optional
val binary: Property<BinaryOverrideOption> = objects.property(BinaryOverrideOption::class.java)

@get:Internal
@Suppress("VariableNaming")
val ByteArray: BinaryOverrideOption = BinaryOverrideOption.ByteArray

@get:Internal
@Suppress("VariableNaming")
val InputStream: BinaryOverrideOption = BinaryOverrideOption.InputStream

}

open class GenerateClientConfiguration @Inject constructor(objects: ObjectFactory) {
Expand Down Expand Up @@ -347,6 +359,15 @@ open class TypeOverridesDefaults @Inject constructor(objects: ObjectFactory) {
@Suppress("VariableNaming")
val LocalDateTime: DateTimeOverrideOption = DateTimeOverrideOption.LocalDateTime

val binary: Property<BinaryOverrideOption> =
objects.property(BinaryOverrideOption::class.java).convention(BinaryOverrideOption.ByteArray)

@Suppress("VariableNaming")
val ByteArray: BinaryOverrideOption = BinaryOverrideOption.ByteArray

@Suppress("VariableNaming")
val InputStream: BinaryOverrideOption = BinaryOverrideOption.InputStream

}

open class GenerateClientDefaults @Inject constructor(objects: ObjectFactory) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package ch.acanda.gradle.fabrikt.generator

import ch.acanda.gradle.fabrikt.FabriktOption
import ch.acanda.gradle.fabrikt.GenerateTaskConfiguration
import com.cjbooms.fabrikt.cli.ClientCodeGenOptionType
import com.cjbooms.fabrikt.cli.CodeGenerationType
import com.cjbooms.fabrikt.cli.ControllerCodeGenOptionType
import com.cjbooms.fabrikt.cli.ModelCodeGenOptionType
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider

internal const val ARG_API_FILE = "--api-file"
Expand Down Expand Up @@ -64,12 +66,14 @@ internal data class FabriktArguments(private val config: GenerateTaskConfigurati
}

private fun GenerateTaskConfiguration.addTypeOverridesArgs(args: MutableList<String>) = with(typeOverrides) {
val overrides = listOfNotNull(
datetime.orNull?.fabriktOption
)
if (overrides.isNotEmpty()) {
addTypeOverrideArg(args, datetime)
addTypeOverrideArg(args, binary)
}

private fun addTypeOverrideArg(args: MutableList<String>, property: Property<out FabriktOption>) {
property.orNull?.fabriktOption?.let { option ->
args.add(ARG_TYPE_OVERRIDES)
args.addAll(overrides.map { it.name })
args.add(option.name)
}
}

Expand Down
1 change: 1 addition & 0 deletions src/test/kotlin/ch/acanda/gradle/fabrikt/Generators.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ internal val generateTaskExtGen: Arb<GenerateTaskExtension> = arbitrary {
resourcesPath.set(Arb.stringPattern("[a-z]{1,5}(/[a-z]{1,5}){0,3}").orNull(0.2).bind())
quarkusReflectionConfig.set(Arb.boolean().orNull(0.2).bind())
typeOverrides.datetime.set(Arb.enum<DateTimeOverrideOption>().orNull(0.2).bind())
typeOverrides.binary.set(Arb.enum<BinaryOverrideOption>().orNull(0.2).bind())
validationLibrary.set(Arb.enum<ValidationLibraryOption>().orNull(0.2).bind())
client.generate.set(Arb.boolean().orNull(0.2).bind())
client.resilience4j.set(Arb.boolean().orNull(0.2).bind())
Expand Down
2 changes: 2 additions & 0 deletions src/test/kotlin/ch/acanda/gradle/fabrikt/GradleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class GradleTest : StringSpec({
| quarkusReflectionConfig = enabled
| typeOverrides {
| datetime = Instant
| binary = InputStream
| }
| client {
| generate = enabled
Expand Down Expand Up @@ -118,6 +119,7 @@ class GradleTest : StringSpec({
| quarkusReflectionConfig = enabled
| typeOverrides {
| datetime = Instant
| binary = InputStream
| }
| client {
| generate = enabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class FabriktArgumentsTest : StringSpec({
}
with(config.typeOverrides) {
cliArgs.shouldContainOptionally(datetime, ARG_TYPE_OVERRIDES)
cliArgs.shouldContainOptionally(binary, ARG_TYPE_OVERRIDES)
}
with(config.client) {
if (generate.get()) {
Expand Down

0 comments on commit 133b6b1

Please sign in to comment.