Skip to content

Commit

Permalink
extend PropertyUtils to support multiple json serialization libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
Jens Zettelmeyer committed Oct 29, 2023
1 parent 4dff08a commit 5d84940
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/main/kotlin/com/cjbooms/fabrikt/generators/PropertyUtils.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.cjbooms.fabrikt.generators

import com.cjbooms.fabrikt.generators.model.JacksonMetadata
//import com.cjbooms.fabrikt.generators.model.JacksonMetadata
import com.cjbooms.fabrikt.model.Annotations
import com.cjbooms.fabrikt.model.JacksonAnnotations
import com.cjbooms.fabrikt.model.KotlinTypeInfo
import com.cjbooms.fabrikt.model.PropertyInfo
import com.squareup.kotlinpoet.ClassName
Expand Down Expand Up @@ -33,6 +35,7 @@ object PropertyUtils {
constructorBuilder: FunSpec.Builder,
classSettings: ClassSettings = ClassSettings(ClassSettings.PolymorphyType.NONE, false),
validationAnnotations: ValidationAnnotations = JavaxValidationAnnotations,
annotations: Annotations = JacksonAnnotations,
) {
val wrappedType =
if (classSettings.isMergePatchPattern && !this.isRequired) {
Expand All @@ -47,7 +50,7 @@ object PropertyUtils {

if (this is PropertyInfo.AdditionalProperties) {
property.initializer(name)
property.addAnnotation(JacksonMetadata.ignore)
annotations.addIgnore(property)
val constructorParameter: ParameterSpec.Builder = ParameterSpec.builder(name, wrappedType)
constructorParameter.defaultValue("mutableMapOf()")
constructorBuilder.addParameter(constructorParameter.build())
Expand All @@ -58,20 +61,28 @@ object PropertyUtils {
} else {
parameterizedType
}
classBuilder.addFunction(

val getterSpec = annotations.addGetter(
FunSpec.builder("get")
.returns(Map::class.asTypeName().parameterizedBy(String::class.asTypeName(), value))
.returns(
Map::class.asTypeName().parameterizedBy(String::class.asTypeName(), value)
)
.addStatement("return $name")
.addAnnotation(JacksonMetadata.anyGetter)
.build(),
)

classBuilder.addFunction(
getterSpec.build(),
)

val spec = annotations.addSetter(
FunSpec.builder("set")
.addParameter("name", String::class)
.addParameter("value", value)
.addStatement("$name[name] = value")
.addAnnotation(JacksonMetadata.anySetter)
.build(),
)

classBuilder.addFunction(
spec.build(),
)
} else {
when (classSettings.polymorphyType) {
Expand All @@ -91,15 +102,15 @@ object PropertyUtils {
property.addModifiers(KModifier.OVERRIDE)
classBuilder.addSuperclassConstructorParameter(name)
}
property.addAnnotation(JacksonMetadata.jacksonParameterAnnotation(oasKey))
annotations.addParameter(property, oasKey)
}
property.addAnnotation(JacksonMetadata.jacksonPropertyAnnotation(oasKey))
annotations.addProperty(property, oasKey)
property.addValidationAnnotations(this, validationAnnotations)
}

ClassSettings.PolymorphyType.NONE -> {
property.addAnnotation(JacksonMetadata.jacksonParameterAnnotation(oasKey))
property.addAnnotation(JacksonMetadata.jacksonPropertyAnnotation(oasKey))
annotations.addParameter(property, oasKey)
annotations.addProperty(property, oasKey)
property.addValidationAnnotations(this, validationAnnotations)
}
}
Expand All @@ -108,7 +119,7 @@ object PropertyUtils {
this as PropertyInfo.Field
if (classSettings.polymorphyType == ClassSettings.PolymorphyType.SUB) {
property.initializer(name)
property.addAnnotation(JacksonMetadata.jacksonParameterAnnotation(oasKey))
annotations.addParameter(property, oasKey)
val constructorParameter: ParameterSpec.Builder = ParameterSpec.builder(name, wrappedType)
val discriminators = maybeDiscriminator.getDiscriminatorMappings(schemaName)
when (val discriminator = discriminators.first()) {
Expand Down
16 changes: 16 additions & 0 deletions src/main/kotlin/com/cjbooms/fabrikt/model/Annotations.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.cjbooms.fabrikt.model

import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.PropertySpec

sealed interface Annotations {
fun addIgnore(property: PropertySpec.Builder)

fun addGetter(`fun`: FunSpec.Builder): FunSpec.Builder

fun addSetter(`fun`: FunSpec.Builder): FunSpec.Builder

fun addProperty(property: PropertySpec.Builder, oasKey: String): PropertySpec.Builder

fun addParameter(property: PropertySpec.Builder, oasKey: String): PropertySpec.Builder
}
30 changes: 30 additions & 0 deletions src/main/kotlin/com/cjbooms/fabrikt/model/JacksonAnnotations.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.cjbooms.fabrikt.model

import com.cjbooms.fabrikt.generators.model.JacksonMetadata
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.PropertySpec

object JacksonAnnotations : Annotations {
override fun addIgnore(property: PropertySpec.Builder) {
property.addAnnotation(JacksonMetadata.ignore)
}

override fun addGetter(`fun`: FunSpec.Builder): FunSpec.Builder {
return `fun`.addAnnotation(JacksonMetadata.anyGetter)
}

override fun addSetter(`fun`: FunSpec.Builder): FunSpec.Builder {
return `fun`.addAnnotation(JacksonMetadata.anySetter)
}

override fun addProperty(property: PropertySpec.Builder, oasKey: String): PropertySpec.Builder {
return property.addAnnotation(JacksonMetadata.jacksonPropertyAnnotation(oasKey))
}

override fun addParameter(
property: PropertySpec.Builder,
oasKey: String
): PropertySpec.Builder {
return property.addAnnotation(JacksonMetadata.jacksonParameterAnnotation(oasKey))
}
}

0 comments on commit 5d84940

Please sign in to comment.