Skip to content

Commit f0cc1ee

Browse files
committed
write string enum class
1 parent a2fab31 commit f0cc1ee

5 files changed

Lines changed: 213 additions & 4 deletions

File tree

src/main/groovy/com/github/hauner/openapi/spring/generatr/SpringGeneratr.groovy

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ import com.github.hauner.openapi.api.OpenApiGeneratr
2020
import com.github.hauner.openapi.spring.converter.ApiConverter
2121
import com.github.hauner.openapi.spring.converter.ApiOptions
2222
import com.github.hauner.openapi.spring.writer.ApiWriter
23+
import com.github.hauner.openapi.spring.writer.DataTypeWriter
2324
import com.github.hauner.openapi.spring.writer.HeaderWriter
2425
import com.github.hauner.openapi.spring.writer.InterfaceWriter
2526
import com.github.hauner.openapi.spring.writer.MethodWriter
27+
import com.github.hauner.openapi.spring.writer.StringEnumWriter
2628
import io.swagger.v3.parser.OpenAPIV3Parser
2729
import io.swagger.v3.parser.core.models.ParseOptions
2830
import io.swagger.v3.parser.core.models.SwaggerParseResult
@@ -63,10 +65,13 @@ class SpringGeneratr implements OpenApiGeneratr<SpringGeneratrOptions> {
6365
def cv = new ApiConverter(options)
6466
def api = cv.convert (result.openAPI)
6567

68+
def headerWriter = new HeaderWriter()
6669
def writer = new ApiWriter (options,
6770
new InterfaceWriter(
68-
headerWriter: new HeaderWriter(),
69-
methodWriter: new MethodWriter())
71+
headerWriter: headerWriter,
72+
methodWriter: new MethodWriter()),
73+
new DataTypeWriter(headerWriter: headerWriter),
74+
new StringEnumWriter(headerWriter: headerWriter)
7075
)
7176

7277
writer.write (api)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ApiWriter {
4343
this.options = options
4444
this.interfaceWriter = interfaceWriter
4545
this.dataTypeWriter = new DataTypeWriter(headerWriter: new HeaderWriter ())
46-
this.enumWriter = new StringEnumWriter()
46+
this.enumWriter = new StringEnumWriter(headerWriter: new HeaderWriter ())
4747
}
4848

4949
ApiWriter(ApiOptions options,

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.github.hauner.openapi.spring.writer
1818

1919
import com.github.hauner.openapi.spring.model.datatypes.StringEnumDataType
20+
import com.github.hauner.openapi.support.Identifier
2021

2122
/**
2223
* Writer for String enum.
@@ -25,7 +26,50 @@ import com.github.hauner.openapi.spring.model.datatypes.StringEnumDataType
2526
*/
2627
class StringEnumWriter {
2728

29+
HeaderWriter headerWriter
30+
2831
void write (Writer target, StringEnumDataType dataType) {
32+
headerWriter.write (target)
33+
target.write ("package ${dataType.packageName};\n\n")
34+
35+
target.write ("public enum ${dataType.type} {\n\n")
36+
37+
def values = []
38+
dataType.values.each {
39+
values.add (" ${Identifier.toEnum (it)}(\"${it}\")")
40+
}
41+
target.write (values.join (",\n") + ";\n\n")
42+
target.write(" private final String value;\n\n")
43+
44+
target.write ("""\
45+
private ${dataType.type}(String value) {
46+
this.value = value;
47+
}
48+
49+
""")
50+
51+
target.write("""\
52+
@JsonValue
53+
public String getValue() {
54+
return this.value;
55+
}
56+
57+
""")
58+
59+
target.write("""\
60+
@JsonCreator
61+
public static ${dataType.type} fromValue(String value) {
62+
for (${dataType.type} val: ${dataType.type}.values()) {
63+
if (val.value.equals(value)) {
64+
return val;
65+
}
66+
}
67+
throw new IllegalArgumentException(value);
68+
}
69+
70+
""")
71+
72+
target.write ("}\n")
2973
}
3074

3175
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package com.github.hauner.openapi.spring.writer
1919
import com.github.hauner.openapi.spring.model.datatypes.ListDataType
2020
import com.github.hauner.openapi.spring.model.datatypes.ObjectDataType
2121
import com.github.hauner.openapi.spring.model.datatypes.StringDataType
22-
import spock.lang.Ignore
2322
import spock.lang.Specification
2423

2524
import static com.github.hauner.openapi.spring.support.AssertHelper.extractImports
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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+
import spock.lang.Specification
21+
22+
class StringEnumWriterSpec extends Specification {
23+
def headerWriter = Mock HeaderWriter
24+
25+
def writer = new StringEnumWriter(headerWriter: headerWriter)
26+
def target = new StringWriter ()
27+
28+
void "writes 'generated' comment" () {
29+
def dataType = new StringEnumDataType(type: 'Foo', values: [])
30+
31+
when:
32+
writer.write (target, dataType)
33+
34+
then:
35+
1 * headerWriter.write (target)
36+
}
37+
38+
void "writes 'package'" () {
39+
def pkg = 'com.github.hauner.openapi'
40+
def dataType = new StringEnumDataType(type: 'Foo', values: [], pkg: pkg)
41+
42+
when:
43+
writer.write (target, dataType)
44+
45+
then:
46+
target.toString ().contains ("""\
47+
package $pkg;
48+
49+
""")
50+
}
51+
52+
void "writes enum class"() {
53+
def pkg = 'com.github.hauner.openapi'
54+
def dataType = new StringEnumDataType(type: 'Foo', values: [], pkg: pkg)
55+
56+
when:
57+
writer.write (target, dataType)
58+
59+
then:
60+
target.toString ().contains ("""\
61+
public enum Foo {
62+
""")
63+
target.toString ().contains ("""\
64+
}
65+
""")
66+
}
67+
68+
void "writes enum values"() {
69+
def pkg = 'com.github.hauner.openapi'
70+
def dataType = new StringEnumDataType(type: 'Foo', values: ['foo', '_foo-2', 'foo-foo'], pkg: pkg)
71+
72+
when:
73+
writer.write (target, dataType)
74+
75+
then:
76+
target.toString ().contains ("""\
77+
public enum Foo {
78+
79+
FOO("foo"),
80+
FOO_2("_foo-2"),
81+
FOO_FOO("foo-foo");
82+
83+
""")
84+
}
85+
86+
void "writes value member"() {
87+
def pkg = 'com.github.hauner.openapi'
88+
def dataType = new StringEnumDataType(type: 'Foo', values: ['foo', '_foo-2', 'foo-foo'], pkg: pkg)
89+
90+
when:
91+
writer.write (target, dataType)
92+
93+
then:
94+
target.toString ().contains ("""\
95+
private final String value;
96+
97+
""")
98+
}
99+
100+
void "writes enum constructor"() {
101+
def pkg = 'com.github.hauner.openapi'
102+
def dataType = new StringEnumDataType (type: 'Foo', values: ['foo', '_foo-2', 'foo-foo'], pkg: pkg)
103+
104+
when:
105+
writer.write (target, dataType)
106+
107+
then:
108+
target.toString ().contains ("""\
109+
110+
private Foo(String value) {
111+
this.value = value;
112+
}
113+
114+
""")
115+
}
116+
117+
void "writes @JsonValue method for serialization"() {
118+
def pkg = 'com.github.hauner.openapi'
119+
def dataType = new StringEnumDataType (type: 'Foo', values: ['foo', '_foo-2', 'foo-foo'], pkg: pkg)
120+
121+
when:
122+
writer.write (target, dataType)
123+
124+
then:
125+
target.toString ().contains ("""\
126+
127+
@JsonValue
128+
public String getValue() {
129+
return this.value;
130+
}
131+
132+
""")
133+
}
134+
135+
void "writes @JsonCreator method for de-serialization"() {
136+
def pkg = 'com.github.hauner.openapi'
137+
def dataType = new StringEnumDataType (type: 'Foo', values: ['foo', '_foo-2', 'foo-foo'], pkg: pkg)
138+
139+
when:
140+
writer.write (target, dataType)
141+
142+
then:
143+
target.toString ().contains ("""\
144+
145+
@JsonCreator
146+
public static Foo fromValue(String value) {
147+
for (Foo val: Foo.values()) {
148+
if (val.value.equals(value)) {
149+
return val;
150+
}
151+
}
152+
throw new IllegalArgumentException(value);
153+
}
154+
155+
""")
156+
}
157+
158+
}
159+
160+
161+

0 commit comments

Comments
 (0)