6
6
package com.github.hauner.openapi.core.writer.java
7
7
8
8
import io.openapiprocessor.core.converter.ApiOptions
9
+ import io.openapiprocessor.core.model.datatypes.DataTypeName
10
+ import io.openapiprocessor.core.model.datatypes.ObjectDataType as ObjectDataTypeP
9
11
import io.openapiprocessor.core.model.datatypes.StringDataType
10
12
import io.openapiprocessor.core.support.datatypes.ListDataType
11
13
import io.openapiprocessor.core.support.datatypes.ObjectDataType
@@ -15,6 +17,7 @@ import io.openapiprocessor.core.writer.java.JavaDocWriter
15
17
import io.openapiprocessor.core.writer.java.SimpleWriter
16
18
import spock.lang.Specification
17
19
20
+ import static io.openapiprocessor.core.AssertKt.extractBody
18
21
import static io.openapiprocessor.core.AssertKt.extractImports
19
22
20
23
class DataTypeWriterSpec extends Specification {
@@ -36,7 +39,7 @@ class DataTypeWriterSpec extends Specification {
36
39
}
37
40
38
41
void " writes 'package'" () {
39
- def pkg = ' com.github.hauner.openapi '
42
+ def pkg = ' io.openapiprocessor.test '
40
43
def dataType = new ObjectDataType (
41
44
' Book' , pkg, [:], null , false , null )
42
45
@@ -54,18 +57,21 @@ package $pkg;
54
57
def pkg = ' external'
55
58
56
59
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 )
59
62
], null , false , null )
60
63
61
64
when :
62
65
writer. write (target, dataType)
63
66
64
67
then :
65
68
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'
69
75
}
70
76
71
77
void " writes import of generic list type" () {
@@ -78,13 +84,52 @@ import external.Isbn;
78
84
79
85
then :
80
86
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
+ }
83
123
""" )
124
+
125
+ where :
126
+ id | type
127
+ ' Bar' | ' Bar'
128
+ ' Bar' | ' BarX'
84
129
}
85
130
86
- void " writes properties" () {
87
- def pkg = ' com.github.hauner.openapi '
131
+ void " writes simple properties" () {
132
+ def pkg = ' io.openapiprocessor.test '
88
133
def dataType = new ObjectDataType (' Book' , pkg, [
89
134
isbn : new StringDataType (),
90
135
title : new StringDataType ()
@@ -104,8 +149,27 @@ import java.util.List;
104
149
""" )
105
150
}
106
151
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
+
107
171
void " writes property getters & setters" () {
108
- def pkg = ' com.github.hauner.openapi '
172
+ def pkg = ' io.openapiprocessor.test '
109
173
def dataType = new ObjectDataType (' Book' , pkg, [
110
174
isbn : new StringDataType (),
111
175
title : new StringDataType ()
@@ -137,8 +201,35 @@ import java.util.List;
137
201
""" )
138
202
}
139
203
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
+
140
231
void " writes deprecated class" () {
141
- def pkg = ' io.openapiprocessor.core '
232
+ def pkg = ' io.openapiprocessor.test '
142
233
def dataType = new ObjectDataType (
143
234
' Bar' , pkg, [:],null , true , null )
144
235
@@ -155,7 +246,7 @@ public class Bar {
155
246
}
156
247
157
248
void " writes deprecated property" () {
158
- def pkg = ' com.github.hauner.openapi '
249
+ def pkg = ' io.openapiprocessor.test '
159
250
def dataType = new ObjectDataType (' Book' , pkg, [
160
251
isbn : new StringDataType (null , true , null )
161
252
], null , false , null )
@@ -172,7 +263,7 @@ public class Bar {
172
263
}
173
264
174
265
void " writes deprecated property getters & setters" () {
175
- def pkg = ' com.github.hauner.openapi '
266
+ def pkg = ' io.openapiprocessor.test '
176
267
def dataType = new ObjectDataType (' Book' , pkg, [
177
268
isbn : new StringDataType (null , true , null )
178
269
], null , false , null )
@@ -229,9 +320,7 @@ public class Bar {
229
320
230
321
then :
231
322
def result = extractImports (target)
232
- result. contains(""" \
233
- import com.fasterxml.jackson.annotation.JsonProperty;
234
- """ )
323
+ result. contains(" import com.fasterxml.jackson.annotation.JsonProperty;" )
235
324
}
236
325
237
326
void " writes properties with @JsonProperty annotation" () {
0 commit comments