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

Commit bfb03d1

Browse files
committed
1 parent c31c3c2 commit bfb03d1

File tree

3 files changed

+147
-57
lines changed

3 files changed

+147
-57
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package io.openapiprocessor.core.model.datatypes
77

8+
import io.openapiprocessor.core.model.Documentation
9+
810
/**
911
* schema "properties" data type wrapper. readOnly/writeOnly may be different on each use of the
1012
* same schema as a property in another schema.
@@ -19,6 +21,10 @@ open class PropertyDataType(
1921
return dataType.getName()
2022
}
2123

24+
override fun getTypeName(): String {
25+
return dataType.getTypeName()
26+
}
27+
2228
override fun getPackageName(): String {
2329
return dataType.getPackageName()
2430
}
@@ -27,5 +33,17 @@ open class PropertyDataType(
2733
return dataType.getImports()
2834
}
2935

36+
override val referencedImports: Set<String>
37+
get() = dataType.referencedImports
38+
39+
override val constraints: DataTypeConstraints?
40+
get() = dataType.constraints
41+
42+
override val deprecated: Boolean
43+
get() = dataType.deprecated
44+
45+
override val documentation: Documentation?
46+
get() = dataType.documentation
47+
3048
}
3149

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

Lines changed: 62 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +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.DataTypeConstraints
109
import io.openapiprocessor.core.model.datatypes.DataTypeName
11-
import io.openapiprocessor.core.model.datatypes.ObjectDataType as ObjectDataTypeP
1210
import io.openapiprocessor.core.model.datatypes.StringDataType
13-
import io.openapiprocessor.core.support.datatypes.ListDataType
1411
import io.openapiprocessor.core.support.datatypes.ObjectDataType
1512
import io.openapiprocessor.core.writer.java.BeanValidationFactory
1613
import io.openapiprocessor.core.writer.java.DataTypeWriter
@@ -20,6 +17,9 @@ import spock.lang.Specification
2017

2118
import static io.openapiprocessor.core.AssertKt.extractBody
2219
import static io.openapiprocessor.core.AssertKt.extractImports
20+
import static io.openapiprocessor.core.support.datatypes.Builder.listDataType
21+
import static io.openapiprocessor.core.support.datatypes.Builder.objectDataType
22+
import static io.openapiprocessor.core.support.datatypes.Builder.propertyDataType
2323

2424
class DataTypeWriterSpec extends Specification {
2525
def headerWriter = Mock SimpleWriter
@@ -29,8 +29,7 @@ class DataTypeWriterSpec extends Specification {
2929
def target = new StringWriter ()
3030

3131
void "writes 'generated' comment" () {
32-
def dataType = new ObjectDataType(
33-
'Book', '', [:], null, false, null)
32+
def dataType = objectDataType ('Book', '')
3433

3534
when:
3635
writer.write (target, dataType)
@@ -41,8 +40,7 @@ class DataTypeWriterSpec extends Specification {
4140

4241
void "writes 'package'" () {
4342
def pkg = 'io.openapiprocessor.test'
44-
def dataType = new ObjectDataType (
45-
'Book', pkg, [:], null, false, null)
43+
def dataType = objectDataType ('Book', pkg)
4644

4745
when:
4846
writer.write (target, dataType)
@@ -57,10 +55,9 @@ package $pkg;
5755
void "writes imports of nested types" () {
5856
def pkg = 'external'
5957

60-
def dataType = new ObjectDataType ('Book', 'mine', [
61-
'isbn': new ObjectDataTypeP (new DataTypeName(id, type), pkg, [:],
62-
null, false, null)
63-
], null, false, null)
58+
def dataType = objectDataType ('Book', 'mine', [
59+
'isbn': propertyDataType (objectDataType (new DataTypeName (id, type), pkg))
60+
])
6461

6562
when:
6663
writer.write (target, dataType)
@@ -76,9 +73,9 @@ package $pkg;
7673
}
7774

7875
void "writes import of generic list type" () {
79-
def dataType = new ObjectDataType ('Book', 'mine', [
80-
'authors': new ListDataType (new StringDataType(), new DataTypeConstraints())
81-
], null, false, null)
76+
def dataType = objectDataType ('Book', 'mine', [
77+
'authors': propertyDataType (listDataType (new StringDataType()))
78+
])
8279

8380
when:
8481
writer.write (target, dataType)
@@ -89,11 +86,11 @@ package $pkg;
8986
}
9087

9188
void "writes import of generic object list type" () {
92-
def dataType = new ObjectDataType ('Foo', 'mine', [
93-
'bars': new ListDataType (
94-
new ObjectDataTypeP (new DataTypeName(id, type), 'other', [:],
95-
null, false, null), new DataTypeConstraints())
96-
], null, false, null)
89+
def dataType = objectDataType ('Foo', 'mine', [
90+
'bars': propertyDataType (listDataType (
91+
objectDataType (new DataTypeName (id, type), 'other')
92+
))
93+
])
9794

9895
when:
9996
writer.write (target, dataType)
@@ -110,9 +107,8 @@ package $pkg;
110107

111108
void "writes class" () {
112109
def pkg = 'io.openapiprocessor.test'
113-
def dataType = new ObjectDataTypeP (
114-
new DataTypeName(id, type), pkg, [:],
115-
null, true, null)
110+
111+
def dataType = objectDataType (new DataTypeName(id, type), pkg, [:])
116112

117113
when:
118114
writer.write (target, dataType)
@@ -132,10 +128,11 @@ public class $type {
132128

133129
void "writes simple properties"() {
134130
def pkg = 'io.openapiprocessor.test'
135-
def dataType = new ObjectDataType ('Book', pkg, [
136-
isbn: new StringDataType(),
137-
title: new StringDataType ()
138-
], null, false, null)
131+
132+
def dataType = objectDataType ('Book', pkg, [
133+
isbn : propertyDataType (new StringDataType ()),
134+
title: propertyDataType (new StringDataType ())
135+
])
139136

140137
when:
141138
writer.write (target, dataType)
@@ -151,12 +148,12 @@ public class $type {
151148
""")
152149
}
153150

154-
void "writes object property"() {
151+
void "writes object property" () {
155152
def pkg = 'io.openapiprocessor.test'
156-
def dataType = new ObjectDataType ('Foo', pkg, [
157-
bar: new ObjectDataTypeP (new DataTypeName(id, type), 'other', [:],
158-
null, false, null),
159-
], null, false, null)
153+
154+
def dataType = objectDataType ('Foo', pkg, [
155+
bar: propertyDataType (objectDataType (new DataTypeName(id, type), 'other', [:]))
156+
])
160157

161158
when:
162159
writer.write (target, dataType)
@@ -172,10 +169,11 @@ public class $type {
172169

173170
void "writes property getters & setters" () {
174171
def pkg = 'io.openapiprocessor.test'
175-
def dataType = new ObjectDataType ('Book', pkg, [
176-
isbn: new StringDataType(),
177-
title: new StringDataType ()
178-
], null, false, null)
172+
173+
def dataType = objectDataType ('Book', pkg, [
174+
isbn : propertyDataType (new StringDataType ()),
175+
title: propertyDataType (new StringDataType ())
176+
])
179177

180178
when:
181179
writer.write (target, dataType)
@@ -205,10 +203,10 @@ public class $type {
205203

206204
void "writes object property getter & setter" () {
207205
def pkg = 'com.github.hauner.openapi'
208-
def dataType = new ObjectDataType ('Foo', pkg, [
209-
bar: new ObjectDataTypeP (new DataTypeName(id, type), 'other', [:],
210-
null, false, null),
211-
], null, false, null)
206+
207+
def dataType = objectDataType ('Foo', pkg, [
208+
bar: propertyDataType (objectDataType (new DataTypeName (id, type), 'other'))
209+
])
212210

213211
when:
214212
writer.write (target, dataType)
@@ -232,6 +230,7 @@ public class $type {
232230

233231
void "writes deprecated class" () {
234232
def pkg = 'io.openapiprocessor.test'
233+
235234
def dataType = new ObjectDataType (
236235
'Bar', pkg, [:],null, true, null)
237236

@@ -249,9 +248,12 @@ public class Bar {
249248

250249
void "writes deprecated property" () {
251250
def pkg = 'io.openapiprocessor.test'
252-
def dataType = new ObjectDataType ('Book', pkg, [
253-
isbn: new StringDataType(null, true, null)
254-
], null, false, null)
251+
252+
def dataType = objectDataType ('Book', pkg, [
253+
isbn: propertyDataType (
254+
new StringDataType(null, true, null)
255+
)
256+
])
255257

256258
when:
257259
writer.write (target, dataType)
@@ -266,9 +268,12 @@ public class Bar {
266268

267269
void "writes deprecated property getters & setters" () {
268270
def pkg = 'io.openapiprocessor.test'
269-
def dataType = new ObjectDataType ('Book', pkg, [
270-
isbn: new StringDataType(null, true, null)
271-
], null, false, null)
271+
272+
def dataType = objectDataType ('Book', pkg, [
273+
isbn: propertyDataType (
274+
new StringDataType (null, true, null)
275+
)
276+
])
272277

273278
when:
274279
writer.write (target, dataType)
@@ -291,10 +296,10 @@ public class Bar {
291296
void "writes properties with valid java identifiers" () {
292297
def pkg = 'com.github.hauner.openapi'
293298

294-
def dataType = new ObjectDataType ('Book', pkg, [
295-
'a-isbn' : new StringDataType (),
296-
'a-title': new StringDataType ()
297-
], null, false, null)
299+
def dataType = objectDataType ('Book', pkg, [
300+
'a-isbn' : propertyDataType (new StringDataType ()),
301+
'a-title': propertyDataType (new StringDataType ())
302+
])
298303

299304
when:
300305
writer.write (target, dataType)
@@ -312,10 +317,10 @@ public class Bar {
312317
void "writes imports of @JsonProperty" () {
313318
def pkg = 'external'
314319

315-
def dataType = new ObjectDataType ('Book', pkg, [
316-
'isbn' : new StringDataType (),
317-
'title': new StringDataType ()
318-
], null, false, null)
320+
def dataType = objectDataType ('Book', pkg, [
321+
'isbn' : propertyDataType (new StringDataType ()),
322+
'title': propertyDataType (new StringDataType ())
323+
])
319324

320325
when:
321326
writer.write (target, dataType)
@@ -328,10 +333,10 @@ public class Bar {
328333
void "writes properties with @JsonProperty annotation" () {
329334
def pkg = 'com.github.hauner.openapi'
330335

331-
def dataType = new ObjectDataType ('Book', pkg, [
332-
'a-isbn': new StringDataType (),
333-
'a-title': new StringDataType ()
334-
], null, false, null)
336+
def dataType = objectDataType ('Book', pkg, [
337+
'a-isbn' : propertyDataType (new StringDataType ()),
338+
'a-title': propertyDataType (new StringDataType ())
339+
])
335340

336341
when:
337342
writer.write (target, dataType)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.support.datatypes
7+
8+
import io.openapiprocessor.core.model.Documentation
9+
import io.openapiprocessor.core.model.datatypes.DataType
10+
import io.openapiprocessor.core.model.datatypes.DataTypeConstraints
11+
import io.openapiprocessor.core.model.datatypes.DataTypeName
12+
import io.openapiprocessor.core.model.datatypes.ObjectDataType
13+
import io.openapiprocessor.core.model.datatypes.PropertyDataType
14+
15+
class Builder {
16+
17+
static PropertyDataType propertyDataType(
18+
DataType dataType,
19+
Boolean readOnly = false,
20+
Boolean writeOnly = false
21+
) {
22+
return new PropertyDataType(readOnly, writeOnly, dataType)
23+
}
24+
25+
static ObjectDataType objectDataType (
26+
String nameId,
27+
String pkg,
28+
LinkedHashMap<String, DataType> properties = [:],
29+
DataTypeConstraints constraints = null,
30+
Boolean deprecated = false,
31+
Documentation documentation = null
32+
) {
33+
return new ObjectDataType(
34+
new DataTypeName(nameId, nameId),
35+
pkg,
36+
properties,
37+
constraints,
38+
deprecated,
39+
documentation
40+
)
41+
}
42+
43+
static ObjectDataType objectDataType (
44+
DataTypeName dataTypeName,
45+
String pkg,
46+
LinkedHashMap<String, DataType> properties = [:],
47+
DataTypeConstraints constraints = null,
48+
Boolean deprecated = false,
49+
Documentation documentation = null
50+
) {
51+
return new ObjectDataType(
52+
dataTypeName,
53+
pkg,
54+
properties,
55+
constraints,
56+
deprecated,
57+
documentation
58+
)
59+
}
60+
61+
static ListDataType listDataType(
62+
DataType item,
63+
DataTypeConstraints constraints = null
64+
) {
65+
return new ListDataType (item, constraints)
66+
}
67+
}

0 commit comments

Comments
 (0)