diff --git a/src/main/kotlin/wu/seal/jsontokotlin/model/classscodestruct/DataClass.kt b/src/main/kotlin/wu/seal/jsontokotlin/model/classscodestruct/DataClass.kt index c2d0e9fb..3b612845 100755 --- a/src/main/kotlin/wu/seal/jsontokotlin/model/classscodestruct/DataClass.kt +++ b/src/main/kotlin/wu/seal/jsontokotlin/model/classscodestruct/DataClass.kt @@ -1,11 +1,7 @@ package wu.seal.jsontokotlin.model.classscodestruct import wu.seal.jsontokotlin.interceptor.IKotlinClassInterceptor -import wu.seal.jsontokotlin.utils.LogUtil -import wu.seal.jsontokotlin.utils.getCommentCode -import wu.seal.jsontokotlin.utils.getIndent -import wu.seal.jsontokotlin.utils.toAnnotationComments -import java.lang.IllegalStateException +import wu.seal.jsontokotlin.utils.* data class DataClass( val annotations: List = listOf(), @@ -53,7 +49,8 @@ data class DataClass( else -> it } LogUtil.i("replace type: ${property.type} to ${newTypObj.name}") - return@let property.copy(type = newTypObj.name, typeObject = newTypObj) + val typeSuffix = if (property.type.endsWith("?")) "?" else "" + return@let property.copy(type = "${newTypObj.name}$typeSuffix", typeObject = newTypObj) } } @@ -77,15 +74,12 @@ data class DataClass( append("data class ").append(name).append("(").append("\n") } properties.forEachIndexed { index, property -> - val code = property.getCode() + val addIndentCode = property.getCode().addIndent(indent) val commentCode = getCommentCode(property.comment) if (fromJsonSchema && commentCode.isNotBlank()) { - append("/**\n * $commentCode\n */\n".split("\n").joinToString("\n") { indent + it }) - append(code) - }else{ - val addIndentCode = code.split("\n").joinToString("\n") { indent + it } + append(commentCode.toAnnotationComments(indent)) append(addIndentCode) - } + } else append(addIndentCode) if (index != properties.size - 1) append(",") if (!fromJsonSchema && commentCode.isNotBlank()) append(" // ").append(commentCode) append("\n") @@ -100,14 +94,13 @@ data class DataClass( append(" {") append("\n") val nestedClassesCode = nestedClasses.joinToString("\n\n") { it.getCode() } - append(nestedClassesCode.lines().joinToString("\n") { if (it.isNotBlank()) "$indent$it" else it }) + append(nestedClassesCode.addIndent(indent)) append("\n") append("}") } } } - override fun applyInterceptors(enabledKotlinClassInterceptors: List>): KotlinClass { val newProperties = mutableListOf() properties.forEach { diff --git a/src/main/kotlin/wu/seal/jsontokotlin/model/classscodestruct/Property.kt b/src/main/kotlin/wu/seal/jsontokotlin/model/classscodestruct/Property.kt index 03853507..034808a3 100755 --- a/src/main/kotlin/wu/seal/jsontokotlin/model/classscodestruct/Property.kt +++ b/src/main/kotlin/wu/seal/jsontokotlin/model/classscodestruct/Property.kt @@ -10,8 +10,7 @@ data class Property( val value: String = "", val comment: String = "", val typeObject: KotlinClass, - private var separatorBetweenAnnotationAndProperty: String = "\n", - val nullable: Boolean = false + private var separatorBetweenAnnotationAndProperty: String = "\n" ) { fun letLastAnnotationStayInSameLine() { separatorBetweenAnnotationAndProperty = " " @@ -27,7 +26,6 @@ data class Property( } } append(keyword).append(" ").append(name).append(": ").append(type) - if(nullable) append("?") if (value.isNotBlank()) { append(" = ").append(value) } diff --git a/src/main/kotlin/wu/seal/jsontokotlin/utils/Extensions.kt b/src/main/kotlin/wu/seal/jsontokotlin/utils/Extensions.kt index 1a064a25..bf3f03a8 100755 --- a/src/main/kotlin/wu/seal/jsontokotlin/utils/Extensions.kt +++ b/src/main/kotlin/wu/seal/jsontokotlin/utils/Extensions.kt @@ -24,6 +24,7 @@ fun String.numberOf(subString: String): Int { private fun JsonArray.onlyHasOneElement(): Boolean { return size() == 1 } + /** * array only has object element */ @@ -35,6 +36,7 @@ fun JsonArray.allItemAreNullElement(): Boolean { } return true } + /** * array only has object element */ @@ -196,11 +198,15 @@ fun JsonPrimitive.toKotlinClass(): KotlinClass { /** * convert string into annotation comments format,TODO need automatic line wrapping */ -fun String.toAnnotationComments():String{ - return if(this.isBlank()) "" else{ - StringBuffer().append("/**\n") - .append(" * $this\n") - .append(" */\n") +fun String.toAnnotationComments() = this.toAnnotationComments("") + +fun String.toAnnotationComments(indent: String): String { + return if (this.isBlank()) "" else { + StringBuffer().append("$indent/**\n") + .append("$indent * $this\n") + .append("$indent */\n") .toString() } -} \ No newline at end of file +} + +fun String.addIndent(indent: String): String = this.lines().joinToString("\n") { if (it.isBlank()) it else "$indent$it" } \ No newline at end of file diff --git a/src/main/kotlin/wu/seal/jsontokotlin/utils/classgenerator/DataClassGeneratorByJSONSchema.kt b/src/main/kotlin/wu/seal/jsontokotlin/utils/classgenerator/DataClassGeneratorByJSONSchema.kt index 7b828bdf..bc2021c2 100755 --- a/src/main/kotlin/wu/seal/jsontokotlin/utils/classgenerator/DataClassGeneratorByJSONSchema.kt +++ b/src/main/kotlin/wu/seal/jsontokotlin/utils/classgenerator/DataClassGeneratorByJSONSchema.kt @@ -58,14 +58,13 @@ class DataClassGeneratorByJSONSchema(private val rootClassName: String, private val (jsonClassName, realDef) = getRealDefinition(jsonProp) resolveTypeClass(realDef.typeString, jsonClassName, realDef, propertyName) } - val value = if (isRequired) getDefaultValue(typeClass.name) else null + val value = if (isRequired || !jsonProp.isTypeNullable) getDefaultValue(typeClass.name) else null return Property( originName = propertyName, originJsonValue = value, - type = typeClass.name + if (jsonProp.isTypeNullable) "?" else "", + type = typeClass.name, comment = jsonProp.description ?: "", - typeObject = typeClass, - nullable = jsonProp.isTypeNullable + typeObject = typeClass ) } diff --git a/src/test/kotlin/wu/seal/jsontokotlin/KotlinCodeMakerTest.kt b/src/test/kotlin/wu/seal/jsontokotlin/KotlinCodeMakerTest.kt index bb6cf776..1945eeba 100755 --- a/src/test/kotlin/wu/seal/jsontokotlin/KotlinCodeMakerTest.kt +++ b/src/test/kotlin/wu/seal/jsontokotlin/KotlinCodeMakerTest.kt @@ -351,7 +351,7 @@ data class TestData( ) }""".trimIndent() val dataClass = KotlinClassMaker("TestData", json).makeKotlinClass() as DataClass - dataClass.properties[3].originJsonValue.should.be.`null` + dataClass.properties[3].originJsonValue.should.be.equal("Nested()") val result = dataClass.getCode() result.trim().should.be.equal(expected) } diff --git a/src/test/kotlin/wu/seal/jsontokotlin/regression/Issue269Test.kt b/src/test/kotlin/wu/seal/jsontokotlin/regression/Issue269Test.kt index c971fa0c..26241ee5 100644 --- a/src/test/kotlin/wu/seal/jsontokotlin/regression/Issue269Test.kt +++ b/src/test/kotlin/wu/seal/jsontokotlin/regression/Issue269Test.kt @@ -1530,7 +1530,7 @@ class Issue269Test { data class Variant( val id: Int, val color: String, - val size: Int, + val size: Int?, val price: Int )