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

Commit 24530ca

Browse files
committed
handle id/type split (object writer), #65
1 parent f8b222a commit 24530ca

File tree

4 files changed

+133
-22
lines changed

4 files changed

+133
-22
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
@@ -33,7 +33,7 @@ open class ObjectDataType(
3333
}
3434

3535
override fun getImports(): Set<String> {
36-
return setOf("${getPackageName()}.${getName()}")
36+
return setOf("${getPackageName()}.${getTypeName()}")
3737
}
3838

3939
override val referencedImports: Set<String>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class DataTypeWriter(
4242
target.write(javadocWriter.convert(dataType))
4343
}
4444

45-
target.write("public class ${dataType.getName()} {\n\n")
45+
target.write("public class ${dataType.getTypeName()} {\n\n")
4646

4747
val properties = dataType.getProperties()
4848
properties.forEach { (propName, propDataType) ->
@@ -74,7 +74,7 @@ class DataTypeWriter(
7474
result += " @Deprecated\n"
7575
}
7676

77-
var propTypeName = propDataType.getName()
77+
var propTypeName = propDataType.getTypeName()
7878
if(apiOptions.beanValidation) {
7979
val info = validationAnnotations.validate(propDataType, required)
8080
if (info.hasAnnotations) {
@@ -102,7 +102,7 @@ class DataTypeWriter(
102102
}
103103

104104
result += """
105-
| public ${propDataType.getName()} get${propertyName.capitalize()}() {
105+
| public ${propDataType.getTypeName()} get${propertyName.capitalize()}() {
106106
| return ${propertyName};
107107
| }
108108
|
@@ -119,7 +119,7 @@ class DataTypeWriter(
119119
}
120120

121121
result += """
122-
| public void set${propertyName.capitalize()}(${propDataType.getName()} ${propertyName}) {
122+
| public void set${propertyName.capitalize()}(${propDataType.getTypeName()} ${propertyName}) {
123123
| this.${propertyName} = ${propertyName};
124124
| }
125125
|

src/test/groovy/com/github/hauner/openapi/core/writer/java/DataTypeWriterSpec.groovy

Lines changed: 106 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package com.github.hauner.openapi.core.writer.java
77

88
import io.openapiprocessor.core.converter.ApiOptions
9+
import io.openapiprocessor.core.model.datatypes.DataTypeName
10+
import io.openapiprocessor.core.model.datatypes.ObjectDataType as ObjectDataTypeP
911
import io.openapiprocessor.core.model.datatypes.StringDataType
1012
import io.openapiprocessor.core.support.datatypes.ListDataType
1113
import io.openapiprocessor.core.support.datatypes.ObjectDataType
@@ -15,6 +17,7 @@ import io.openapiprocessor.core.writer.java.JavaDocWriter
1517
import io.openapiprocessor.core.writer.java.SimpleWriter
1618
import spock.lang.Specification
1719

20+
import static io.openapiprocessor.core.AssertKt.extractBody
1821
import static io.openapiprocessor.core.AssertKt.extractImports
1922

2023
class DataTypeWriterSpec extends Specification {
@@ -36,7 +39,7 @@ class DataTypeWriterSpec extends Specification {
3639
}
3740

3841
void "writes 'package'" () {
39-
def pkg = 'com.github.hauner.openapi'
42+
def pkg = 'io.openapiprocessor.test'
4043
def dataType = new ObjectDataType (
4144
'Book', pkg, [:], null, false, null)
4245

@@ -54,18 +57,21 @@ package $pkg;
5457
def pkg = 'external'
5558

5659
def dataType = new ObjectDataType ('Book', 'mine', [
57-
'isbn': new ObjectDataType (
58-
'Isbn', pkg, [:], null, false, null)
60+
'isbn': new ObjectDataTypeP (new DataTypeName(id, type), pkg, [:],
61+
null, false, null)
5962
], null, false, null)
6063

6164
when:
6265
writer.write (target, dataType)
6366

6467
then:
6568
def result = extractImports (target)
66-
result.contains("""\
67-
import external.Isbn;
68-
""")
69+
result.contains("import external.$type;".toString ())
70+
71+
where:
72+
id | type
73+
'Isbn' | 'Isbn'
74+
'Isbn' | 'IsbnX'
6975
}
7076

7177
void "writes import of generic list type" () {
@@ -78,13 +84,52 @@ import external.Isbn;
7884

7985
then:
8086
def result = extractImports (target)
81-
result.contains("""\
82-
import java.util.List;
87+
result.contains("import java.util.List;")
88+
}
89+
90+
void "writes import of generic object list type" () {
91+
def dataType = new ObjectDataType ('Foo', 'mine', [
92+
'bars': new ListDataType (new ObjectDataTypeP (new DataTypeName(id, type), 'other', [:],
93+
null, false, null))
94+
], null, false, null)
95+
96+
when:
97+
writer.write (target, dataType)
98+
99+
then:
100+
def result = extractImports (target)
101+
result.contains("import other.$type;".toString ())
102+
103+
where:
104+
id | type
105+
'Bar' | 'Bar'
106+
'Bar' | 'BarX'
107+
}
108+
109+
void "writes class" () {
110+
def pkg = 'io.openapiprocessor.test'
111+
def dataType = new ObjectDataTypeP (
112+
new DataTypeName(id, type), pkg, [:],
113+
null, true, null)
114+
115+
when:
116+
writer.write (target, dataType)
117+
118+
then:
119+
target.toString ().contains ("""\
120+
public class $type {
121+
122+
}
83123
""")
124+
125+
where:
126+
id | type
127+
'Bar' | 'Bar'
128+
'Bar' | 'BarX'
84129
}
85130

86-
void "writes properties"() {
87-
def pkg = 'com.github.hauner.openapi'
131+
void "writes simple properties"() {
132+
def pkg = 'io.openapiprocessor.test'
88133
def dataType = new ObjectDataType ('Book', pkg, [
89134
isbn: new StringDataType(),
90135
title: new StringDataType ()
@@ -104,8 +149,27 @@ import java.util.List;
104149
""")
105150
}
106151

152+
void "writes object property"() {
153+
def pkg = 'io.openapiprocessor.test'
154+
def dataType = new ObjectDataType ('Foo', pkg, [
155+
bar: new ObjectDataTypeP (new DataTypeName(id, type), 'other', [:],
156+
null, false, null),
157+
], null, false, null)
158+
159+
when:
160+
writer.write (target, dataType)
161+
162+
then:
163+
extractBody (target).contains (" private $type bar;".toString ())
164+
165+
where:
166+
id | type
167+
'Bar' | 'Bar'
168+
'Bar' | 'BarX'
169+
}
170+
107171
void "writes property getters & setters" () {
108-
def pkg = 'com.github.hauner.openapi'
172+
def pkg = 'io.openapiprocessor.test'
109173
def dataType = new ObjectDataType ('Book', pkg, [
110174
isbn: new StringDataType(),
111175
title: new StringDataType ()
@@ -137,8 +201,35 @@ import java.util.List;
137201
""")
138202
}
139203

204+
void "writes object property getter & setter" () {
205+
def pkg = 'com.github.hauner.openapi'
206+
def dataType = new ObjectDataType ('Foo', pkg, [
207+
bar: new ObjectDataTypeP (new DataTypeName(id, type), 'other', [:],
208+
null, false, null),
209+
], null, false, null)
210+
211+
when:
212+
writer.write (target, dataType)
213+
214+
then:
215+
target.toString ().contains ("""\
216+
public $type getBar() {
217+
return bar;
218+
}
219+
220+
public void setBar($type bar) {
221+
this.bar = bar;
222+
}
223+
224+
""")
225+
where:
226+
id | type
227+
'Bar' | 'Bar'
228+
'Bar' | 'BarX'
229+
}
230+
140231
void "writes deprecated class" () {
141-
def pkg = 'io.openapiprocessor.core'
232+
def pkg = 'io.openapiprocessor.test'
142233
def dataType = new ObjectDataType (
143234
'Bar', pkg, [:],null, true, null)
144235

@@ -155,7 +246,7 @@ public class Bar {
155246
}
156247

157248
void "writes deprecated property" () {
158-
def pkg = 'com.github.hauner.openapi'
249+
def pkg = 'io.openapiprocessor.test'
159250
def dataType = new ObjectDataType ('Book', pkg, [
160251
isbn: new StringDataType(null, true, null)
161252
], null, false, null)
@@ -172,7 +263,7 @@ public class Bar {
172263
}
173264

174265
void "writes deprecated property getters & setters" () {
175-
def pkg = 'com.github.hauner.openapi'
266+
def pkg = 'io.openapiprocessor.test'
176267
def dataType = new ObjectDataType ('Book', pkg, [
177268
isbn: new StringDataType(null, true, null)
178269
], null, false, null)
@@ -229,9 +320,7 @@ public class Bar {
229320

230321
then:
231322
def result = extractImports (target)
232-
result.contains("""\
233-
import com.fasterxml.jackson.annotation.JsonProperty;
234-
""")
323+
result.contains("import com.fasterxml.jackson.annotation.JsonProperty;")
235324
}
236325

237326
void "writes properties with @JsonProperty annotation" () {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2021 https://github.com/openapi-processor/openapi-processor-core
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiprocessor.core.model.datatypes
7+
8+
import io.kotest.core.spec.style.StringSpec
9+
import io.kotest.data.blocking.forAll
10+
import io.kotest.data.row
11+
import io.kotest.matchers.shouldBe
12+
13+
class ObjectDataTypeSpec : StringSpec({
14+
15+
"should create import with type name" {
16+
forAll(row("Foo", "Foo"), row("Fooo", "FoooX")) { id, type ->
17+
val odt = ObjectDataType(DataTypeName(id, type), "pkg")
18+
odt.getImports() shouldBe setOf("pkg.$type")
19+
}
20+
}
21+
22+
})

0 commit comments

Comments
 (0)