Skip to content

Commit

Permalink
Add option to specify custom model suffix (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
shanio authored Oct 2, 2024
1 parent 108ec27 commit d553278
Show file tree
Hide file tree
Showing 41 changed files with 1,148 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ This section documents the available CLI parameters for controlling what gets ge
| | `INCLUDE_COMPANION_OBJECT` - This option adds a companion object to the generated models. |
| | `SEALED_INTERFACES_FOR_ONE_OF` - This option enables the generation of interfaces for discriminated oneOf types |
| | `NON_NULL_MAP_VALUES` - This option makes map values non-null. The default (since v15) and most spec compliant is make map values nullable |
| `--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` |
| `--src-path` | Allows the path for generated source files to be overridden. Defaults to `src/main/kotlin` |
Expand Down
15 changes: 14 additions & 1 deletion src/main/kotlin/com/cjbooms/fabrikt/cli/CodeGen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ object CodeGen {
codeGenArgs.controllerOptions,
codeGenArgs.controllerTarget,
codeGenArgs.modelOptions,
codeGenArgs.modelSuffix,
codeGenArgs.clientOptions,
codeGenArgs.clientTarget,
codeGenArgs.typeOverrides,
Expand All @@ -47,6 +48,7 @@ object CodeGen {
controllerOptions: Set<ControllerCodeGenOptionType>,
controllerTarget: ControllerCodeGenTargetType,
modelOptions: Set<ModelCodeGenOptionType>,
modelSuffix: String,
clientOptions: Set<ClientCodeGenOptionType>,
clientTarget: ClientCodeGenTargetType,
typeOverrides: Set<CodeGenTypeOverride>,
Expand All @@ -55,7 +57,18 @@ object CodeGen {
validationLibrary: ValidationLibrary,
externalRefResolutionMode: ExternalReferencesResolutionMode
) {
MutableSettings.updateSettings(codeGenTypes, controllerOptions, controllerTarget, modelOptions, clientOptions, clientTarget, typeOverrides, validationLibrary, externalRefResolutionMode)
MutableSettings.updateSettings(
codeGenTypes,
controllerOptions,
controllerTarget,
modelOptions,
modelSuffix,
clientOptions,
clientTarget,
typeOverrides,
validationLibrary,
externalRefResolutionMode
)

val suppliedApi = pathToApi.toFile().readText()
val baseDir = pathToApi.parent
Expand Down
8 changes: 7 additions & 1 deletion src/main/kotlin/com/cjbooms/fabrikt/cli/CodeGenArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import com.beust.jcommander.ParameterException
import com.beust.jcommander.converters.PathConverter
import com.cjbooms.fabrikt.model.Destinations
import com.cjbooms.fabrikt.util.NormalisedString.isValidJavaPackage
import com.cjbooms.fabrikt.util.toUpperCase
import java.nio.file.InvalidPathException
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.system.exitProcess
import com.cjbooms.fabrikt.util.toUpperCase

class CodeGenArgs {

Expand Down Expand Up @@ -99,6 +99,12 @@ class CodeGenArgs {
)
var modelOptions: Set<ModelCodeGenOptionType> = emptySet()

@Parameter(
names = ["--http-model-suffix"],
description = "Specify custom suffix for all generated model classes. Defaults to no suffix."
)
var modelSuffix: String = ""

@Parameter(
names = ["--http-client-opts"],
description = "Select the options for the http client code that you want to be generated.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ object MutableSettings {
private lateinit var controllerOptions: MutableSet<ControllerCodeGenOptionType>
private lateinit var controllerTarget: ControllerCodeGenTargetType
private lateinit var modelOptions: MutableSet<ModelCodeGenOptionType>
private lateinit var modelSuffix: String
private lateinit var clientOptions: MutableSet<ClientCodeGenOptionType>
private lateinit var clientTarget: ClientCodeGenTargetType
private lateinit var typeOverrides: MutableSet<CodeGenTypeOverride>
Expand All @@ -18,6 +19,7 @@ object MutableSettings {
controllerOptions: Set<ControllerCodeGenOptionType> = emptySet(),
controllerTarget: ControllerCodeGenTargetType = ControllerCodeGenTargetType.SPRING,
modelOptions: Set<ModelCodeGenOptionType> = emptySet(),
modelSuffix: String = "",
clientOptions: Set<ClientCodeGenOptionType> = emptySet(),
clientTarget: ClientCodeGenTargetType = ClientCodeGenTargetType.OK_HTTP,
typeOverrides: Set<CodeGenTypeOverride> = emptySet(),
Expand All @@ -28,6 +30,7 @@ object MutableSettings {
this.controllerOptions = controllerOptions.toMutableSet()
this.controllerTarget = controllerTarget
this.modelOptions = modelOptions.toMutableSet()
this.modelSuffix = modelSuffix
this.clientOptions = clientOptions.toMutableSet()
this.clientTarget = clientTarget
this.typeOverrides = typeOverrides.toMutableSet()
Expand All @@ -42,6 +45,7 @@ object MutableSettings {
fun controllerOptions() = this.controllerOptions.toSet()
fun controllerTarget() = this.controllerTarget
fun modelOptions() = this.modelOptions.toSet()
fun modelSuffix() = this.modelSuffix
fun clientOptions() = this.clientOptions.toSet()
fun clientTarget() = this.clientTarget
fun typeOverrides() = this.typeOverrides.toSet()
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/com/cjbooms/fabrikt/util/ModelNameRegistry.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cjbooms.fabrikt.util

import com.cjbooms.fabrikt.generators.MutableSettings
import com.cjbooms.fabrikt.model.EnclosingSchemaInfo
import com.cjbooms.fabrikt.model.EnclosingSchemaInfoName
import com.cjbooms.fabrikt.model.EnclosingSchemaInfoOasModel
Expand Down Expand Up @@ -59,6 +60,8 @@ object ModelNameRegistry {
if (valueSuffix) {
append("Value")
}
val modelClassNameSuffix = MutableSettings.modelSuffix()
append(modelClassNameSuffix)
}

private fun EnclosingSchemaInfo.toModelClassName() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,29 @@ class ModelGeneratorTest {
tempDirectory.toFile().deleteRecursively()
}

@Test
fun `generate models with suffix`() {
MutableSettings.updateSettings(
genTypes = setOf(CodeGenerationType.HTTP_MODELS),
modelSuffix = "Dto"
)
val basePackage = "examples.modelSuffix"
val apiLocation = javaClass.getResource("/examples/modelSuffix/api.yaml")!!
val sourceApi = SourceApi(apiLocation.readText(), baseDir = Paths.get(apiLocation.toURI()))
val expectedModels = readFolder(Path.of("src/test/resources/examples/modelSuffix/models/"))

val models = JacksonModelGenerator(
Packages(basePackage),
sourceApi,
).generate()

models.files.forEach { file ->
val key = "${file.name}.kt"
val content = file.toString()
assertThat(content).isEqualTo(expectedModels[key])
}
}

@Test
fun `generate models using jakarta validation`() {
val basePackage = "examples.jakartaValidationAnnotations"
Expand Down
Loading

0 comments on commit d553278

Please sign in to comment.