Skip to content

Commit 4a6b732

Browse files
committed
re-format generated code
1 parent e03f74a commit 4a6b732

File tree

3 files changed

+154
-20
lines changed

3 files changed

+154
-20
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ dependencies {
3636
implementation 'org.codehaus.groovy:groovy:2.5.4'
3737
implementation 'io.swagger.parser.v3:swagger-parser:2.0.12'
3838
implementation 'org.yaml:snakeyaml:1.25'
39+
implementation 'com.google.googlejavaformat:google-java-format:1.7'
3940
compileOnly "com.github.hauner.openapi:openapi-generatr-api:$generatrApiVersion"
4041

4142
testImplementation 'net.bytebuddy:byte-buddy:1.9.13'

src/main/groovy/com/github/hauner/openapi/spring/writer/ApiWriter.groovy

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 the original authors
2+
* Copyright 2019-2020 the original authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,8 +18,11 @@ package com.github.hauner.openapi.spring.writer
1818

1919
import com.github.hauner.openapi.spring.converter.ApiOptions
2020
import com.github.hauner.openapi.spring.model.Api
21+
import com.github.hauner.openapi.spring.model.Interface
2122
import com.github.hauner.openapi.spring.model.datatypes.ObjectDataType
2223
import com.github.hauner.openapi.spring.model.datatypes.StringEnumDataType
24+
import com.google.googlejavaformat.java.Formatter
25+
import com.google.googlejavaformat.java.JavaFormatterOptions
2326
import groovy.util.logging.Slf4j
2427

2528
/**
@@ -38,22 +41,25 @@ class ApiWriter {
3841
File apiFolder
3942
File modelFolder
4043

41-
@Deprecated
42-
ApiWriter(ApiOptions options, InterfaceWriter interfaceWriter) {
43-
this.options = options
44-
this.interfaceWriter = interfaceWriter
45-
this.dataTypeWriter = new DataTypeWriter(headerWriter: new HeaderWriter ())
46-
this.enumWriter = new StringEnumWriter(headerWriter: new HeaderWriter ())
47-
}
44+
Formatter formatter
4845

49-
ApiWriter(ApiOptions options,
50-
InterfaceWriter interfaceWriter,
51-
DataTypeWriter dataTypeWriter,
52-
StringEnumWriter enumWriter) {
46+
ApiWriter (ApiOptions options,
47+
InterfaceWriter interfaceWriter,
48+
DataTypeWriter dataTypeWriter,
49+
StringEnumWriter enumWriter,
50+
boolean enableFormatter = true) {
5351
this.options = options
5452
this.interfaceWriter = interfaceWriter
5553
this.dataTypeWriter = dataTypeWriter
5654
this.enumWriter = enumWriter
55+
56+
if (enableFormatter) {
57+
formatter = new Formatter (
58+
JavaFormatterOptions
59+
.builder ()
60+
.style (JavaFormatterOptions.Style.AOSP)
61+
.build ())
62+
}
5763
}
5864

5965
void write(Api api) {
@@ -62,25 +68,60 @@ class ApiWriter {
6268
api.interfaces.each {
6369
def target = new File (apiFolder, "${it.interfaceName}.java")
6470
def writer = new FileWriter(target)
65-
interfaceWriter.write (writer, it)
71+
writeInterface (writer, it)
6672
writer.close ()
6773
}
6874

6975
api.models.objectDataTypes.each {
7076
def target = new File (modelFolder, "${it.name}.java")
7177
def writer = new FileWriter(target)
72-
dataTypeWriter.write (writer, it as ObjectDataType)
78+
writeDataType (writer, it)
7379
writer.close ()
7480
}
7581

7682
api.models.enumDataTypes.each {
7783
def target = new File (modelFolder, "${it.name}.java")
7884
def writer = new FileWriter(target)
79-
enumWriter.write (writer, it as StringEnumDataType)
85+
writeEnumDataType (writer, it)
8086
writer.close ()
8187
}
8288
}
8389

90+
private void writeInterface (Writer writer, Interface itf) {
91+
def raw = new StringWriter ()
92+
interfaceWriter.write (raw, itf)
93+
writer.write (format (raw.toString ()))
94+
}
95+
96+
private void writeDataType (Writer writer, ObjectDataType dataType) {
97+
def raw = new StringWriter ()
98+
dataTypeWriter.write (raw, dataType)
99+
writer.write (format (raw.toString ()))
100+
}
101+
102+
private void writeEnumDataType (Writer writer, StringEnumDataType enumDataType) {
103+
def raw = new StringWriter ()
104+
enumWriter.write (raw, enumDataType)
105+
writer.write (format (raw.toString ()))
106+
}
107+
108+
private String format (String raw) {
109+
if (formatter == null) {
110+
return raw
111+
}
112+
correctLineFeed (formatter.formatSource (raw))
113+
}
114+
115+
private String correctLineFeed (String formatted) {
116+
int index = formatted.findLastIndexOf (0) {
117+
it == '}'
118+
}
119+
120+
new StringBuilder ()
121+
.append (formatted.substring (0, index))
122+
.append ("\n}\n")
123+
}
124+
84125
private void createTargetFolders () {
85126
def rootPkg = options.packageName.replace ('.', File.separator)
86127
def apiPkg = [rootPkg, 'api'].join (File.separator)

src/test/groovy/com/github/hauner/openapi/spring/writer/ApiWriterSpec.groovy

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 the original authors
2+
* Copyright 2019-2020 the original authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -95,7 +95,8 @@ class ApiWriterSpec extends Specification {
9595
])
9696

9797
when:
98-
new ApiWriter (opts, interfaceWriter, null, null).write (api)
98+
new ApiWriter (opts, interfaceWriter, null, null, false)
99+
.write (api)
99100

100101
then:
101102
def fooSource = new File(getApiPath (opts.targetDir, 'FooApi.java'))
@@ -130,7 +131,8 @@ Bar interface!
130131
def api = new Api(dt)
131132

132133
when:
133-
new ApiWriter (opts, Stub(InterfaceWriter), dataTypeWriter, Stub(StringEnumWriter)).write (api)
134+
new ApiWriter (opts, Stub(InterfaceWriter), dataTypeWriter, Stub(StringEnumWriter), false)
135+
.write (api)
134136

135137
then:
136138
def fooSource = new File(getModelPath (opts.targetDir, 'Foo.java'))
@@ -165,7 +167,8 @@ Bar class!
165167
def api = new Api(dt)
166168

167169
when:
168-
new ApiWriter (opts, Stub(InterfaceWriter), Stub(DataTypeWriter), enumWriter).write (api)
170+
new ApiWriter (opts, Stub(InterfaceWriter), Stub(DataTypeWriter), enumWriter, false)
171+
.write (api)
169172

170173
then:
171174
def fooSource = new File(getModelPath (opts.targetDir, 'Foo.java'))
@@ -202,18 +205,107 @@ Bar enum!
202205
def api = new Api(dt)
203206

204207
when:
205-
new ApiWriter (opts, Stub(InterfaceWriter), dataTypeWriter, Stub(StringEnumWriter)).write (api)
208+
new ApiWriter (opts, Stub(InterfaceWriter), dataTypeWriter, Stub(StringEnumWriter), false)
209+
.write (api)
206210

207211
then:
208212
0 * dataTypeWriter.write (_, dt.find ('simple'))
209213
0 * dataTypeWriter.write (_, dt.find ('Type'))
210214
}
211215

216+
void "re-formats interface sources"() {
217+
def interfaceWriter = Stub (InterfaceWriter) {
218+
write (_ as Writer, _ as Interface) >> {
219+
Writer writer = it.get(0)
220+
writer.write (' interface Foo { }\n')
221+
}
222+
}
223+
224+
def opts = new ApiOptions(
225+
packageName: 'com.github.hauner.openapi',
226+
targetDir: [target.root.toString (), 'java', 'src'].join (File.separator)
227+
)
228+
229+
def api = new Api(interfaces: [
230+
new Interface(pkg: "${opts.packageName}.api", name: 'Foo')
231+
])
232+
233+
when:
234+
new ApiWriter (opts, interfaceWriter, null, null)
235+
.write (api)
236+
237+
then:
238+
def fooSource = new File(getApiPath (opts.targetDir, 'FooApi.java'))
239+
fooSource.text == """\
240+
interface Foo {
241+
}
242+
"""
243+
}
244+
245+
void "re-formats model sources"() {
246+
def dataTypeWriter = Stub (DataTypeWriter) {
247+
write (_ as Writer, _ as ObjectDataType) >> {
248+
Writer writer = it.get(0)
249+
writer.write (' class Foo { }')
250+
}
251+
}
252+
253+
def opts = new ApiOptions(
254+
packageName: 'com.github.hauner.openapi',
255+
targetDir: [target.root.toString (), 'java', 'src'].join (File.separator)
256+
)
257+
258+
def dt = new DataTypes()
259+
dt.add (new ObjectDataType(pkg: "${opts.packageName}.model", type: 'Foo'))
260+
def api = new Api(dt)
261+
262+
when:
263+
new ApiWriter (opts, Stub(InterfaceWriter), dataTypeWriter, Stub(StringEnumWriter))
264+
.write (api)
265+
266+
then:
267+
def fooSource = new File(getModelPath (opts.targetDir, 'Foo.java'))
268+
fooSource.text == """\
269+
class Foo {
270+
}
271+
"""
272+
}
273+
274+
void "re-formats model enum sources"() {
275+
def enumWriter = Stub (StringEnumWriter) {
276+
write (_ as Writer, _ as StringEnumDataType) >> {
277+
Writer writer = it.get(0)
278+
writer.write (' enum Foo { }')
279+
}
280+
}
281+
282+
def opts = new ApiOptions(
283+
packageName: 'com.github.hauner.openapi',
284+
targetDir: [target.root.toString (), 'java', 'src'].join (File.separator)
285+
)
286+
287+
def dt = new DataTypes()
288+
dt.add (new StringEnumDataType(pkg: "${opts.packageName}.model", type: 'Foo'))
289+
def api = new Api(dt)
290+
291+
when:
292+
new ApiWriter (opts, Stub(InterfaceWriter), Stub(DataTypeWriter), enumWriter)
293+
.write (api)
294+
295+
then:
296+
def fooSource = new File(getModelPath (opts.targetDir, 'Foo.java'))
297+
fooSource.text == """\
298+
enum Foo {
299+
}
300+
"""
301+
}
302+
212303
String getApiPath(String targetFolder, String clazzName) {
213304
([targetFolder] + apiPkgPath + [clazzName]).join(File.separator)
214305
}
215306

216307
String getModelPath(String targetFolder, String clazzName) {
217308
([targetFolder] + apiModelPath + [clazzName]).join(File.separator)
218309
}
310+
219311
}

0 commit comments

Comments
 (0)