Skip to content

Commit

Permalink
Merge pull request #59 from infinum/fix/serial_name_annotation_with_h…
Browse files Browse the repository at this point in the history
…as_one_or_many

Fix: @SerialName annotation with @hasone and @hasmany annotations
  • Loading branch information
DrakslerT authored Jan 12, 2024
2 parents 68a26e1 + 4954cc4 commit 94d193f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.infinum.jsonapix.processor.extensions

import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.TypeName
import javax.lang.model.element.Element
import javax.lang.model.type.MirroredTypeException
import javax.lang.model.type.TypeMirror
Expand All @@ -18,3 +20,11 @@ public inline fun <reified T : Annotation> Element.getAnnotationClassValue(
} catch (e: MirroredTypeException) {
e.typeMirror
}

public fun List<AnnotationSpec>.missingTypeName(typeName: TypeName): Boolean =
indexOfFirst { it.typeName == typeName } < 0

public fun List<AnnotationSpec>.findAnnotationWithTypeName(typeName: TypeName): AnnotationSpec? =
this.firstOrNull { annotationSpec ->
annotationSpec.typeName == typeName
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package com.infinum.jsonapix.processor.specs
import com.infinum.jsonapix.core.common.JsonApiConstants
import com.infinum.jsonapix.core.common.JsonApiConstants.withName
import com.infinum.jsonapix.core.resources.Attributes
import com.squareup.kotlinpoet.AnnotationSpec
import com.infinum.jsonapix.processor.extensions.missingTypeName
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.asClassName
import com.squareup.kotlinpoet.asTypeName
Expand Down Expand Up @@ -61,10 +60,6 @@ internal object AttributesSpecBuilder {
.build()
}

private fun List<AnnotationSpec>.missingTypeName(typeName: TypeName): Boolean {
return indexOfFirst { it.typeName == typeName } < 0
}

private fun fromOriginalObjectSpec(
originalClass: ClassName,
generatedName: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.infinum.jsonapix.core.resources.ManyRelationshipMember
import com.infinum.jsonapix.core.resources.Meta
import com.infinum.jsonapix.core.resources.OneRelationshipMember
import com.infinum.jsonapix.core.resources.Relationships
import com.infinum.jsonapix.processor.extensions.findAnnotationWithTypeName
import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FunSpec
Expand All @@ -19,12 +20,14 @@ import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.asClassName
import com.squareup.kotlinpoet.asTypeName
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient

internal object RelationshipsSpecBuilder {

private val serializableClassName = Serializable::class.asClassName()
private val serialNameTypeName = SerialName::class.asTypeName()

fun build(
className: ClassName,
Expand All @@ -40,7 +43,11 @@ internal object RelationshipsSpecBuilder {
} else {
PropertySpec.builder(it.name, OneRelationshipMember::class)
}
builder.initializer(it.name).build()

builder
.addSerialNameAnnotation(it)
.initializer(it.name)
.build()
}.toMutableList()

properties.addAll(
Expand All @@ -50,7 +57,11 @@ internal object RelationshipsSpecBuilder {
} else {
PropertySpec.builder(it.name, ManyRelationshipMember::class)
}
builder.initializer(it.name).build()

builder
.addSerialNameAnnotation(it)
.initializer(it.name)
.build()
}
)

Expand Down Expand Up @@ -90,6 +101,25 @@ internal object RelationshipsSpecBuilder {
.build()
}

private fun PropertySpec.Builder.addSerialNameAnnotation(originalProperty: PropertySpec): PropertySpec.Builder = this.apply {
val serialNameAnnotation = originalProperty.annotations.findAnnotationWithTypeName(serialNameTypeName)
?: Specs.getSerialNameSpec(originalProperty.name)

addAnnotation(serialNameAnnotation)
}

private fun mapPropertiesToParams(properties: List<PropertySpec>): List<ParameterSpec> {
return properties.map {
ParameterSpec.builder(it.name, it.type)
.apply {
if (it.type.isNullable) {
defaultValue("%L", null)
}
}
.build()
}
}

@SuppressWarnings("SpreadOperator")
private fun fromOriginalObjectSpec(
originalClass: ClassName,
Expand Down Expand Up @@ -224,17 +254,4 @@ internal object RelationshipsSpecBuilder {
member.toString().trim().startsWith("type")
}.toString().split("=")[1].trim()
}

private fun mapPropertiesToParams(properties: List<PropertySpec>): List<ParameterSpec> {
return properties.map {
ParameterSpec.builder(it.name, it.type)
.addAnnotation(Specs.getSerialNameSpec(it.name))
.apply {
if (it.type.isNullable) {
defaultValue("%L", null)
}
}
.build()
}
}
}

0 comments on commit 94d193f

Please sign in to comment.