Skip to content

Commit 4c3d3e4

Browse files
authored
Merge pull request #261 from wuseal/fix#260
fix #260
2 parents 51245c7 + b40f930 commit 4c3d3e4

File tree

5 files changed

+79
-6
lines changed

5 files changed

+79
-6
lines changed

src/main/kotlin/extensions/chen/biao/KeepAnnotationSupport.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ object KeepAnnotationSupport : Extension() {
3434

3535
val classAnnotation = Annotation.fromAnnotationString(classAnnotationString)
3636

37-
return kotlinDataClass.copy(annotations = listOf(classAnnotation))
37+
val newAnnotations = mutableListOf(classAnnotation).also { it.addAll(kotlinDataClass.annotations) }
38+
39+
return kotlinDataClass.copy(annotations = newAnnotations)
3840
} else {
3941
kotlinDataClass
4042
}

src/main/kotlin/extensions/jose/han/ParcelableAnnotationSupport.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ object ParcelableAnnotationSupport : Extension() {
3535
val classAnnotation1 = Annotation.fromAnnotationString(classAnnotationString1)
3636
val classAnnotation2 = Annotation.fromAnnotationString(classAnnotationString2)
3737

38-
return kotlinDataClass.copy(annotations = listOf(classAnnotation1, classAnnotation2), parentClassTemplate = "Parcelable")
38+
val newAnnotations = mutableListOf(classAnnotation1,classAnnotation2).also { it.addAll(kotlinDataClass.annotations) }
39+
40+
return kotlinDataClass.copy(annotations = newAnnotations, parentClassTemplate = "Parcelable")
3941
} else {
4042
kotlinDataClass
4143
}

src/main/kotlin/extensions/wu/seal/KeepAnnotationSupportForAndroidX.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ object KeepAnnotationSupportForAndroidX : Extension() {
3434

3535
val classAnnotation = Annotation.fromAnnotationString(classAnnotationString)
3636

37-
return kotlinDataClass.copy(annotations = listOf(classAnnotation))
37+
val newAnnotations = mutableListOf(classAnnotation).also { it.addAll(kotlinDataClass.annotations) }
38+
39+
return kotlinDataClass.copy(annotations = newAnnotations)
3840
} else {
3941
kotlinDataClass
4042
}

src/main/kotlin/wu/seal/jsontokotlin/interceptor/InterceptorManager.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ object InterceptorManager {
5050
TargetJsonConverter.Custom -> add(AddCustomAnnotationInterceptor())
5151
TargetJsonConverter.Serilizable -> add(AddSerializableAnnotationInterceptor())
5252
}
53-
if (ConfigManager.enableMinimalAnnotation) {
54-
add(MinimalAnnotationKotlinDataClassInterceptor())
55-
}
5653

5754
if (ConfigManager.parenClassTemplate.isNotBlank()) {
5855
add(ParentClassTemplateKotlinDataClassInterceptor())
@@ -70,6 +67,9 @@ object InterceptorManager {
7067
//add extensions's interceptor
7168
addAll(ExtensionsCollector.extensions)
7269
}.apply {
70+
if (ConfigManager.enableMinimalAnnotation) {
71+
add(MinimalAnnotationKotlinDataClassInterceptor())
72+
}
7373
add(FinalKotlinDataClassWrapperInterceptor())
7474
}
7575
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package wu.seal.jsontokotlin.regression
2+
3+
import com.winterbe.expekt.should
4+
import extensions.chen.biao.KeepAnnotationSupport
5+
import extensions.jose.han.ParcelableAnnotationSupport
6+
import extensions.wu.seal.KeepAnnotationSupportForAndroidX
7+
import org.junit.Test
8+
import wu.seal.jsontokotlin.KotlinCodeMaker
9+
import wu.seal.jsontokotlin.TargetJsonConverter
10+
import wu.seal.jsontokotlin.test.TestConfig
11+
12+
class Issue260Test {
13+
14+
@Test
15+
fun testIssue260() {
16+
TestConfig.setToTestInitState()
17+
TestConfig.targetJsonConvertLib = TargetJsonConverter.MoshiCodeGen
18+
KeepAnnotationSupport.getTestHelper().setConfig("chen.biao.add_keep_annotation_enable",true.toString())
19+
val json="""{"a":"yes"}"""
20+
val expectCode = """
21+
@Keep
22+
@JsonClass(generateAdapter = true)
23+
data class A(
24+
@Json(name = "a")
25+
val a: String = "" // yes
26+
)
27+
""".trimIndent()
28+
val resultCode = KotlinCodeMaker("A", json).makeKotlinData()
29+
resultCode.should.be.equal(expectCode)
30+
}
31+
@Test
32+
fun testIssue260ForAndroidXKeep() {
33+
TestConfig.setToTestInitState()
34+
TestConfig.targetJsonConvertLib = TargetJsonConverter.MoshiCodeGen
35+
KeepAnnotationSupportForAndroidX.getTestHelper().setConfig("wu.seal.add_keep_annotation_enable_androidx",true.toString())
36+
val json="""{"a":"yes"}"""
37+
val expectCode = """
38+
@Keep
39+
@JsonClass(generateAdapter = true)
40+
data class A(
41+
@Json(name = "a")
42+
val a: String = "" // yes
43+
)
44+
""".trimIndent()
45+
val resultCode = KotlinCodeMaker("A", json).makeKotlinData()
46+
resultCode.should.be.equal(expectCode)
47+
}
48+
49+
@Test
50+
fun testIssue260ForParcelableSupport() {
51+
TestConfig.setToTestInitState()
52+
TestConfig.targetJsonConvertLib = TargetJsonConverter.MoshiCodeGen
53+
ParcelableAnnotationSupport.getTestHelper().setConfig("jose.han.add_parcelable_annotatioin_enable",true.toString())
54+
val json="""{"a":"yes"}"""
55+
val expectCode = """
56+
@SuppressLint("ParcelCreator")
57+
@Parcelize
58+
@JsonClass(generateAdapter = true)
59+
data class A(
60+
@Json(name = "a")
61+
val a: String = "" // yes
62+
) : Parcelable
63+
""".trimIndent()
64+
val resultCode = KotlinCodeMaker("A", json).makeKotlinData()
65+
resultCode.should.be.equal(expectCode)
66+
}
67+
}

0 commit comments

Comments
 (0)