Skip to content

Commit 122988c

Browse files
committed
create model enum classes
1 parent 27c9d59 commit 122988c

4 files changed

Lines changed: 100 additions & 6 deletions

File tree

src/main/groovy/com/github/hauner/openapi/spring/model/DataTypes.groovy

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.github.hauner.openapi.spring.model
1818

1919
import com.github.hauner.openapi.spring.model.datatypes.DataType
2020
import com.github.hauner.openapi.spring.model.datatypes.ObjectDataType
21+
import com.github.hauner.openapi.spring.model.datatypes.StringEnumDataType
2122

2223
/**
2324
* Container for Java data types from OpenAPI '#/component/schemas'.
@@ -49,6 +50,18 @@ class DataTypes {
4950
} as List<ObjectDataType>
5051
}
5152

53+
/**
54+
* provides the enum data types (model classes) used by the api endpoints. For this object
55+
* the generatr will create enum classes.
56+
*
57+
* @return list of enum data types
58+
*/
59+
List<StringEnumDataType> getEnumDataTypes () {
60+
types.values ().findAll {
61+
it instanceof StringEnumDataType
62+
} as List<StringEnumDataType>
63+
}
64+
5265
void add (List<DataType> dataTypes) {
5366
dataTypes.each {
5467
types.put (it.name, it)

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.github.hauner.openapi.spring.writer
1919
import com.github.hauner.openapi.spring.converter.ApiOptions
2020
import com.github.hauner.openapi.spring.model.Api
2121
import com.github.hauner.openapi.spring.model.datatypes.ObjectDataType
22+
import com.github.hauner.openapi.spring.model.datatypes.StringEnumDataType
2223
import groovy.util.logging.Slf4j
2324

2425
/**
@@ -32,6 +33,7 @@ class ApiWriter {
3233
private ApiOptions options
3334
InterfaceWriter interfaceWriter
3435
DataTypeWriter dataTypeWriter
36+
StringEnumWriter enumWriter
3537

3638
File apiFolder
3739
File modelFolder
@@ -41,12 +43,17 @@ class ApiWriter {
4143
this.options = options
4244
this.interfaceWriter = interfaceWriter
4345
this.dataTypeWriter = new DataTypeWriter(headerWriter: new HeaderWriter ())
46+
this.enumWriter = new StringEnumWriter()
4447
}
4548

46-
ApiWriter(ApiOptions options, InterfaceWriter interfaceWriter, DataTypeWriter dataTypeWriter) {
49+
ApiWriter(ApiOptions options,
50+
InterfaceWriter interfaceWriter,
51+
DataTypeWriter dataTypeWriter,
52+
StringEnumWriter enumWriter) {
4753
this.options = options
4854
this.interfaceWriter = interfaceWriter
4955
this.dataTypeWriter = dataTypeWriter
56+
this.enumWriter = enumWriter
5057
}
5158

5259
void write(Api api) {
@@ -65,6 +72,13 @@ class ApiWriter {
6572
dataTypeWriter.write (writer, it as ObjectDataType)
6673
writer.close ()
6774
}
75+
76+
api.models.enumDataTypes.each {
77+
def target = new File (modelFolder, "${it.name}.java")
78+
def writer = new FileWriter(target)
79+
enumWriter.write (writer, it as StringEnumDataType)
80+
writer.close ()
81+
}
6882
}
6983

7084
private void createTargetFolders () {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2019 the original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.github.hauner.openapi.spring.writer
18+
19+
import com.github.hauner.openapi.spring.model.datatypes.StringEnumDataType
20+
21+
/**
22+
* Writer for String enum.
23+
*
24+
* @author Martin Hauner
25+
*/
26+
class StringEnumWriter {
27+
28+
void write (Writer target, StringEnumDataType dataType) {
29+
}
30+
31+
}

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

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.github.hauner.openapi.spring.model.Interface
2323
import com.github.hauner.openapi.spring.model.datatypes.MappedDataType
2424
import com.github.hauner.openapi.spring.model.datatypes.ObjectDataType
2525
import com.github.hauner.openapi.spring.model.datatypes.StringDataType
26+
import com.github.hauner.openapi.spring.model.datatypes.StringEnumDataType
2627
import com.github.hauner.openapi.spring.support.Sl4jMockRule
2728
import org.junit.Rule
2829
import org.junit.rules.TemporaryFolder
@@ -46,7 +47,7 @@ class ApiWriterSpec extends Specification {
4647
)
4748

4849
when:
49-
new ApiWriter (opts, Stub (InterfaceWriter)).write (new Api())
50+
new ApiWriter (opts, Stub (InterfaceWriter), null, null).write (new Api())
5051

5152
then:
5253
def api = new File([opts.targetDir, 'com', 'github', 'hauner', 'openapi', 'api'].join(File.separator))
@@ -66,7 +67,7 @@ class ApiWriterSpec extends Specification {
6667
when:
6768
target.newFolder ('java', 'src', 'com', 'github', 'hauner', 'openapi', 'api')
6869
target.newFolder ('java', 'src', 'com', 'github', 'hauner', 'openapi', 'model')
69-
new ApiWriter (opts, Stub (InterfaceWriter)).write (new Api())
70+
new ApiWriter (opts, Stub (InterfaceWriter), null, null).write (new Api())
7071

7172
then:
7273
0 * log.error (*_)
@@ -94,7 +95,7 @@ class ApiWriterSpec extends Specification {
9495
])
9596

9697
when:
97-
new ApiWriter (opts, interfaceWriter).write (api)
98+
new ApiWriter (opts, interfaceWriter, null, null).write (api)
9899

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

131132
when:
132-
new ApiWriter (opts, Stub(InterfaceWriter), dataTypeWriter).write (api)
133+
new ApiWriter (opts, Stub(InterfaceWriter), dataTypeWriter, Stub(StringEnumWriter)).write (api)
133134

134135
then:
135136
def fooSource = new File(getModelPath (opts.targetDir, 'Foo.java'))
@@ -142,6 +143,41 @@ Bar class!
142143
"""
143144
}
144145

146+
void "generates model enum sources in model target folder"() {
147+
def enumWriter = Stub (StringEnumWriter) {
148+
write (_ as Writer, _ as StringEnumDataType) >> {
149+
Writer writer = it.get(0)
150+
writer.write ('Foo enum!\n')
151+
} >> {
152+
Writer writer = it.get(0)
153+
writer.write ('Bar enum!\n')
154+
}
155+
}
156+
157+
def opts = new ApiOptions(
158+
packageName: 'com.github.hauner.openapi',
159+
targetDir: [target.root.toString (), 'java', 'src'].join (File.separator)
160+
)
161+
162+
def dt = new DataTypes()
163+
dt.add ('foo', new StringEnumDataType(pkg: "${opts.packageName}.model", type: 'foo'))
164+
dt.add (new StringEnumDataType(pkg: "${opts.packageName}.model", type: 'Bar'))
165+
def api = new Api(dt)
166+
167+
when:
168+
new ApiWriter (opts, Stub(InterfaceWriter), Stub(DataTypeWriter), enumWriter).write (api)
169+
170+
then:
171+
def fooSource = new File(getModelPath (opts.targetDir, 'Foo.java'))
172+
fooSource.text == """\
173+
Foo enum!
174+
"""
175+
def barSource = new File(getModelPath (opts.targetDir, 'Bar.java'))
176+
barSource.text == """\
177+
Bar enum!
178+
"""
179+
}
180+
145181
void "generates model for object data types only" () {
146182
def dataTypeWriter = Mock (DataTypeWriter) {
147183
write (_ as Writer, _ as ObjectDataType) >> {
@@ -166,7 +202,7 @@ Bar class!
166202
def api = new Api(dt)
167203

168204
when:
169-
new ApiWriter (opts, Stub(InterfaceWriter), dataTypeWriter).write (api)
205+
new ApiWriter (opts, Stub(InterfaceWriter), dataTypeWriter, Stub(StringEnumWriter)).write (api)
170206

171207
then:
172208
0 * dataTypeWriter.write (_, dt.find ('simple'))

0 commit comments

Comments
 (0)