Skip to content

Commit

Permalink
Merge pull request #282 from wuseal/3.6.0/jsonschema
Browse files Browse the repository at this point in the history
 Property Nullable Type missing when calling function replaceReferencedClasses() in DataClass.kt
  • Loading branch information
wuseal authored Mar 19, 2020
2 parents c8ef710 + bcf8c38 commit 2b56b7b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -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<Annotation> = listOf(),
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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")
Expand All @@ -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 <T : KotlinClass> applyInterceptors(enabledKotlinClassInterceptors: List<IKotlinClassInterceptor<T>>): KotlinClass {
val newProperties = mutableListOf<Property>()
properties.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = " "
Expand All @@ -27,7 +26,6 @@ data class Property(
}
}
append(keyword).append(" ").append(name).append(": ").append(type)
if(nullable) append("?")
if (value.isNotBlank()) {
append(" = ").append(value)
}
Expand Down
18 changes: 12 additions & 6 deletions src/main/kotlin/wu/seal/jsontokotlin/utils/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fun String.numberOf(subString: String): Int {
private fun JsonArray.onlyHasOneElement(): Boolean {
return size() == 1
}

/**
* array only has object element
*/
Expand All @@ -35,6 +36,7 @@ fun JsonArray.allItemAreNullElement(): Boolean {
}
return true
}

/**
* array only has object element
*/
Expand Down Expand Up @@ -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()
}
}
}

fun String.addIndent(indent: String): String = this.lines().joinToString("\n") { if (it.isBlank()) it else "$indent$it" }
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1530,7 +1530,7 @@ class Issue269Test {
data class Variant(
val id: Int,
val color: String,
val size: Int,
val size: Int?,
val price: Int
)
Expand Down

0 comments on commit 2b56b7b

Please sign in to comment.