Skip to content

Commit 2b56b7b

Browse files
authored
Merge pull request #282 from wuseal/3.6.0/jsonschema
Property Nullable Type missing when calling function replaceReferencedClasses() in DataClass.kt
2 parents c8ef710 + bcf8c38 commit 2b56b7b

File tree

6 files changed

+25
-29
lines changed

6 files changed

+25
-29
lines changed

src/main/kotlin/wu/seal/jsontokotlin/model/classscodestruct/DataClass.kt

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package wu.seal.jsontokotlin.model.classscodestruct
22

33
import wu.seal.jsontokotlin.interceptor.IKotlinClassInterceptor
4-
import wu.seal.jsontokotlin.utils.LogUtil
5-
import wu.seal.jsontokotlin.utils.getCommentCode
6-
import wu.seal.jsontokotlin.utils.getIndent
7-
import wu.seal.jsontokotlin.utils.toAnnotationComments
8-
import java.lang.IllegalStateException
4+
import wu.seal.jsontokotlin.utils.*
95

106
data class DataClass(
117
val annotations: List<Annotation> = listOf(),
@@ -53,7 +49,8 @@ data class DataClass(
5349
else -> it
5450
}
5551
LogUtil.i("replace type: ${property.type} to ${newTypObj.name}")
56-
return@let property.copy(type = newTypObj.name, typeObject = newTypObj)
52+
val typeSuffix = if (property.type.endsWith("?")) "?" else ""
53+
return@let property.copy(type = "${newTypObj.name}$typeSuffix", typeObject = newTypObj)
5754
}
5855
}
5956

@@ -77,15 +74,12 @@ data class DataClass(
7774
append("data class ").append(name).append("(").append("\n")
7875
}
7976
properties.forEachIndexed { index, property ->
80-
val code = property.getCode()
77+
val addIndentCode = property.getCode().addIndent(indent)
8178
val commentCode = getCommentCode(property.comment)
8279
if (fromJsonSchema && commentCode.isNotBlank()) {
83-
append("/**\n * $commentCode\n */\n".split("\n").joinToString("\n") { indent + it })
84-
append(code)
85-
}else{
86-
val addIndentCode = code.split("\n").joinToString("\n") { indent + it }
80+
append(commentCode.toAnnotationComments(indent))
8781
append(addIndentCode)
88-
}
82+
} else append(addIndentCode)
8983
if (index != properties.size - 1) append(",")
9084
if (!fromJsonSchema && commentCode.isNotBlank()) append(" // ").append(commentCode)
9185
append("\n")
@@ -100,14 +94,13 @@ data class DataClass(
10094
append(" {")
10195
append("\n")
10296
val nestedClassesCode = nestedClasses.joinToString("\n\n") { it.getCode() }
103-
append(nestedClassesCode.lines().joinToString("\n") { if (it.isNotBlank()) "$indent$it" else it })
97+
append(nestedClassesCode.addIndent(indent))
10498
append("\n")
10599
append("}")
106100
}
107101
}
108102
}
109103

110-
111104
override fun <T : KotlinClass> applyInterceptors(enabledKotlinClassInterceptors: List<IKotlinClassInterceptor<T>>): KotlinClass {
112105
val newProperties = mutableListOf<Property>()
113106
properties.forEach {

src/main/kotlin/wu/seal/jsontokotlin/model/classscodestruct/Property.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ data class Property(
1010
val value: String = "",
1111
val comment: String = "",
1212
val typeObject: KotlinClass,
13-
private var separatorBetweenAnnotationAndProperty: String = "\n",
14-
val nullable: Boolean = false
13+
private var separatorBetweenAnnotationAndProperty: String = "\n"
1514
) {
1615
fun letLastAnnotationStayInSameLine() {
1716
separatorBetweenAnnotationAndProperty = " "
@@ -27,7 +26,6 @@ data class Property(
2726
}
2827
}
2928
append(keyword).append(" ").append(name).append(": ").append(type)
30-
if(nullable) append("?")
3129
if (value.isNotBlank()) {
3230
append(" = ").append(value)
3331
}

src/main/kotlin/wu/seal/jsontokotlin/utils/Extensions.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fun String.numberOf(subString: String): Int {
2424
private fun JsonArray.onlyHasOneElement(): Boolean {
2525
return size() == 1
2626
}
27+
2728
/**
2829
* array only has object element
2930
*/
@@ -35,6 +36,7 @@ fun JsonArray.allItemAreNullElement(): Boolean {
3536
}
3637
return true
3738
}
39+
3840
/**
3941
* array only has object element
4042
*/
@@ -196,11 +198,15 @@ fun JsonPrimitive.toKotlinClass(): KotlinClass {
196198
/**
197199
* convert string into annotation comments format,TODO need automatic line wrapping
198200
*/
199-
fun String.toAnnotationComments():String{
200-
return if(this.isBlank()) "" else{
201-
StringBuffer().append("/**\n")
202-
.append(" * $this\n")
203-
.append(" */\n")
201+
fun String.toAnnotationComments() = this.toAnnotationComments("")
202+
203+
fun String.toAnnotationComments(indent: String): String {
204+
return if (this.isBlank()) "" else {
205+
StringBuffer().append("$indent/**\n")
206+
.append("$indent * $this\n")
207+
.append("$indent */\n")
204208
.toString()
205209
}
206-
}
210+
}
211+
212+
fun String.addIndent(indent: String): String = this.lines().joinToString("\n") { if (it.isBlank()) it else "$indent$it" }

src/main/kotlin/wu/seal/jsontokotlin/utils/classgenerator/DataClassGeneratorByJSONSchema.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,13 @@ class DataClassGeneratorByJSONSchema(private val rootClassName: String, private
5858
val (jsonClassName, realDef) = getRealDefinition(jsonProp)
5959
resolveTypeClass(realDef.typeString, jsonClassName, realDef, propertyName)
6060
}
61-
val value = if (isRequired) getDefaultValue(typeClass.name) else null
61+
val value = if (isRequired || !jsonProp.isTypeNullable) getDefaultValue(typeClass.name) else null
6262
return Property(
6363
originName = propertyName,
6464
originJsonValue = value,
65-
type = typeClass.name + if (jsonProp.isTypeNullable) "?" else "",
65+
type = typeClass.name,
6666
comment = jsonProp.description ?: "",
67-
typeObject = typeClass,
68-
nullable = jsonProp.isTypeNullable
67+
typeObject = typeClass
6968
)
7069
}
7170

src/test/kotlin/wu/seal/jsontokotlin/KotlinCodeMakerTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ data class TestData(
351351
)
352352
}""".trimIndent()
353353
val dataClass = KotlinClassMaker("TestData", json).makeKotlinClass() as DataClass
354-
dataClass.properties[3].originJsonValue.should.be.`null`
354+
dataClass.properties[3].originJsonValue.should.be.equal("Nested()")
355355
val result = dataClass.getCode()
356356
result.trim().should.be.equal(expected)
357357
}

src/test/kotlin/wu/seal/jsontokotlin/regression/Issue269Test.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,7 @@ class Issue269Test {
15301530
data class Variant(
15311531
val id: Int,
15321532
val color: String,
1533-
val size: Int,
1533+
val size: Int?,
15341534
val price: Int
15351535
)
15361536

0 commit comments

Comments
 (0)