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

Commit 4ec9003

Browse files
committed
handle format-code option
1 parent 7fbd090 commit 4ec9003

4 files changed

Lines changed: 36 additions & 15 deletions

File tree

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ class ApiWriter(
2929
private val options: ApiOptions,
3030
private val interfaceWriter: InterfaceWriter,
3131
private val dataTypeWriter: DataTypeWriter,
32-
private val enumWriter: StringEnumWriter,
33-
private val enableFormatter: Boolean = true
32+
private val enumWriter: StringEnumWriter
3433
) {
3534
private var log: Logger = LoggerFactory.getLogger(this.javaClass.name)
3635

@@ -140,7 +139,7 @@ class ApiWriter(
140139
}
141140

142141
private fun initFormatter() {
143-
if (enableFormatter) {
142+
if (options.formatCode) {
144143
formatter = Formatter(
145144
JavaFormatterOptions
146145
.builder()

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

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ class ApiWriterSpec: StringSpec({
6262
val api = Api(dataTypes = dts)
6363

6464
// when:
65-
ApiWriter(options, stub(), stub(), enumWriter, false)
65+
options.formatCode = false
66+
ApiWriter(options, stub(), stub(), enumWriter)
6667
.write(api)
6768

6869
// then:
@@ -83,7 +84,7 @@ class ApiWriterSpec: StringSpec({
8384
val api = Api(dataTypes = dts)
8485

8586
// when:
86-
ApiWriter(options, stub(), stub(), enumWriter, true)
87+
ApiWriter(options, stub(), stub(), enumWriter)
8788
.write(api)
8889

8990
// then:
@@ -132,7 +133,8 @@ class ApiWriterSpec: StringSpec({
132133
))
133134

134135
// when:
135-
ApiWriter(options, itfWriter, stub(), stub(), false)
136+
options.formatCode = false
137+
ApiWriter(options, itfWriter, stub(), stub())
136138
.write (api)
137139

138140
// then:
@@ -151,7 +153,8 @@ class ApiWriterSpec: StringSpec({
151153
))
152154

153155
// when:
154-
ApiWriter(options, itfWriter, stub(), stub(), false)
156+
options.formatCode = false
157+
ApiWriter(options, itfWriter, stub(), stub())
155158
.write (api)
156159

157160
// then:
@@ -171,7 +174,8 @@ class ApiWriterSpec: StringSpec({
171174
val api = Api(dataTypes = dts)
172175

173176
// when:
174-
ApiWriter(options, stub(), dtWriter, stub(), false)
177+
options.formatCode = false
178+
ApiWriter(options, stub(), dtWriter, stub())
175179
.write(api)
176180

177181
// then:
@@ -188,7 +192,7 @@ class ApiWriterSpec: StringSpec({
188192
val api = Api(dataTypes = dt)
189193

190194
// when:
191-
ApiWriter(options, stub(), dtWriter, stub(), false)
195+
ApiWriter(options, stub(), dtWriter, stub())
192196
.write (api)
193197

194198
// then:
@@ -204,7 +208,7 @@ class ApiWriterSpec: StringSpec({
204208
}
205209

206210
// when:
207-
ApiWriter(options, itfWriter, stub(), stub(), true)
211+
ApiWriter(options, itfWriter, stub(), stub())
208212
.write (Api(listOf(
209213
`interface`("Foo", options.getSourceDir("api").toString()) {}
210214
)))
@@ -229,7 +233,7 @@ class ApiWriterSpec: StringSpec({
229233
val api = Api(dataTypes = dt)
230234

231235
// when:
232-
ApiWriter(options, stub(), dtWriter, stub(), true)
236+
ApiWriter(options, stub(), dtWriter, stub())
233237
.write (api)
234238

235239
// then:
@@ -240,6 +244,26 @@ class ApiWriterSpec: StringSpec({
240244
""".trimMargin()
241245
}
242246

247+
"does not re-format sources if code formatting is disabled" {
248+
val itfWriter = io.mockk.mockk<InterfaceWriter>()
249+
every { itfWriter.write(any(), any()) } answers {
250+
arg<Writer>(0).write(" interface \n ${arg<Interface>(1).name} { }\n")
251+
}
252+
253+
// when:
254+
options.formatCode = false
255+
ApiWriter(options, itfWriter, stub(), stub())
256+
.write (Api(listOf(
257+
`interface`("Foo", options.getSourceDir("api").toString()) {}
258+
)))
259+
260+
// then:
261+
textOfApi("FooApi.java") shouldBe """
262+
| interface
263+
| Foo { }
264+
|
265+
""".trimMargin()
266+
}
243267
})
244268

245269

src/testInt/groovy/io/openapiprocessor/core/test/TestProcessor.groovy

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ class TestProcessor implements OpenApiProcessor {
7272
beanValidationFactory,
7373
new JavaDocWriter()
7474
),
75-
new StringEnumWriter(headerWriter),
76-
true
75+
new StringEnumWriter(headerWriter)
7776
)
7877

7978
writer.write (api)

src/testInt/kotlin/io/openapiprocessor/core/TestProcessor.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ class TestProcessor: OpenApiProcessor {
6262
beanValidation,
6363
JavaDocWriter()
6464
),
65-
StringEnumWriter(headerWriter),
66-
true
65+
StringEnumWriter(headerWriter)
6766
)
6867

6968
writer.write(api)

0 commit comments

Comments
 (0)