Skip to content

Commit

Permalink
Move mutable settings out of constructor parameters in the generators…
Browse files Browse the repository at this point in the history
…. Most of the time these are accessed in multiple locations, so having them overridden in the generators leads to inconsistent behaviour (#274)
  • Loading branch information
cjbooms authored Apr 11, 2024
1 parent 5e2ee8e commit 9e2d9f8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/com/cjbooms/fabrikt/cli/CodeGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ class CodeGenerator(
private fun resourceSet(resFiles: Collection<ResourceFile>) = setOf(ResourceSourceSet(resFiles, resourcesPath))

private fun models(): Models =
JacksonModelGenerator(packages, sourceApi, MutableSettings.modelOptions(), MutableSettings.validationLibrary().annotations, MutableSettings.externalRefResolutionMode()).generate()
JacksonModelGenerator(packages, sourceApi).generate()

private fun resources(models: Models): List<ResourceFile> =
listOfNotNull(QuarkusReflectionModelGenerator(models, MutableSettings.generationTypes()).generate())
listOfNotNull(QuarkusReflectionModelGenerator(models).generate())

private fun controllers(): KotlinTypes {
val generator =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.cjbooms.fabrikt.cli.ModelCodeGenOptionType.SEALED_INTERFACES_FOR_ONE_
import com.cjbooms.fabrikt.configurations.Packages
import com.cjbooms.fabrikt.generators.ClassSettings
import com.cjbooms.fabrikt.generators.GeneratorUtils.toClassName
import com.cjbooms.fabrikt.generators.JavaxValidationAnnotations
import com.cjbooms.fabrikt.generators.MutableSettings
import com.cjbooms.fabrikt.generators.PropertyUtils.addToClass
import com.cjbooms.fabrikt.generators.PropertyUtils.isNullable
import com.cjbooms.fabrikt.generators.TypeFactory.createList
Expand Down Expand Up @@ -69,10 +69,10 @@ import java.net.URL
class JacksonModelGenerator(
private val packages: Packages,
private val sourceApi: SourceApi,
private val options: Set<ModelCodeGenOptionType> = emptySet(),
private val validationAnnotations: ValidationAnnotations = JavaxValidationAnnotations,
private val externalRefResolutionMode: ExternalReferencesResolutionMode = ExternalReferencesResolutionMode.TARGETED,
) {
private val options = MutableSettings.modelOptions()
private val validationAnnotations: ValidationAnnotations = MutableSettings.validationLibrary().annotations
private val externalRefResolutionMode: ExternalReferencesResolutionMode = MutableSettings.externalRefResolutionMode()
companion object {
fun toModelType(basePackage: String, typeInfo: KotlinTypeInfo, isNullable: Boolean = false): TypeName {
val className =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cjbooms.fabrikt.generators.model

import com.cjbooms.fabrikt.cli.CodeGenerationType
import com.cjbooms.fabrikt.generators.MutableSettings
import com.cjbooms.fabrikt.model.Models
import com.cjbooms.fabrikt.model.QuarkusReflectionModel
import com.cjbooms.fabrikt.model.ResourceFile
Expand All @@ -10,8 +11,8 @@ import com.fasterxml.jackson.module.kotlin.registerKotlinModule

class QuarkusReflectionModelGenerator(
private val models: Models,
private val generationTypes: Set<CodeGenerationType> = emptySet()
) {
private val generationTypes: Set<CodeGenerationType> = MutableSettings.generationTypes()
companion object {
const val RESOURCE_FILE_NAME = "reflection-config.json"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class ModelGeneratorTest {
@MethodSource("testCases")
fun `correct models are generated for different OpenApi Specifications`(testCaseName: String) {
print("Testcase: $testCaseName")
MutableSettings.updateSettings()
MutableSettings.addOption(ModelCodeGenOptionType.X_EXTENSIBLE_ENUMS)
if (testCaseName == "instantDateTime") {
MutableSettings.addOption(CodeGenTypeOverride.DATETIME_AS_INSTANT)
Expand All @@ -88,7 +89,6 @@ class ModelGeneratorTest {
val models = JacksonModelGenerator(
Packages(basePackage),
sourceApi,
setOf(ModelCodeGenOptionType.SEALED_INTERFACES_FOR_ONE_OF),
).generate().toSingleFile()

assertThat(models).isEqualTo(expectedModels)
Expand All @@ -99,11 +99,13 @@ class ModelGeneratorTest {
val basePackage = "examples.jakartaValidationAnnotations"
val spec = readTextResource("/examples/jakartaValidationAnnotations/api.yaml")
val expectedJakartaModel = readTextResource("/examples/jakartaValidationAnnotations/models/Models.kt")
MutableSettings.updateSettings(genTypes = setOf(CodeGenerationType.HTTP_MODELS))
MutableSettings.updateSettings(
genTypes = setOf(CodeGenerationType.HTTP_MODELS),
validationLibrary = ValidationLibrary.JAKARTA_VALIDATION
)
val models = JacksonModelGenerator(
Packages(basePackage),
SourceApi(spec),
validationAnnotations = ValidationLibrary.JAKARTA_VALIDATION.annotations,
).generate()

assertThat(models.files.size).isEqualTo(4)
Expand Down Expand Up @@ -131,11 +133,12 @@ class ModelGeneratorTest {
val basePackage = "examples.javaSerializableModels"
val spec = readTextResource("/examples/javaSerializableModels/api.yaml")
val expectedModels = readTextResource("/examples/javaSerializableModels/models/Models.kt")

MutableSettings.updateSettings(
modelOptions = setOf(ModelCodeGenOptionType.JAVA_SERIALIZATION),
)
val models = JacksonModelGenerator(
Packages(basePackage),
SourceApi(spec),
setOf(ModelCodeGenOptionType.JAVA_SERIALIZATION),
)
.generate()
.toSingleFile()
Expand Down Expand Up @@ -188,11 +191,13 @@ class ModelGeneratorTest {
val basePackage = "examples.quarkusReflectionModels"
val spec = readTextResource("/examples/quarkusReflectionModels/api.yaml")
val expectedModels = readTextResource("/examples/quarkusReflectionModels/models/Models.kt")
MutableSettings.updateSettings(
modelOptions = setOf(ModelCodeGenOptionType.QUARKUS_REFLECTION),
)

val models = JacksonModelGenerator(
Packages(basePackage),
SourceApi(spec),
setOf(ModelCodeGenOptionType.QUARKUS_REFLECTION),
)
.generate()
.toSingleFile()
Expand All @@ -205,11 +210,13 @@ class ModelGeneratorTest {
val basePackage = "examples.micronautIntrospectedModels"
val spec = readTextResource("/examples/micronautIntrospectedModels/api.yaml")
val expectedModels = readTextResource("/examples/micronautIntrospectedModels/models/Models.kt")
MutableSettings.updateSettings(
modelOptions = setOf(ModelCodeGenOptionType.MICRONAUT_INTROSPECTION),
)

val models = JacksonModelGenerator(
Packages(basePackage),
SourceApi(spec),
setOf(ModelCodeGenOptionType.MICRONAUT_INTROSPECTION),
)
.generate()
.toSingleFile()
Expand All @@ -222,11 +229,13 @@ class ModelGeneratorTest {
val basePackage = "examples.micronautReflectionModels"
val spec = readTextResource("/examples/micronautReflectionModels/api.yaml")
val expectedModels = readTextResource("/examples/micronautReflectionModels/models/Models.kt")
MutableSettings.updateSettings(
modelOptions = setOf(ModelCodeGenOptionType.MICRONAUT_REFLECTION),
)

val models = JacksonModelGenerator(
Packages(basePackage),
SourceApi(spec),
setOf(ModelCodeGenOptionType.MICRONAUT_REFLECTION),
)
.generate()
.toSingleFile()
Expand All @@ -239,11 +248,13 @@ class ModelGeneratorTest {
val basePackage = "examples.companionObject"
val spec = readTextResource("/examples/companionObject/api.yaml")
val expectedModels = readTextResource("/examples/companionObject/models/Models.kt")
MutableSettings.updateSettings(
modelOptions = setOf(ModelCodeGenOptionType.INCLUDE_COMPANION_OBJECT),
)

val models = JacksonModelGenerator(
Packages(basePackage),
SourceApi(spec),
setOf(ModelCodeGenOptionType.INCLUDE_COMPANION_OBJECT),
)
.generate()
.toSingleFile()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ class OkHttpClientGeneratorTest {
val expectedModel = readTextResource("/examples/externalReferences/aggressive/models/Models.kt")
val expectedClient = readTextResource("/examples/externalReferences/aggressive/client/ApiClient.kt")
val expectedClientCode = readTextResource("/examples/externalReferences/aggressive/client/ApiService.kt")
MutableSettings.updateSettings(
externalRefResolutionMode = ExternalReferencesResolutionMode.AGGRESSIVE,
)

val models = JacksonModelGenerator(
packages,
sourceApi,
externalRefResolutionMode = ExternalReferencesResolutionMode.AGGRESSIVE
).generate().toSingleFile()
val generator =
OkHttpEnhancedClientGenerator(packages, sourceApi)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ class ResourceGeneratorTest {
val sourceApi = SourceApi(apiLocation!!.readText(), baseDir = Paths.get(apiLocation.toURI()))
val expectedResource =
javaClass.getResource("/examples/$testCaseName/resources/reflection-config.json")!!.readText()
val generationTypes = setOf(CodeGenerationType.QUARKUS_REFLECTION_CONFIG)
MutableSettings.updateSettings(
genTypes = setOf(CodeGenerationType.QUARKUS_REFLECTION_CONFIG),
)

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

val resources = QuarkusReflectionModelGenerator(models, generationTypes).generate()?.toSingleFile()
val resources = QuarkusReflectionModelGenerator(models).generate()?.toSingleFile()

assertThat(resources).isEqualTo(expectedResource)
}
Expand Down

0 comments on commit 9e2d9f8

Please sign in to comment.