Skip to content

Commit

Permalink
ISSUE#23 Ensures enums are only generated for x-extensible-enum defin…
Browse files Browse the repository at this point in the history
…itions when the option is enabled (#29)
  • Loading branch information
cjbooms authored Dec 15, 2020
1 parent be16d44 commit 33a6aec
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 200 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ This section documents the available CLI parameters for controlling what gets ge
| | `RESILIENCE4J` - Generates a fault tolerance service for the client using the following library "io.github.resilience4j:resilience4j-all:+"
| `--http-model-opts` | Select the options for the http models that you want to be generated.
| | CHOOSE ANY OF:
| | `X_EXTENSIBLE_ENUMS` - This option treats x-extensible-enums as enums
| | `JAVA_SERIALIZATION` - This option adds Java Serializable interface to the generated models
| | `QUARKUS_REFLECTION` - This option adds @RegisterForReflection to the generated models. Requires dependency "'io.quarkus:quarkus-core:+"
| `--output-directory` | Allows the generation dir to be overridden. Defaults to current dir
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/com/cjbooms/fabrikt/cli/CodeGen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.cjbooms.fabrikt.cli

import com.beust.jcommander.ParameterException
import com.cjbooms.fabrikt.configurations.Packages
import com.cjbooms.fabrikt.generators.MutableSettings
import com.cjbooms.fabrikt.model.SourceApi
import java.nio.file.Files
import java.nio.file.Path
Expand Down Expand Up @@ -46,7 +47,7 @@ object CodeGen {
val packages = Packages(basePackage)
val sourceApi = SourceApi.create(suppliedApi, apiFragments, baseDir)
val generator = CodeGenerator(packages, sourceApi, codeGenTypes, modelOptions, clientOptions)

MutableSettings.updateSettings(codeGenTypes, modelOptions, clientOptions)
generator.generate().forEach { it.writeFileTo(outputDir.toFile()) }
}
}
1 change: 1 addition & 0 deletions src/main/kotlin/com/cjbooms/fabrikt/cli/CodeGenOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ enum class ClientCodeGenOptionType(private val description: String) {
}

enum class ModelCodeGenOptionType(val description: String) {
X_EXTENSIBLE_ENUMS("This option treats x-extensible-enums as enums"),
JAVA_SERIALIZATION("This option adds Java Serializable interface to the generated models"),
QUARKUS_REFLECTION("This option adds @RegisterForReflection to the generated models. Requires dependency \"'io.quarkus:quarkus-core:+\"");

Expand Down
20 changes: 20 additions & 0 deletions src/main/kotlin/com/cjbooms/fabrikt/generators/MutableSettings.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.cjbooms.fabrikt.generators

import com.cjbooms.fabrikt.cli.ClientCodeGenOptionType
import com.cjbooms.fabrikt.cli.CodeGenerationType
import com.cjbooms.fabrikt.cli.ModelCodeGenOptionType

object MutableSettings {
var generationTypes: MutableSet<CodeGenerationType> = mutableSetOf()
var modelOptions: MutableSet<ModelCodeGenOptionType> = mutableSetOf()
var clientOptions: MutableSet<ClientCodeGenOptionType> = mutableSetOf()
fun updateSettings(
genTypes: Set<CodeGenerationType>,
modelOptions: Set<ModelCodeGenOptionType>,
clientOptions: Set<ClientCodeGenOptionType>
) {
this.generationTypes = genTypes.toMutableSet()
this.modelOptions = modelOptions.toMutableSet()
this.clientOptions = clientOptions.toMutableSet()
}
}
14 changes: 10 additions & 4 deletions src/main/kotlin/com/cjbooms/fabrikt/util/KaizenParserExtensions.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.cjbooms.fabrikt.util

import com.cjbooms.fabrikt.cli.ModelCodeGenOptionType
import com.cjbooms.fabrikt.generators.MutableSettings
import com.cjbooms.fabrikt.model.OasType
import com.cjbooms.fabrikt.model.PropertyInfo
import com.cjbooms.fabrikt.util.NormalisedString.toMapValueClassName
Expand Down Expand Up @@ -54,12 +56,16 @@ object KaizenParserExtensions {
fun Schema.isInlineableMapDefinition() = hasAdditionalProperties() && properties?.isEmpty() == true

fun Schema.isEnumDefinition(): Boolean =
this.type == OasType.Text.type && (this.hasEnums() || extensions.containsKey(EXTENSIBLE_ENUM_KEY))
this.type == OasType.Text.type && (this.hasEnums() ||
(MutableSettings.modelOptions.contains(ModelCodeGenOptionType.X_EXTENSIBLE_ENUMS) &&
extensions.containsKey(EXTENSIBLE_ENUM_KEY)))

@Suppress("UNCHECKED_CAST")
fun Schema.getEnumValues(): List<String> =
if (this.hasEnums()) this.enums.map { it.toString() }
else extensions[EXTENSIBLE_ENUM_KEY]?.let { it as List<String> } ?: emptyList()
fun Schema.getEnumValues(): List<String> = when {
this.hasEnums() -> this.enums.map { it.toString() }
!MutableSettings.modelOptions.contains(ModelCodeGenOptionType.X_EXTENSIBLE_ENUMS) -> emptyList()
else -> extensions[EXTENSIBLE_ENUM_KEY]?.let { it as List<String> } ?: emptyList()
}

fun Schema.hasAdditionalProperties(): Boolean = isObjectType() && Overlay.of(additionalPropertiesSchema).isPresent

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import org.junit.jupiter.params.provider.MethodSource
class ModelGeneratorTest {

private fun testCases(): Stream<String> = Stream.of(
/* "arrays",
"arrays",
"anyOfOneOfAllOf",
"deepNestedSharingReferences",
"defaultValues",
Expand All @@ -30,19 +30,20 @@ class ModelGeneratorTest {
"githubApi",
"inLinedObject",
"mapExamples",
"mixingCamelSnakeLispCase",*/
"oneOfPolymorphicModels"
/* "optionalVsRequired",
"mixingCamelSnakeLispCase",
"oneOfPolymorphicModels",
"optionalVsRequired",
"polymorphicModels",
"requiredReadOnly",
"validationAnnotations",
"wildCardTypes"*/
"wildCardTypes"
)

@ParameterizedTest
@MethodSource("testCases")
fun `correct models are generated for different OpenApi Specifications`(testCaseName: String) {
print("Testcase: $testCaseName")
MutableSettings.modelOptions.add(ModelCodeGenOptionType.X_EXTENSIBLE_ENUMS)
val basePackage = "examples.$testCaseName"
val apiLocation = javaClass.getResource("/examples/$testCaseName/api.yaml")
val sourceApi = SourceApi(apiLocation.readText(), baseDir = Paths.get(apiLocation.toURI()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ components:

EnumDiscriminator:
type: string
x-extensible-enum:
enum:
- obj_one
- obj_two

Expand Down
12 changes: 6 additions & 6 deletions src/test/resources/examples/githubApi/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ components:
in: query
schema:
type: string
x-extensible-enum:
enum:
- active
- inactive
- all
Expand Down Expand Up @@ -1022,7 +1022,7 @@ components:
readOnly: true
status:
type: string
x-extensible-enum:
enum:
- active
- inactive
description: States whether the entity is currently active or inactive
Expand Down Expand Up @@ -1092,7 +1092,7 @@ components:
readOnly: true
status:
type: string
x-extensible-enum:
enum:
- active
- inactive
description: States whether the entity is currently active or inactive
Expand Down Expand Up @@ -1167,7 +1167,7 @@ components:
readOnly: true
status:
type: string
x-extensible-enum:
enum:
- active
- inactive
description: States whether the entity is currently active or inactive
Expand All @@ -1182,7 +1182,7 @@ components:
type: string
visibility:
type: string
x-extensible-enum:
enum:
- Private
- Public
tags:
Expand Down Expand Up @@ -1246,7 +1246,7 @@ components:
readOnly: true
status:
type: string
x-extensible-enum:
enum:
- active
- inactive
description: States whether the entity is currently active or inactive
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/examples/javaSerializableModels/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ components:
readOnly: true
third_attr:
type: "string"
x-extensible-enum:
enum:
- "enum_type_1"
- "enum_type_2"
description: "Enum types for attribute 3"
Expand All @@ -231,7 +231,7 @@ components:
model_type:
type: "string"
description: "The model discrimination type"
x-extensible-enum:
enum:
- "first_model"
- "second_model"
- "third_model"
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/examples/okHttpClient/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ components:
readOnly: true
third_attr:
type: "string"
x-extensible-enum:
enum:
- "enum_type_1"
- "enum_type_2"
description: "Enum types for attribute 3"
Expand All @@ -231,7 +231,7 @@ components:
model_type:
type: "string"
description: "The model discrimination type"
x-extensible-enum:
enum:
- "first_model"
- "second_model"
- "third_model"
Expand Down
Loading

0 comments on commit 33a6aec

Please sign in to comment.