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

Commit a78144d

Browse files
committed
1 parent eb64b16 commit a78144d

12 files changed

Lines changed: 99 additions & 50 deletions

File tree

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class DataTypeWriter(
8686

8787
var propTypeName = propDataType.getTypeName()
8888
if(apiOptions.beanValidation) {
89-
val info = validationAnnotations.validate(propDataType, required)
89+
val info = validationAnnotations.validate(propDataType.dataType, required)
9090
val prop = info.prop
9191
prop.annotations.forEach {
9292
result += " ${it}\n"
@@ -185,9 +185,8 @@ class DataTypeWriter(
185185
imports.addAll(prop.imports)
186186

187187
dataType.forEach { propName, propDataType ->
188-
val propInfo = validationAnnotations.validate(
189-
propDataType, dataType.isRequired(propName))
190-
188+
val target = getTarget(propDataType)
189+
val propInfo = validationAnnotations.validate(target, dataType.isRequired(propName))
191190
val propProp = propInfo.prop
192191
imports.addAll(propProp.imports)
193192
}
@@ -198,6 +197,13 @@ class DataTypeWriter(
198197
.sorted()
199198
}
200199

200+
private fun getTarget(dataType: DataType): DataType {
201+
if (dataType is PropertyDataType)
202+
return dataType.dataType
203+
204+
return dataType
205+
}
206+
201207
}
202208

203209
class PropertyAccess(val value: String)

src/test/groovy/com/github/hauner/openapi/core/converter/DataTypeConverterLoopSpec.groovy

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import io.openapiprocessor.core.framework.Framework
2121
import io.openapiprocessor.core.model.datatypes.LazyDataType
2222
import io.openapiprocessor.core.converter.ApiOptions
2323
import io.openapiprocessor.core.model.datatypes.ObjectDataType
24+
import io.openapiprocessor.core.model.datatypes.PropertyDataType
2425
import spock.lang.Specification
2526

2627
import static com.github.hauner.openapi.core.test.OpenApiParser.parse
@@ -66,9 +67,11 @@ components:
6667
def itf = api.interfaces.first ()
6768
def ep = itf.endpoints.first ()
6869
def rp = ep.getFirstResponse ('200')
69-
def rt = rp.responseType
70-
def sf = rt.properties.self
70+
def rt = rp.responseType as ObjectDataType
71+
def pt = rt.properties.self
72+
def sf = pt.dataType
7173
rt instanceof ObjectDataType
74+
pt instanceof PropertyDataType
7275
sf instanceof LazyDataType
7376
sf.name == 'Self'
7477
sf.packageName == 'io.openapiprocessor.generated.model'

src/test/groovy/com/github/hauner/openapi/core/converter/DataTypeConverterSpec.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class DataTypeConverterSpec extends Specification {
150150
def bar = dt.find ('Bar') as ObjectDataType
151151
bar.properties['val'].name == 'String'
152152
def foo = dt.find ('Foo') as ObjectDataType
153-
foo.properties['bar'] == bar
153+
foo.properties['bar'].dataType == bar
154154
}
155155
156156
void "converts simple array schema to Array[]" () {

src/test/kotlin/io/openapiprocessor/core/model/DataTypesSpec.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2021 https://github.com/openapi-processor/openapi-processor-core
2+
* Copyright 2021 https://github.com/openapi-processor/openapi-processor-core
33
* PDX-License-Identifier: Apache-2.0
44
*/
55

@@ -8,6 +8,7 @@ package io.openapiprocessor.core.model
88
import io.kotest.core.spec.style.StringSpec
99
import io.kotest.matchers.shouldBe
1010
import io.openapiprocessor.core.model.datatypes.DataTypeName
11+
import io.openapiprocessor.core.model.datatypes.PropertyDataType
1112
import io.openapiprocessor.core.model.datatypes.StringDataType
1213
import io.openapiprocessor.core.model.datatypes.StringEnumDataType
1314
import io.openapiprocessor.core.support.datatypes.ObjectDataType
@@ -18,15 +19,22 @@ class DataTypesSpec : StringSpec({
1819

1920
"does not provide unused model data types" {
2021
dataTypes.add(ObjectDataType("Foo", "any", linkedMapOf(
21-
Pair("foo", StringDataType())
22+
Pair("foo", PropertyDataType(
23+
readOnly = false,
24+
writeOnly = false,
25+
dataType = StringDataType()))
2226
)))
2327

2428
dataTypes.getModelDataTypes().size shouldBe 0
2529
}
2630

2731
"does provide used model data types" {
2832
dataTypes.add(ObjectDataType("Foo", "any", linkedMapOf(
29-
Pair("foo", StringDataType())
33+
Pair("foo", PropertyDataType(
34+
readOnly = false,
35+
writeOnly = false,
36+
dataType = StringDataType()
37+
))
3038
)))
3139
dataTypes.addRef("Foo")
3240

src/test/kotlin/io/openapiprocessor/core/model/EndpointResponseSpec.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ package io.openapiprocessor.core.model
77

88
import io.kotest.core.spec.style.StringSpec
99
import io.kotest.matchers.shouldBe
10-
import io.openapiprocessor.core.model.datatypes.AnyOneOfObjectDataType
11-
import io.openapiprocessor.core.model.datatypes.DataTypeName
12-
import io.openapiprocessor.core.model.datatypes.ObjectDataType
13-
import io.openapiprocessor.core.model.datatypes.StringDataType
10+
import io.openapiprocessor.core.model.datatypes.*
1411
import io.openapiprocessor.core.processor.mapping.v2.ResultStyle
1512

1613
class EndpointResponseSpec: StringSpec({
@@ -40,7 +37,11 @@ class EndpointResponseSpec: StringSpec({
4037
"result style all without errors uses single response" {
4138
val er = EndpointResponse(
4239
Response("", ObjectDataType(DataTypeName("Foo"), "pkg", linkedMapOf(
43-
"bar" to StringDataType()
40+
"bar" to PropertyDataType(
41+
readOnly = false,
42+
writeOnly = false,
43+
dataType = StringDataType()
44+
)
4445
))),
4546
emptySet()
4647
)
@@ -52,7 +53,11 @@ class EndpointResponseSpec: StringSpec({
5253
"result style single uses single response" {
5354
val er = EndpointResponse(
5455
Response("", ObjectDataType(DataTypeName("Foo"), "pkg", linkedMapOf(
55-
"bar" to StringDataType()
56+
"bar" to PropertyDataType(
57+
readOnly = false,
58+
writeOnly = false,
59+
dataType = StringDataType()
60+
)
5661
))),
5762
setOf(Response("text/plain", StringDataType()))
5863
)

src/test/kotlin/io/openapiprocessor/core/model/datatypes/ComposedObjectDataTypeSpec.kt

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@ import io.kotest.core.spec.style.StringSpec
99
import io.kotest.matchers.collections.shouldContainAll
1010
import io.kotest.matchers.shouldBe
1111
import io.openapiprocessor.core.support.datatypes.ObjectDataType
12+
import io.openapiprocessor.core.support.datatypes.propertyDataType
13+
import io.openapiprocessor.core.support.datatypes.propertyDataTypeString
1214

1315
class ComposedObjectDataTypeSpec : StringSpec({
1416

1517
"loop properties of allOf objects as if it was a single object" {
1618
val composed = AllOfObjectDataType(DataTypeName("Foo"), "pkg", listOf(
1719
ObjectDataType("Foo", "pkg", linkedMapOf(
18-
Pair("foo", StringDataType()),
19-
Pair("foobar", StringDataType())
20-
)),
20+
Pair("foo", propertyDataTypeString()),
21+
Pair("foobar", propertyDataTypeString()))
22+
),
2123
ObjectDataType("Bar", "pkg", linkedMapOf(
22-
Pair("bar", StringDataType()),
23-
Pair("barfoo", StringDataType())
24+
Pair("bar", propertyDataTypeString()),
25+
Pair("barfoo", propertyDataTypeString())
2426
))
2527
))
2628

@@ -44,12 +46,12 @@ class ComposedObjectDataTypeSpec : StringSpec({
4446
"allOf creates imports for all items" {
4547
val composed = AllOfObjectDataType(DataTypeName("Foo"), "pkg", listOf(
4648
ObjectDataType("Foo", "pkg", linkedMapOf(
47-
"foo" to OffsetDateTimeDataType()
49+
"foo" to propertyDataType(OffsetDateTimeDataType())
4850
)),
4951
ObjectDataType("Bar", "pkg", linkedMapOf(
50-
"bar" to ObjectDataType("BarBar", "pkg", linkedMapOf(
51-
"barbar" to OffsetDateTimeDataType()
52-
))
52+
"bar" to propertyDataType(ObjectDataType("BarBar", "pkg", linkedMapOf(
53+
"barbar" to propertyDataType(OffsetDateTimeDataType())
54+
)))
5355
))
5456
))
5557

@@ -60,8 +62,8 @@ class ComposedObjectDataTypeSpec : StringSpec({
6062
"allOf creates does not leak import for type-less item" {
6163
val composed = AllOfObjectDataType(DataTypeName("Foo"), "pkg", listOf(
6264
ObjectDataType("Bar", "pkg", linkedMapOf(
63-
"bar" to StringDataType())
64-
),
65+
"bar" to propertyDataTypeString()
66+
)),
6567
NoDataType("Leak")
6668
))
6769

@@ -72,13 +74,13 @@ class ComposedObjectDataTypeSpec : StringSpec({
7274
"allOf handles 'required' constraint of all items" {
7375
val composed = AllOfObjectDataType(DataTypeName("AllOf"), "pkg", listOf(
7476
ObjectDataType("Foo", "pkg", linkedMapOf(
75-
"foo" to StringDataType(),
76-
"fux" to StringDataType()
77+
"foo" to propertyDataTypeString(),
78+
"fux" to propertyDataTypeString()
7779
), constraints = DataTypeConstraints(required = listOf("foo", "fux"))),
7880
ObjectDataType(
7981
"Bar", "pkg", linkedMapOf(
80-
"bar" to StringDataType(),
81-
"bux" to StringDataType()
82+
"bar" to propertyDataTypeString(),
83+
"bux" to propertyDataTypeString()
8284
), constraints = DataTypeConstraints(required = listOf("bar", "bux")))
8385
))
8486

@@ -88,13 +90,13 @@ class ComposedObjectDataTypeSpec : StringSpec({
8890
"allOf without 'required' has null constraints" {
8991
val composed = AllOfObjectDataType(DataTypeName("AllOf"), "pkg", listOf(
9092
ObjectDataType("Foo", "pkg", linkedMapOf(
91-
"foo" to StringDataType(),
92-
"fux" to StringDataType()
93+
"foo" to propertyDataTypeString(),
94+
"fux" to propertyDataTypeString()
9395
), constraints = DataTypeConstraints()),
9496
ObjectDataType(
9597
"Bar", "pkg", linkedMapOf(
96-
"bar" to StringDataType(),
97-
"bux" to StringDataType()
98+
"bar" to propertyDataTypeString(),
99+
"bux" to propertyDataTypeString()
98100
), constraints = null)
99101
))
100102

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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.datatypes.*
9+
10+
fun propertyDataType(dataType: DataType): PropertyDataType {
11+
return PropertyDataType(readOnly = false, writeOnly = false, dataType = dataType)
12+
}
13+
14+
fun propertyDataTypeString(): PropertyDataType {
15+
return PropertyDataType(readOnly = false, writeOnly = false, dataType = StringDataType())
16+
}

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import io.kotest.matchers.shouldBe
1111
import io.openapiprocessor.core.model.datatypes.*
1212
import io.openapiprocessor.core.support.datatypes.ListDataType
1313
import io.openapiprocessor.core.support.datatypes.ObjectDataType
14+
import io.openapiprocessor.core.support.datatypes.propertyDataType
15+
import io.openapiprocessor.core.support.datatypes.propertyDataTypeString
1416

1517
class BeanValidationFactorySpec: StringSpec({
1618
isolationMode = IsolationMode.InstancePerTest
@@ -19,7 +21,9 @@ class BeanValidationFactorySpec: StringSpec({
1921
val validation = BeanValidationFactory()
2022

2123
val dataType = ArrayDataType(
22-
ObjectDataType("Foo", "pkg", linkedMapOf("foo" to StringDataType())
24+
ObjectDataType("Foo", "pkg", linkedMapOf(
25+
"foo" to propertyDataTypeString()
26+
)
2327
))
2428
val info = validation.validate(dataType)
2529

@@ -55,7 +59,7 @@ class BeanValidationFactorySpec: StringSpec({
5559
val validation = BeanValidationFactory()
5660

5761
val dataType = MappedCollectionDataType("List", "pkg",
58-
ObjectDataType("Foo", "pkg", linkedMapOf("foo" to StringDataType())
62+
ObjectDataType("Foo", "pkg", linkedMapOf("foo" to propertyDataTypeString())
5963
))
6064
val info = validation.validate(dataType)
6165

@@ -150,9 +154,9 @@ class BeanValidationFactorySpec: StringSpec({
150154

151155
val dataType = ListDataType(
152156
ObjectDataType("Foo", "pkg",
153-
linkedMapOf("foo" to StringDataType(
157+
linkedMapOf("foo" to propertyDataType(StringDataType(
154158
constraints = DataTypeConstraints(minLength = 2, maxLength = 3))
155-
)))
159+
))))
156160
val info = validation.validate(dataType, true)
157161

158162
val prop = info.prop
@@ -198,9 +202,9 @@ class BeanValidationFactorySpec: StringSpec({
198202

199203
val dataType = ArrayDataType(
200204
ObjectDataType("Foo", "pkg",
201-
linkedMapOf("foo" to StringDataType(
205+
linkedMapOf("foo" to propertyDataType(StringDataType(
202206
constraints = DataTypeConstraints(minLength = 2, maxLength = 3))
203-
)))
207+
))))
204208
val info = validation.validate(dataType, true)
205209

206210
val prop = info.prop

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import io.openapiprocessor.core.model.datatypes.ModelDataType
1616
import io.openapiprocessor.core.support.datatypes.ObjectDataType
1717
import io.openapiprocessor.core.model.datatypes.StringDataType
1818
import io.openapiprocessor.core.support.datatypes.ListDataType
19+
import io.openapiprocessor.core.support.datatypes.propertyDataType
20+
import io.openapiprocessor.core.support.datatypes.propertyDataTypeString
1921
import java.io.StringWriter
2022

2123
class DataTypeWriterSpec: StringSpec({
@@ -29,7 +31,7 @@ class DataTypeWriterSpec: StringSpec({
2931
options.beanValidation = true
3032

3133
val dataType = ObjectDataType("Foo", "pkg", linkedMapOf(
32-
Pair("foo", StringDataType())
34+
Pair("foo", propertyDataTypeString())
3335
), DataTypeConstraints(required = listOf("foo")), false)
3436

3537
// when:
@@ -44,7 +46,7 @@ class DataTypeWriterSpec: StringSpec({
4446
options.beanValidation = true
4547

4648
val dataType = ObjectDataType("Foo", "pkg", linkedMapOf(
47-
Pair("foo", StringDataType())
49+
Pair("foo", propertyDataTypeString())
4850
), DataTypeConstraints(required = listOf("foo")), false)
4951

5052
// when:
@@ -62,8 +64,8 @@ class DataTypeWriterSpec: StringSpec({
6264

6365
"writes import of nested generic list type" {
6466
val dataType = ObjectDataType("Foo", "pkg",
65-
linkedMapOf("foos" to ListDataType(StringDataType())
66-
), null, false)
67+
linkedMapOf("foos" to propertyDataType(ListDataType(StringDataType()))
68+
))
6769

6870
// when:
6971
writer.write(target, dataType)
@@ -85,7 +87,7 @@ class DataTypeWriterSpec: StringSpec({
8587
writer = DataTypeWriter(options, headerWriter, validation)
8688

8789
val dataType = ObjectDataType("Foo",
88-
"pkg", linkedMapOf("foo" to StringDataType()))
90+
"pkg", linkedMapOf("foo" to propertyDataTypeString()))
8991

9092
// when:
9193
writer.write(target, dataType)
@@ -106,7 +108,7 @@ class DataTypeWriterSpec: StringSpec({
106108
writer = DataTypeWriter(options, headerWriter, validation)
107109

108110
val dataType = ObjectDataType("Foo",
109-
"pkg", linkedMapOf("foo" to StringDataType()))
111+
"pkg", linkedMapOf("foo" to propertyDataTypeString()))
110112

111113
// when:
112114
writer.write(target, dataType)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import io.openapiprocessor.core.model.parameters.AdditionalParameter
2424
import io.openapiprocessor.core.model.parameters.Parameter
2525
import io.openapiprocessor.core.model.parameters.ParameterBase
2626
import io.openapiprocessor.core.support.datatypes.ObjectDataType
27+
import io.openapiprocessor.core.support.datatypes.propertyDataTypeString
2728
import java.io.StringWriter
2829
import java.io.Writer
2930
import io.mockk.mockk as stub
@@ -186,8 +187,8 @@ class InterfaceWriterSpec: StringSpec({
186187
endpoint("/foo") {
187188
parameters {
188189
query("foo", ObjectDataType("Foo", "model", linkedMapOf(
189-
Pair("foo1", StringDataType()),
190-
Pair("foo2", StringDataType())
190+
Pair("foo1", propertyDataTypeString()),
191+
Pair("foo2", propertyDataTypeString())
191192
)))
192193
}
193194
responses { status("200") }

0 commit comments

Comments
 (0)