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

Commit 01553ad

Browse files
committed
add suffix to data type, #65
1 parent a0b2a2f commit 01553ad

File tree

3 files changed

+244
-4
lines changed

3 files changed

+244
-4
lines changed

src/main/kotlin/io/openapiprocessor/core/converter/DataTypeConverter.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import io.openapiprocessor.core.model.DataTypeCollector
1111
import io.openapiprocessor.core.model.DataTypes
1212
import io.openapiprocessor.core.model.Documentation
1313
import io.openapiprocessor.core.model.datatypes.*
14+
import io.openapiprocessor.core.writer.java.ModelClassNameCreator
1415
import java.util.*
1516
import kotlin.collections.LinkedHashMap
1617

@@ -199,15 +200,15 @@ class DataTypeConverter(
199200
)
200201

201202
val objectType = ObjectDataType (
202-
schemaInfo.getName(),
203+
getNameWithSuffix(schemaInfo.getName()),
203204
listOf(options.packageName, "model").joinToString("."),
204205
properties = properties,
205206
constraints = constraints,
206207
deprecated = schemaInfo.getDeprecated(),
207208
documentation = Documentation(description = schemaInfo.description)
208209
)
209210

210-
dataTypes.add (objectType)
211+
dataTypes.add (schemaInfo.getName(), objectType)
211212
return objectType
212213
}
213214

@@ -318,13 +319,13 @@ class DataTypeConverter(
318319
}
319320

320321
val enumType = StringEnumDataType (
321-
enumName,
322+
getNameWithSuffix(enumName),
322323
listOf(options.packageName, "model").joinToString("."),
323324
schemaInfo.getEnumValues() as List<String>,
324325
constraints,
325326
schemaInfo.getDeprecated())
326327

327-
dataTypes.add (enumType)
328+
dataTypes.add (enumName, enumType)
328329
return enumType
329330
}
330331

@@ -394,4 +395,8 @@ class DataTypeConverter(
394395
return found != null
395396
}
396397

398+
private fun getNameWithSuffix(name: String): String {
399+
return ModelClassNameCreator(options.modelNameSuffix).createName(name)
400+
}
401+
397402
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.writer.java
7+
8+
/**
9+
* create a class name, possibly adding a suffix if any is configured.
10+
*/
11+
class ModelClassNameCreator(private val suffix: String) {
12+
13+
fun createName(dataTypeName: String): String {
14+
if (dataTypeName.isEmpty())
15+
return dataTypeName
16+
17+
if (dataTypeName.endsWith(suffix.capitalize()))
18+
return dataTypeName
19+
20+
return "$dataTypeName${suffix.capitalize()}"
21+
}
22+
23+
}
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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.converter
7+
8+
import io.kotest.core.spec.style.StringSpec
9+
import io.kotest.matchers.shouldBe
10+
import io.kotest.matchers.types.shouldBeInstanceOf
11+
import io.kotest.matchers.types.shouldBeSameInstanceAs
12+
import io.openapiprocessor.core.model.DataTypes
13+
import io.openapiprocessor.core.model.HttpMethod
14+
import io.openapiprocessor.core.model.datatypes.ObjectDataType
15+
import io.openapiprocessor.core.model.datatypes.StringEnumDataType
16+
import io.openapiprocessor.core.support.getBodySchemaInfo
17+
import io.openapiprocessor.core.support.parse
18+
19+
class DataTypeConverterSuffixSpec: StringSpec({
20+
val dataTypes = DataTypes()
21+
22+
"adds suffix to model data type name" {
23+
val options = ApiOptions()
24+
options.modelNameSuffix = "Suffix"
25+
26+
val openApi = parse("""
27+
openapi: 3.0.2
28+
info:
29+
title: API
30+
version: 1.0.0
31+
32+
paths:
33+
/foo:
34+
post:
35+
requestBody:
36+
content:
37+
application/json:
38+
schema:
39+
${'$'}ref: '#/components/schemas/Foo'
40+
responses:
41+
'204':
42+
description: empty
43+
44+
components:
45+
schemas:
46+
47+
Foo:
48+
description: a Foo
49+
type: object
50+
properties:
51+
foo:
52+
type: string
53+
54+
""".trimIndent())
55+
56+
val schemaInfo = openApi.getBodySchemaInfo("Foo",
57+
"/foo", HttpMethod.POST, "application/json")
58+
59+
// when:
60+
val converter = DataTypeConverter(options)
61+
val datatype = converter.convert(schemaInfo, dataTypes)
62+
63+
// then:
64+
dataTypes.find("Foo") shouldBeSameInstanceAs datatype
65+
66+
datatype.shouldBeInstanceOf<ObjectDataType>()
67+
datatype.getName().shouldBe("FooSuffix")
68+
}
69+
70+
"ignores suffix if model data type name already ends with the suffix" {
71+
val options = ApiOptions()
72+
options.modelNameSuffix = "Suffix"
73+
74+
val openApi = parse("""
75+
openapi: 3.0.2
76+
info:
77+
title: API
78+
version: 1.0.0
79+
80+
paths:
81+
/foo:
82+
post:
83+
requestBody:
84+
content:
85+
application/json:
86+
schema:
87+
${'$'}ref: '#/components/schemas/FooWithSuffix'
88+
responses:
89+
'204':
90+
description: empty
91+
92+
components:
93+
schemas:
94+
95+
FooWithSuffix:
96+
description: a Foo
97+
type: object
98+
properties:
99+
foo:
100+
type: string
101+
102+
""".trimIndent())
103+
104+
val schemaInfo = openApi.getBodySchemaInfo("FooWithSuffix",
105+
"/foo", HttpMethod.POST, "application/json")
106+
107+
// when:
108+
val converter = DataTypeConverter(options)
109+
val datatype = converter.convert(schemaInfo, dataTypes)
110+
111+
// then:
112+
dataTypes.find("FooWithSuffix") shouldBeSameInstanceAs datatype
113+
114+
datatype.shouldBeInstanceOf<ObjectDataType>()
115+
datatype.getName().shouldBe("FooWithSuffix")
116+
}
117+
118+
"adds suffix to model enum data type name" {
119+
val options = ApiOptions()
120+
options.modelNameSuffix = "Suffix"
121+
122+
val openApi = parse("""
123+
openapi: 3.0.2
124+
info:
125+
title: API
126+
version: 1.0.0
127+
128+
paths:
129+
/foo:
130+
post:
131+
requestBody:
132+
content:
133+
application/json:
134+
schema:
135+
${'$'}ref: '#/components/schemas/Bar'
136+
responses:
137+
'204':
138+
description: empty
139+
140+
components:
141+
schemas:
142+
143+
Bar:
144+
type: string
145+
enum:
146+
- bar-1
147+
- bar-2
148+
149+
""".trimIndent())
150+
151+
val schemaInfo = openApi.getBodySchemaInfo("Bar",
152+
"/foo", HttpMethod.POST, "application/json")
153+
154+
// when:
155+
val converter = DataTypeConverter(options)
156+
val datatype = converter.convert(schemaInfo, dataTypes)
157+
158+
// then:
159+
dataTypes.find("Bar") shouldBeSameInstanceAs datatype
160+
161+
datatype.shouldBeInstanceOf<StringEnumDataType>()
162+
datatype.getName().shouldBe("BarSuffix")
163+
}
164+
165+
"ignores suffix if model enum data type name already ends with the suffix" {
166+
val options = ApiOptions()
167+
options.modelNameSuffix = "Suffix"
168+
169+
val openApi = parse("""
170+
openapi: 3.0.2
171+
info:
172+
title: API
173+
version: 1.0.0
174+
175+
paths:
176+
/foo:
177+
post:
178+
requestBody:
179+
content:
180+
application/json:
181+
schema:
182+
${'$'}ref: '#/components/schemas/BarWithSuffix'
183+
responses:
184+
'204':
185+
description: empty
186+
187+
components:
188+
schemas:
189+
190+
BarWithSuffix:
191+
type: string
192+
enum:
193+
- bar-1
194+
- bar-2
195+
196+
""".trimIndent())
197+
198+
val schemaInfo = openApi.getBodySchemaInfo("BarWithSuffix",
199+
"/foo", HttpMethod.POST, "application/json")
200+
201+
// when:
202+
val converter = DataTypeConverter(options)
203+
val datatype = converter.convert(schemaInfo, dataTypes)
204+
205+
// then:
206+
dataTypes.find("BarWithSuffix") shouldBeSameInstanceAs datatype
207+
208+
datatype.shouldBeInstanceOf<StringEnumDataType>()
209+
datatype.getName().shouldBe("BarWithSuffix")
210+
}
211+
212+
})

0 commit comments

Comments
 (0)