diff --git a/end2end-tests/models-jackson/build.gradle.kts b/end2end-tests/models-jackson/build.gradle.kts new file mode 100644 index 00000000..83fb8fc3 --- /dev/null +++ b/end2end-tests/models-jackson/build.gradle.kts @@ -0,0 +1,66 @@ +val fabrikt: Configuration by configurations.creating + +val generationDir = "$buildDir/generated" +val apiFile = "$projectDir/openapi/api.yaml" + +sourceSets { + main { java.srcDirs("$generationDir/src/main/kotlin") } + test { java.srcDirs("$generationDir/src/test/kotlin") } +} + +plugins { + id("org.jetbrains.kotlin.jvm") version "1.8.20" // Apply the Kotlin JVM plugin to add support for Kotlin. +} + +java { + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 +} + +val jacksonVersion: String by rootProject.extra +val junitVersion: String by rootProject.extra + +dependencies { + implementation("jakarta.validation:jakarta.validation-api:3.0.2") + implementation("javax.validation:validation-api:2.0.1.Final") + implementation("com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonVersion") + implementation("com.fasterxml.jackson.core:jackson-databind:$jacksonVersion") + implementation("com.fasterxml.jackson.core:jackson-core:$jacksonVersion") + implementation("com.fasterxml.jackson.core:jackson-annotations:$jacksonVersion") + + testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion") + testImplementation("org.junit.jupiter:junit-jupiter-engine:$junitVersion") + testImplementation("org.junit.jupiter:junit-jupiter-params:$junitVersion") + testImplementation("org.assertj:assertj-core:3.24.2") +} + +tasks { + + val generateCode by creating(JavaExec::class) { + inputs.files(apiFile) + outputs.dir(generationDir) + outputs.cacheIf { true } + classpath = rootProject.files("./build/libs/fabrikt-${rootProject.version}.jar") + mainClass.set("com.cjbooms.fabrikt.cli.CodeGen") + args = listOf( + "--output-directory", generationDir, + "--base-package", "com.example", + "--api-file", apiFile, + "--targets", "http_models" + ) + dependsOn(":jar") + dependsOn(":shadowJar") + } + + withType { + kotlinOptions.jvmTarget = "17" + dependsOn(generateCode) + } + + + withType { + useJUnitPlatform() + jvmArgs = listOf("--add-opens=java.base/java.lang=ALL-UNNAMED", "--add-opens=java.base/java.util=ALL-UNNAMED") + + } +} \ No newline at end of file diff --git a/end2end-tests/models-jackson/openapi/api.yaml b/end2end-tests/models-jackson/openapi/api.yaml new file mode 100644 index 00000000..08b3c400 --- /dev/null +++ b/end2end-tests/models-jackson/openapi/api.yaml @@ -0,0 +1,89 @@ +openapi: 3.0.0 +paths: +info: + title: + version: +components: + schemas: + EnumHolder: + type: object + properties: + array_of_enums: + type: array + items: + type: string + x-extensible-enum: + - array_enum_one + - array_enum_two + inlined_enum: + type: string + enum: + - inlined_one + - inlined_two + - inlined_three + inlined_extensible_enum: + type: string + x-extensible-enum: + - inlined_one + - inlined_two + - inlined_three + enum_ref: + $ref: '#/components/schemas/EnumObject' + extensible_enum_ref: + $ref: '#/components/schemas/ExtensibleEnumObject' + list_enums: + $ref: '#/components/schemas/ListEnums' + + EnumObject: + type: string + enum: + - one + - two + - three + - 4 + - -5 + - _6 + + ExtensibleEnumObject: + type: string + x-extensible-enum: + - active + - inactive + + ContentType: + type: string + x-extensible-enum: + - application/json + - application/x.some-type+json + - application/x.some-other-type+json;version=2 + + ListEnums: + type: array + items: + $ref: '#/components/schemas/ContentType' + + FooBars: + type: object + properties: + prop_one: + $ref: '#/components/schemas/Foo' + prop_two: + $ref: '#/components/schemas/Bar' + + Foo: + type: array + items: + type: string + enum: + - X + - Y + Bar: + type: array + items: + type: object + properties: + bar_prop: + type: string + + + diff --git a/end2end-tests/models-jackson/src/test/kotlin/com/cjbooms/fabrikt/models/jackson/EnumTest.kt b/end2end-tests/models-jackson/src/test/kotlin/com/cjbooms/fabrikt/models/jackson/EnumTest.kt new file mode 100644 index 00000000..6729f5c5 --- /dev/null +++ b/end2end-tests/models-jackson/src/test/kotlin/com/cjbooms/fabrikt/models/jackson/EnumTest.kt @@ -0,0 +1,121 @@ +package com.cjbooms.fabrikt.models.jackson + +import com.example.models.EnumHolder +import com.example.models.EnumHolderInlinedEnum +import com.example.models.EnumObject +import com.fasterxml.jackson.module.kotlin.jsonMapper +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class EnumTest { + private val objectMapper = jsonMapper() + private val writer = objectMapper.writerWithDefaultPrettyPrinter() + + @Test + fun `must serialize array of enums`() { + val wrapper = EnumHolder(arrayOfEnums = listOf("a", "b", "c")) + val result = writer.writeValueAsString(wrapper) + + val expected = javaClass.getResource("/enums/array_of_enums.json")!!.readText() + assertThat(result).isEqualTo(expected) + } + + @Test + fun `must deserialize array of enums`() { + val result = readEnumHolder("array_of_enums") + + val expected = EnumHolder(arrayOfEnums = listOf("a", "b", "c")) + assertThat(result).isEqualTo(expected) + } + + @Test + fun `must serialize inlined enum`() { + val wrapper = EnumHolder(inlinedEnum = EnumHolderInlinedEnum.INLINED_ONE) + val result = writer.writeValueAsString(wrapper) + + val expected = javaClass.getResource("/enums/inlined_enum.json")!!.readText() + assertThat(result).isEqualTo(expected) + } + + @Test + fun `must deserialize inlined enums`() { + val result = readEnumHolder("inlined_enum") + + val expected = EnumHolder(inlinedEnum = EnumHolderInlinedEnum.INLINED_ONE) + assertThat(result).isEqualTo(expected) + } + + @Test + fun `must serialize extensible enum`() { + val wrapper = EnumHolder(inlinedExtensibleEnum = "new enum value") + val result = writer.writeValueAsString(wrapper) + + val expected = javaClass.getResource("/enums/extensible_enum.json")!!.readText() + assertThat(result).isEqualTo(expected) + } + + @Test + fun `must deserialize extensible enum`() { + val result = readEnumHolder("extensible_enum") + + val expected = EnumHolder(inlinedExtensibleEnum = "new enum value") + assertThat(result).isEqualTo(expected) + } + + @Test + fun `must serialize enum ref`() { + val wrapper = EnumHolder(enumRef = EnumObject.ONE) + val result = writer.writeValueAsString(wrapper) + + val expected = javaClass.getResource("/enums/enum_ref.json")!!.readText() + assertThat(result).isEqualTo(expected) + } + + @Test + fun `must deserialize enum refs`() { + val result = readEnumHolder("enum_ref") + + val expected = EnumHolder(enumRef = EnumObject.ONE) + assertThat(result).isEqualTo(expected) + } + + @Test + fun `must serialize extensible enum ref`() { + val wrapper = EnumHolder(extensibleEnumRef = "extensible_enum_ref") + val result = writer.writeValueAsString(wrapper) + + val expected = javaClass.getResource("/enums/extensible_enum_ref.json")!!.readText() + assertThat(result).isEqualTo(expected) + } + + @Test + fun `must serialize extensible enum refs`() { + val result = readEnumHolder("extensible_enum_ref") + + val expected = EnumHolder(extensibleEnumRef = "extensible_enum_ref") + assertThat(result).isEqualTo(expected) + } + + @Test + fun `must serialize a list of enum`() { + val wrapper = EnumHolder(listEnums = listOf("list enum value 1")) + val result = writer.writeValueAsString(wrapper) + + val expected = javaClass.getResource("/enums/list_of_enums.json")!!.readText() + assertThat(result).isEqualTo(expected) + } + + @Test + fun `must deserialize a list of enum`() { + val result = readEnumHolder("list_of_enums") + + val expected = EnumHolder(listEnums = listOf("list enum value 1")) + assertThat(result).isEqualTo(expected) + } + + private fun readEnumHolder(name: String): EnumHolder { + val jsonString = javaClass.getResource("/enums/$name.json")!!.readText() + + return objectMapper.readValue(jsonString, EnumHolder::class.java) + } +} \ No newline at end of file diff --git a/end2end-tests/models-jackson/src/test/resources/enums/array_of_enums.json b/end2end-tests/models-jackson/src/test/resources/enums/array_of_enums.json new file mode 100644 index 00000000..47ed164a --- /dev/null +++ b/end2end-tests/models-jackson/src/test/resources/enums/array_of_enums.json @@ -0,0 +1,8 @@ +{ + "array_of_enums" : [ "a", "b", "c" ], + "inlined_enum" : null, + "inlined_extensible_enum" : null, + "enum_ref" : null, + "extensible_enum_ref" : null, + "list_enums" : null +} \ No newline at end of file diff --git a/end2end-tests/models-jackson/src/test/resources/enums/enum_ref.json b/end2end-tests/models-jackson/src/test/resources/enums/enum_ref.json new file mode 100644 index 00000000..5355d85b --- /dev/null +++ b/end2end-tests/models-jackson/src/test/resources/enums/enum_ref.json @@ -0,0 +1,8 @@ +{ + "array_of_enums" : null, + "inlined_enum" : null, + "inlined_extensible_enum" : null, + "enum_ref" : "one", + "extensible_enum_ref" : null, + "list_enums" : null +} \ No newline at end of file diff --git a/end2end-tests/models-jackson/src/test/resources/enums/extensible_enum.json b/end2end-tests/models-jackson/src/test/resources/enums/extensible_enum.json new file mode 100644 index 00000000..2ca1a1a8 --- /dev/null +++ b/end2end-tests/models-jackson/src/test/resources/enums/extensible_enum.json @@ -0,0 +1,8 @@ +{ + "array_of_enums" : null, + "inlined_enum" : null, + "inlined_extensible_enum" : "new enum value", + "enum_ref" : null, + "extensible_enum_ref" : null, + "list_enums" : null +} \ No newline at end of file diff --git a/end2end-tests/models-jackson/src/test/resources/enums/extensible_enum_ref.json b/end2end-tests/models-jackson/src/test/resources/enums/extensible_enum_ref.json new file mode 100644 index 00000000..64e54425 --- /dev/null +++ b/end2end-tests/models-jackson/src/test/resources/enums/extensible_enum_ref.json @@ -0,0 +1,8 @@ +{ + "array_of_enums" : null, + "inlined_enum" : null, + "inlined_extensible_enum" : null, + "enum_ref" : null, + "extensible_enum_ref" : "extensible_enum_ref", + "list_enums" : null +} \ No newline at end of file diff --git a/end2end-tests/models-jackson/src/test/resources/enums/inlined_enum.json b/end2end-tests/models-jackson/src/test/resources/enums/inlined_enum.json new file mode 100644 index 00000000..a58e21cd --- /dev/null +++ b/end2end-tests/models-jackson/src/test/resources/enums/inlined_enum.json @@ -0,0 +1,8 @@ +{ + "array_of_enums" : null, + "inlined_enum" : "inlined_one", + "inlined_extensible_enum" : null, + "enum_ref" : null, + "extensible_enum_ref" : null, + "list_enums" : null +} \ No newline at end of file diff --git a/end2end-tests/models-jackson/src/test/resources/enums/list_of_enums.json b/end2end-tests/models-jackson/src/test/resources/enums/list_of_enums.json new file mode 100644 index 00000000..916de5d7 --- /dev/null +++ b/end2end-tests/models-jackson/src/test/resources/enums/list_of_enums.json @@ -0,0 +1,8 @@ +{ + "array_of_enums" : null, + "inlined_enum" : null, + "inlined_extensible_enum" : null, + "enum_ref" : null, + "extensible_enum_ref" : null, + "list_enums" : [ "list enum value 1" ] +} \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index d399612a..09a4277e 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -3,4 +3,5 @@ rootProject.name = "fabrikt" include( "end2end-tests:okhttp", "end2end-tests:ktor", + "end2end-tests:models-jackson", )