Skip to content

Commit

Permalink
Add --serialization-library option
Browse files Browse the repository at this point in the history
  • Loading branch information
ulrikandersen committed Oct 17, 2024
1 parent b95cb5b commit 2a3ee2b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ This section documents the available CLI parameters for controlling what gets ge
| `--http-model-suffix` | Specify custom suffix for all generated model classes. Defaults to no suffix. |
| `--output-directory` | Allows the generation dir to be overridden. Defaults to current dir |
| `--resources-path` | Allows the path for generated resources to be overridden. Defaults to `src/main/resources` |
| `--serialization-library` | Specify which serialization library to use for annotations in generated model classes. Default: JACKSON |
| | CHOOSE ONE OF: |
| | `JACKSON` - Use Jackson for serialization and deserialization |
| | `KOTLINX_SERIALIZATION` - **!EXPERIMENTAL!** Use kotlinx.serialization for serialization and deserialization |
| `--src-path` | Allows the path for generated source files to be overridden. Defaults to `src/main/kotlin` |
| `--targets` | Targets are the parts of the application that you want to be generated. |
| | CHOOSE ANY OF: |
Expand Down
8 changes: 5 additions & 3 deletions src/main/kotlin/com/cjbooms/fabrikt/cli/CodeGen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ object CodeGen {
codeGenArgs.srcPath,
codeGenArgs.resourcesPath,
codeGenArgs.validationLibrary,
codeGenArgs.externalRefResolutionMode
codeGenArgs.externalRefResolutionMode,
codeGenArgs.serializationLibrary,
)
}

Expand All @@ -55,7 +56,8 @@ object CodeGen {
srcPath: Path,
resourcesPath: Path,
validationLibrary: ValidationLibrary,
externalRefResolutionMode: ExternalReferencesResolutionMode
externalRefResolutionMode: ExternalReferencesResolutionMode,
serializationLibrary: SerializationLibrary,
) {
MutableSettings.updateSettings(
codeGenTypes,
Expand All @@ -68,7 +70,7 @@ object CodeGen {
typeOverrides,
validationLibrary,
externalRefResolutionMode
)
, serializationLibrary)

val suppliedApi = pathToApi.toFile().readText()
val baseDir = pathToApi.parent
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/com/cjbooms/fabrikt/cli/CodeGenArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ class CodeGenArgs {
converter = ExternalReferencesResolutionModeConverter::class
)
var externalRefResolutionMode: ExternalReferencesResolutionMode = ExternalReferencesResolutionMode.TARGETED

@Parameter(
names = ["--serialization-library"],
description = "Specify which serialization library to use for annotations in generated model classes. Default: JACKSON",
converter = SerializationLibraryOptionConverter::class
)
var serializationLibrary: SerializationLibrary = SerializationLibrary.JACKSON
}

class CodeGenerationTypesConverter : IStringConverter<CodeGenerationType> {
Expand Down Expand Up @@ -193,6 +200,10 @@ class ExternalReferencesResolutionModeConverter: IStringConverter<ExternalRefere
override fun convert(value: String): ExternalReferencesResolutionMode = convertToEnumValue(value)
}

class SerializationLibraryOptionConverter : IStringConverter<SerializationLibrary> {
override fun convert(value: String): SerializationLibrary = convertToEnumValue(value)
}

class PackageNameValidator : IValueValidator<String> {
override fun validate(name: String, value: String) {
if (!value.isValidJavaPackage()) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/com/cjbooms/fabrikt/cli/CodeGenOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import com.cjbooms.fabrikt.generators.JakartaAnnotations
import com.cjbooms.fabrikt.generators.JavaxValidationAnnotations
import com.cjbooms.fabrikt.generators.NoValidationAnnotations
import com.cjbooms.fabrikt.generators.ValidationAnnotations
import com.cjbooms.fabrikt.model.SerializationAnnotations
import com.cjbooms.fabrikt.model.JacksonAnnotations
import com.cjbooms.fabrikt.model.KotlinxSerializationAnnotations

enum class CodeGenerationType(val description: String) {
HTTP_MODELS(
Expand Down Expand Up @@ -86,3 +89,10 @@ enum class ExternalReferencesResolutionMode(val description: String) {

override fun toString() = "`${super.toString()}` - $description"
}

enum class SerializationLibrary(val description: String, val serializationAnnotations: SerializationAnnotations) {
JACKSON("Use Jackson for serialization and deserialization", JacksonAnnotations),
KOTLINX_SERIALIZATION("**!EXPERIMENTAL!** Use kotlinx.serialization for serialization and deserialization", KotlinxSerializationAnnotations);

override fun toString() = "`${super.toString()}` - $description"
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ object MutableSettings {
private lateinit var typeOverrides: MutableSet<CodeGenTypeOverride>
private lateinit var validationLibrary: ValidationLibrary
private lateinit var externalRefResolutionMode: ExternalReferencesResolutionMode
private lateinit var serializationLibrary: SerializationLibrary

fun updateSettings(
genTypes: Set<CodeGenerationType> = emptySet(),
Expand All @@ -25,6 +26,7 @@ object MutableSettings {
typeOverrides: Set<CodeGenTypeOverride> = emptySet(),
validationLibrary: ValidationLibrary = ValidationLibrary.JAVAX_VALIDATION,
externalRefResolutionMode: ExternalReferencesResolutionMode = ExternalReferencesResolutionMode.TARGETED,
serializationLibrary: SerializationLibrary = SerializationLibrary.JACKSON,
) {
this.generationTypes = genTypes.toMutableSet()
this.controllerOptions = controllerOptions.toMutableSet()
Expand All @@ -36,6 +38,7 @@ object MutableSettings {
this.typeOverrides = typeOverrides.toMutableSet()
this.validationLibrary = validationLibrary
this.externalRefResolutionMode = externalRefResolutionMode
this.serializationLibrary = serializationLibrary
}

fun addOption(option: ModelCodeGenOptionType) = modelOptions.add(option)
Expand All @@ -51,4 +54,5 @@ object MutableSettings {
fun typeOverrides() = this.typeOverrides.toSet()
fun validationLibrary() = this.validationLibrary
fun externalRefResolutionMode() = this.externalRefResolutionMode
fun serializationLibrary() = this.serializationLibrary
}

0 comments on commit 2a3ee2b

Please sign in to comment.