Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support kotlinx serialization #249

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 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,7 @@
package com.cjbooms.fabrikt.generators

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 +34,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 +49,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 +60,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 +101,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 +118,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))
}
}
Loading