-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move serialization annotations to separate objects
and add basic support for Kotlinx Serialization
- Loading branch information
1 parent
3b9be5a
commit 11e46f4
Showing
7 changed files
with
157 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/kotlin/com/cjbooms/fabrikt/model/JacksonAnnotations.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.cjbooms.fabrikt.model | ||
|
||
import com.cjbooms.fabrikt.generators.model.JacksonMetadata | ||
import com.cjbooms.fabrikt.generators.model.JacksonMetadata.basePolymorphicType | ||
import com.cjbooms.fabrikt.generators.model.JacksonMetadata.polymorphicSubTypes | ||
import com.squareup.kotlinpoet.FunSpec | ||
import com.squareup.kotlinpoet.PropertySpec | ||
import com.squareup.kotlinpoet.TypeName | ||
import com.squareup.kotlinpoet.TypeSpec | ||
|
||
object JacksonAnnotations : SerializationAnnotations { | ||
override val supportsAdditionalProperties = true | ||
|
||
override fun addIgnore(propertySpecBuilder: PropertySpec.Builder): PropertySpec.Builder = | ||
propertySpecBuilder.addAnnotation(JacksonMetadata.ignore) | ||
|
||
override fun addGetter(funSpecBuilder: FunSpec.Builder): FunSpec.Builder = | ||
funSpecBuilder.addAnnotation(JacksonMetadata.anyGetter) | ||
|
||
override fun addSetter(funSpecBuilder: FunSpec.Builder): FunSpec.Builder = | ||
funSpecBuilder.addAnnotation(JacksonMetadata.anySetter) | ||
|
||
override fun addProperty(propertySpecBuilder: PropertySpec.Builder, oasKey: String): PropertySpec.Builder = | ||
propertySpecBuilder.addAnnotation(JacksonMetadata.jacksonPropertyAnnotation(oasKey)) | ||
|
||
override fun addParameter(propertySpecBuilder: PropertySpec.Builder, oasKey: String): PropertySpec.Builder = | ||
propertySpecBuilder.addAnnotation(JacksonMetadata.jacksonParameterAnnotation(oasKey)) | ||
|
||
override fun addClassAnnotation(typeSpecBuilder: TypeSpec.Builder): TypeSpec.Builder = | ||
typeSpecBuilder | ||
|
||
override fun addBasePolymorphicTypeAnnotation(typeSpecBuilder: TypeSpec.Builder, propertyName: String) = | ||
typeSpecBuilder.addAnnotation(basePolymorphicType(propertyName)) | ||
|
||
override fun addPolymorphicSubTypesAnnotation(typeSpecBuilder: TypeSpec.Builder, mappings: Map<String, TypeName>) = | ||
typeSpecBuilder.addAnnotation(polymorphicSubTypes(mappings, enumDiscriminator = null)) | ||
|
||
override fun addSubtypeMappingAnnotation(typeSpecBuilder: TypeSpec.Builder, mapping: String): TypeSpec.Builder = | ||
typeSpecBuilder | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/com/cjbooms/fabrikt/model/KotlinxSerializationAnnotations.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.cjbooms.fabrikt.model | ||
|
||
import com.squareup.kotlinpoet.AnnotationSpec | ||
import com.squareup.kotlinpoet.FunSpec | ||
import com.squareup.kotlinpoet.PropertySpec | ||
import com.squareup.kotlinpoet.TypeName | ||
import com.squareup.kotlinpoet.TypeSpec | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
object KotlinxSerializationAnnotations : SerializationAnnotations { | ||
/** | ||
* Supporting "additionalProperties: true" for kotlinx serialization requires additional | ||
* research and work due to Any type in the map (val properties: MutableMap<String, Any?>) | ||
* | ||
* Currently, the generated code does not support additional properties. | ||
*/ | ||
override val supportsAdditionalProperties = false | ||
|
||
override fun addIgnore(propertySpecBuilder: PropertySpec.Builder) = | ||
propertySpecBuilder // not applicable | ||
|
||
override fun addGetter(funSpecBuilder: FunSpec.Builder) = | ||
funSpecBuilder // not applicable | ||
|
||
override fun addSetter(funSpecBuilder: FunSpec.Builder) = | ||
funSpecBuilder // not applicable | ||
|
||
override fun addProperty(propertySpecBuilder: PropertySpec.Builder, oasKey: String) = | ||
propertySpecBuilder.addAnnotation(AnnotationSpec.builder(SerialName::class).addMember("%S", oasKey).build()) | ||
|
||
override fun addParameter(propertySpecBuilder: PropertySpec.Builder, oasKey: String) = | ||
propertySpecBuilder // not applicable | ||
|
||
override fun addClassAnnotation(typeSpecBuilder: TypeSpec.Builder) = | ||
typeSpecBuilder.addAnnotation(AnnotationSpec.builder(Serializable::class).build()) | ||
|
||
override fun addBasePolymorphicTypeAnnotation(typeSpecBuilder: TypeSpec.Builder, propertyName: String) = | ||
typeSpecBuilder // not applicable | ||
|
||
override fun addPolymorphicSubTypesAnnotation(typeSpecBuilder: TypeSpec.Builder, mappings: Map<String, TypeName>) = | ||
typeSpecBuilder // not applicable | ||
|
||
override fun addSubtypeMappingAnnotation(typeSpecBuilder: TypeSpec.Builder, mapping: String): TypeSpec.Builder { | ||
return typeSpecBuilder.addAnnotation(AnnotationSpec.builder(SerialName::class).addMember("%S", mapping).build()) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/com/cjbooms/fabrikt/model/SerializationAnnotations.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.cjbooms.fabrikt.model | ||
|
||
import com.squareup.kotlinpoet.FunSpec | ||
import com.squareup.kotlinpoet.PropertySpec | ||
import com.squareup.kotlinpoet.TypeName | ||
import com.squareup.kotlinpoet.TypeSpec | ||
|
||
sealed interface SerializationAnnotations { | ||
/** | ||
* Whether the annotation supports OpenAPI's additional properties | ||
* https://spec.openapis.org/oas/v3.0.0.html#model-with-map-dictionary-properties | ||
*/ | ||
val supportsAdditionalProperties: Boolean | ||
|
||
fun addIgnore(propertySpecBuilder: PropertySpec.Builder): PropertySpec.Builder | ||
fun addGetter(funSpecBuilder: FunSpec.Builder): FunSpec.Builder | ||
fun addSetter(funSpecBuilder: FunSpec.Builder): FunSpec.Builder | ||
fun addProperty(propertySpecBuilder: PropertySpec.Builder, oasKey: String): PropertySpec.Builder | ||
fun addParameter(propertySpecBuilder: PropertySpec.Builder, oasKey: String): PropertySpec.Builder | ||
fun addClassAnnotation(typeSpecBuilder: TypeSpec.Builder): TypeSpec.Builder | ||
fun addBasePolymorphicTypeAnnotation(typeSpecBuilder: TypeSpec.Builder, propertyName: String): TypeSpec.Builder | ||
fun addPolymorphicSubTypesAnnotation(typeSpecBuilder: TypeSpec.Builder, mappings: Map<String, TypeName>): TypeSpec.Builder | ||
fun addSubtypeMappingAnnotation(typeSpecBuilder: TypeSpec.Builder, mapping: String): TypeSpec.Builder | ||
} |