Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Commit 036599a

Browse files
committed
#78, write data type with implements
1 parent 633f8b7 commit 036599a

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

src/main/kotlin/io/openapiprocessor/core/model/datatypes/ObjectDataType.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ open class ObjectDataType(
7272

7373
private val implementsImports: Set<String>
7474
get() {
75-
return implementsDataType?.referencedImports ?: emptySet()
75+
return implementsDataType?.getImports() ?: emptySet()
7676
}
7777
}

src/main/kotlin/io/openapiprocessor/core/writer/java/DataTypeWriter.kt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ class DataTypeWriter(
5353
}
5454
}
5555

56-
target.write("public class ${dataType.getTypeName()} {\n\n")
56+
val implements: DataType? = dataType.implementsDataType
57+
if (implements != null) {
58+
writeClassImplementsHeader(target, dataType, implements)
59+
} else {
60+
writeClassHeader(target, dataType)
61+
}
5762

5863
dataType.forEach { propName, propDataType ->
5964
val javaPropertyName = toCamelCase(propName)
@@ -70,6 +75,21 @@ class DataTypeWriter(
7075
target.write ("}\n")
7176
}
7277

78+
private fun writeClassImplementsHeader(
79+
target: Writer,
80+
dataType: ModelDataType,
81+
implements: DataType
82+
) {
83+
target.write("public class ${dataType.getTypeName()} implements ${implements.getTypeName()} {\n\n")
84+
}
85+
86+
private fun writeClassHeader(
87+
target: Writer,
88+
dataType: ModelDataType
89+
) {
90+
target.write("public class ${dataType.getTypeName()} {\n\n")
91+
}
92+
7393
private fun getProp(
7494
propertyName: String,
7595
javaPropertyName: String,

src/test/kotlin/io/openapiprocessor/core/writer/java/DataTypeWriterSpec.kt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@ import io.kotest.matchers.string.shouldContain
1111
import io.mockk.mockk
1212
import io.openapiprocessor.core.converter.ApiOptions
1313
import io.openapiprocessor.core.extractImports
14-
import io.openapiprocessor.core.model.datatypes.DataTypeConstraints
15-
import io.openapiprocessor.core.model.datatypes.ModelDataType
16-
import io.openapiprocessor.core.model.datatypes.PropertyDataType
14+
import io.openapiprocessor.core.model.datatypes.*
1715
import io.openapiprocessor.core.support.datatypes.ObjectDataType
18-
import io.openapiprocessor.core.model.datatypes.StringDataType
1916
import io.openapiprocessor.core.support.datatypes.ListDataType
2017
import io.openapiprocessor.core.support.datatypes.propertyDataType
2118
import io.openapiprocessor.core.support.datatypes.propertyDataTypeString
@@ -143,4 +140,22 @@ class DataTypeWriterSpec: StringSpec({
143140
""".trimMargin()
144141
}
145142

143+
"writes class with implements" {
144+
options.oneOfInterface = true
145+
146+
val dataType = ObjectDataType ("Foo", "pkg", linkedMapOf(
147+
"foo" to PropertyDataType (false, false, StringDataType ())
148+
))
149+
150+
val ifDataType = InterfaceDataType(
151+
DataTypeName("MarkerInterface"), "pkg", listOf(dataType))
152+
153+
dataType.implementsDataType = ifDataType
154+
155+
// when:
156+
writer.write (target, dataType)
157+
158+
target.toString() shouldContain ("public class Foo implements MarkerInterface {")
159+
}
160+
146161
})

0 commit comments

Comments
 (0)