Skip to content

Commit 6867324

Browse files
committed
create response interfaces (#247)
1 parent 42b6702 commit 6867324

File tree

8 files changed

+374
-10
lines changed

8 files changed

+374
-10
lines changed

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/converter/DataTypeConverter.kt

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,46 @@ class DataTypeConverter(
217217
}
218218

219219
val found = dataTypes.find(schemaInfo.getName())
220-
if (found != null) {
220+
if (found != null && found is ObjectDataType) {
221+
if (schemaInfo.withInterface()) {
222+
return createInterface(schemaInfo, found, dataTypes)
223+
}
224+
221225
return found
222226
}
223227

224228
dataTypes.add (schemaInfo.getName(), objectType)
225229
return objectType
226230
}
227231

232+
private fun createInterface(schemaInfo: SchemaInfo, objectType: ObjectDataType, dataTypes: DataTypes): DataType {
233+
val constraints = DataTypeConstraints(
234+
nullable = schemaInfo.getNullable(),
235+
required = schemaInfo.getRequired())
236+
237+
val interfaceName = schemaInfo.interfaceName!!
238+
val interfaceType = InterfaceDataType(
239+
DataTypeName(interfaceName, getTypeNameWithSuffix2(interfaceName)),
240+
listOf(options.packageName, "model").joinToString("."),
241+
emptyList(),
242+
constraints,
243+
schemaInfo.getDeprecated(),
244+
Documentation(description = schemaInfo.description)
245+
)
246+
247+
val found = dataTypes.find(interfaceType.getName())
248+
if (found != null && found is InterfaceDataType) {
249+
found.addItem(objectType)
250+
objectType.addInterface(found)
251+
return found
252+
}
253+
254+
interfaceType.addItem(objectType)
255+
objectType.addInterface(interfaceType)
256+
dataTypes.add (interfaceType.getName(), interfaceType)
257+
return interfaceType
258+
}
259+
228260
private fun createObjectDataType(
229261
schemaInfo: SchemaInfo,
230262
properties: LinkedHashMap<String, PropertyDataType>
@@ -525,4 +557,8 @@ class DataTypeConverter(
525557
private fun getTypeNameWithSuffix(name: String): String {
526558
return identifier.toClass(name, options.modelNameSuffix)
527559
}
560+
561+
private fun getTypeNameWithSuffix2(name: String): String {
562+
return name + options.modelNameSuffix
563+
}
528564
}

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/converter/SchemaInfo.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ open class SchemaInfo(
353353
resolver = resolver)
354354
}
355355

356+
val interfaceName: String? = contentTypeInterfaceName
357+
356358
/**
357359
* all constraints
358360
*/

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/model/DataTypes.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,19 @@ class DataTypes {
129129
val size: Int
130130
get() = dataTypeInfos.size
131131

132+
/**
133+
* copy the data types. The data types itself are not copied but referenced. The ref count is not copied.
134+
*/
135+
fun copy(): DataTypes {
136+
val copy = DataTypes()
137+
138+
for ((k, v) in dataTypeInfos) {
139+
copy.dataTypeInfos.put(k, DataTypeInfo(v.dataType))
140+
}
141+
142+
return copy
143+
}
144+
132145
/**
133146
* test only. Provides all named data types (including simple data types) used by the api endpoints.
134147
*

openapi-processor-core/src/test/groovy/io/openapiprocessor/core/converter/DataTypeConverterDeprecatedSpec.groovy

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ class DataTypeConverterDeprecatedSpec extends Specification {
4242
"",
4343
schema,
4444
Stub(RefResolver),
45-
""),
45+
"",
46+
false,
47+
null),
4648
new DataTypes())
4749
4850
then:

openapi-processor-core/src/test/groovy/io/openapiprocessor/core/converter/DataTypeConverterSpec.groovy

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ class DataTypeConverterSpec extends Specification {
4242
"",
4343
schema,
4444
Stub(RefResolver),
45-
""),
45+
"",
46+
false,
47+
null),
4648
new DataTypes())
4749
4850
then:
@@ -74,7 +76,9 @@ class DataTypeConverterSpec extends Specification {
7476
"",
7577
schema,
7678
Stub(RefResolver),
77-
""),
79+
"",
80+
false,
81+
null),
7882
new DataTypes())
7983
8084
then:
@@ -103,7 +107,9 @@ class DataTypeConverterSpec extends Specification {
103107
"",
104108
schema,
105109
Stub (RefResolver),
106-
""),
110+
"",
111+
false,
112+
null),
107113
new DataTypes())
108114
109115
then:
@@ -143,7 +149,9 @@ class DataTypeConverterSpec extends Specification {
143149
"",
144150
barSchema,
145151
Stub(RefResolver),
146-
""),
152+
"",
153+
false,
154+
null),
147155
dt)
148156
converter.convert (
149157
new SchemaInfo (
@@ -152,7 +160,9 @@ class DataTypeConverterSpec extends Specification {
152160
"",
153161
fooSchema,
154162
resolver,
155-
""),
163+
"",
164+
false,
165+
null),
156166
dt)
157167
158168
then:

0 commit comments

Comments
 (0)