Skip to content

Commit 575cfd9

Browse files
authored
Merge pull request #1473 from WebFuzzing/phg/dto-handle-null
Handle null in DTO
2 parents 59f189a + dd65f80 commit 575cfd9

4 files changed

Lines changed: 29 additions & 7 deletions

File tree

core-tests/e2e-tests/spring/spring-rest-bb/src/test/kotlin/org/evomaster/e2etests/spring/rest/bb/dtonull/BBDtoNullEMTest.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ class BBDtoNullEMTest : SpringTestBase() {
4141

4242
setOption(args, "bbSwaggerUrl", "$baseUrlOfSut/openapi-dtonull.json")
4343

44-
//TODO need to fix/extend DTO handling before activating it here
45-
// setOption(args, "dtoForRequestPayload", "true")
46-
setOption(args, "dtoForRequestPayload", "false")
44+
setOption(args, "dtoForRequestPayload", "true")
4745

4846
val solution = initAndRun(args)
4947

core-tests/e2e-tests/spring/spring-rest-openapi-v3/src/test/kotlin/org/evomaster/e2etests/spring/openapi/v3/dtoreflectiveassert/DtoReflectiveAssertEMTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.evomaster.e2etests.spring.openapi.v3.SpringTestBase
77
import org.junit.jupiter.api.Assertions
88
import org.junit.jupiter.api.BeforeAll
99
import org.junit.jupiter.api.Test
10+
import java.util.Optional.ofNullable
1011
import kotlin.reflect.KClass
1112
import kotlin.reflect.KMutableProperty1
1213
import kotlin.reflect.full.createInstance
@@ -174,9 +175,9 @@ class DtoReflectiveAssertEMTest: SpringTestBase() {
174175

175176
property?.let {
176177
it.isAccessible = true
177-
it.set(instance, propertyValue) // Set the value
178+
it.set(instance, ofNullable(propertyValue)) // Set the value
178179
}
179-
Assertions.assertEquals(propertyValue, property?.get(instance))
180+
Assertions.assertEquals(ofNullable(propertyValue), property?.get(instance))
180181
}
181182

182183
private fun assertAdditionalPropertiesFunction(klass: KClass<out Any>, instance: Any, propertyName: String, propertyValue: Any?) {

core/src/main/kotlin/org/evomaster/core/output/dto/GeneToDto.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.evomaster.core.search.gene.string.Base64StringGene
2222
import org.evomaster.core.search.gene.string.StringGene
2323
import org.evomaster.core.search.gene.utils.GeneUtils.isInactiveOptionalGene
2424
import org.evomaster.core.search.gene.wrapper.ChoiceGene
25+
import org.evomaster.core.search.gene.wrapper.NullableGene
2526
import org.evomaster.core.search.gene.wrapper.OptionalGene
2627
import org.evomaster.core.utils.StringUtils
2728
import org.slf4j.Logger
@@ -120,6 +121,8 @@ class GeneToDto(
120121
val children = parent.getViewOfChildren()
121122
val otherChoice = children.find { child -> child != leafGene }
122123
result.add(dtoOutput.getSetterStatement(dtoVarName, attributeName, "${leafGene.getValueAsPrintableString(targetFormat = outputFormat)}${getValueSuffix(otherChoice)}"))
124+
} else if (parent is NullableGene) {
125+
result.add(dtoOutput.getSetterStatement(dtoVarName, attributeName, "${parent.getValueAsPrintableString(targetFormat = outputFormat)}${getValueSuffix(leafGene)}"))
123126
} else {
124127
result.add(dtoOutput.getSetterStatement(dtoVarName, attributeName, "${leafGene.getValueAsPrintableString(targetFormat = outputFormat)}${getValueSuffix(leafGene)}"))
125128
}

core/src/main/kotlin/org/evomaster/core/output/dto/KotlinDtoOutput.kt

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.evomaster.core.output.dto
33
import org.evomaster.core.output.Lines
44
import org.evomaster.core.output.OutputFormat
55
import org.evomaster.core.output.TestSuiteFileName
6+
import org.evomaster.core.utils.StringUtils.capitalizeFirstChar
67
import java.nio.file.Path
78

89
class KotlinDtoOutput: JvmDtoOutput() {
@@ -21,7 +22,7 @@ class KotlinDtoOutput: JvmDtoOutput() {
2122
}
2223

2324
override fun getSetterStatement(dtoVarName: String, attributeName: String, value: String): String {
24-
return "$dtoVarName.${attributeName} = $value"
25+
return "$dtoVarName.set${capitalizeFirstChar(attributeName)}($value)"
2526
}
2627

2728
override fun getNewListStatement(listType: String, listVarName: String): String {
@@ -40,14 +41,15 @@ class KotlinDtoOutput: JvmDtoOutput() {
4041
lines.add("@JsonInclude(JsonInclude.Include.NON_NULL)")
4142
lines.add("class $dtoFilename {")
4243
addVariables(lines, dtoClass)
44+
addGettersAndSetters(lines, dtoClass)
4345
lines.add("}")
4446
}
4547

4648
private fun addVariables(lines: Lines, dtoClass: DtoClass) {
4749
dtoClass.fieldsMap.forEach {
4850
lines.indented {
4951
lines.add("@JsonProperty(\"${it.key}\")")
50-
lines.add("var ${it.key}: ${it.value.type}? = null")
52+
lines.add("private var ${it.key}: Optional<${it.value.type}>? = null")
5153
}
5254
lines.addEmpty()
5355
}
@@ -83,4 +85,22 @@ class KotlinDtoOutput: JvmDtoOutput() {
8385
}
8486
}
8587

88+
private fun addGettersAndSetters(lines: Lines, dtoClass: DtoClass) {
89+
dtoClass.fieldsMap.forEach {
90+
val varName = it.key
91+
val varType = it.value.type
92+
val capitalizedVarName = capitalizeFirstChar(varName)
93+
lines.indented {
94+
lines.add("fun get${capitalizedVarName}(): Optional<${varType}>? = $varName")
95+
lines.addEmpty()
96+
lines.add("fun set${capitalizedVarName}(${varName}: ${varType}?) {")
97+
lines.indented {
98+
lines.add("this.${varName} = Optional.ofNullable(${varName})")
99+
}
100+
lines.add("}")
101+
}
102+
lines.addEmpty()
103+
}
104+
}
105+
86106
}

0 commit comments

Comments
 (0)