File tree Expand file tree Collapse file tree 8 files changed +164
-0
lines changed
src/testInt/resources/tests/params-enum Expand file tree Collapse file tree 8 files changed +164
-0
lines changed Original file line number Diff line number Diff line change 1+ items :
2+ - inputs/openapi30.yaml
3+ - inputs/openapi31.yaml
4+ - inputs/mapping.yaml
Original file line number Diff line number Diff line change 1+ openapi-processor-mapping : v5
2+
3+ options :
4+ package-name : generated
5+ format-code : false
6+ enum-type : framework
Original file line number Diff line number Diff line change 1+ openapi : 3.0.3
2+ info :
3+ title : test enum parameters
4+ version : 1.0.0
5+
6+ paths :
7+
8+ /endpoint :
9+ get :
10+ tags :
11+ - enum
12+ parameters :
13+ - name : foo
14+ description : enum parameter
15+ in : query
16+ required : true
17+ schema :
18+ $ref : ' #/components/schemas/Foo'
19+ responses :
20+ ' 204 ' :
21+ description : empty
22+
23+ components :
24+ schemas :
25+
26+ Foo :
27+ type : string
28+ enum :
29+ - foo
30+ - foo-2
31+ - foo-foo
Original file line number Diff line number Diff line change 1+ openapi : 3.1.0
2+ info :
3+ title : test enum parameters
4+ version : 1.0.0
5+
6+ paths :
7+
8+ /endpoint :
9+ get :
10+ tags :
11+ - enum
12+ parameters :
13+ - name : foo
14+ description : enum parameter
15+ in : query
16+ required : true
17+ schema :
18+ $ref : ' #/components/schemas/Foo'
19+ responses :
20+ ' 204 ' :
21+ description : empty
22+
23+ components :
24+ schemas :
25+
26+ Foo :
27+ type : string
28+ enum :
29+ - foo
30+ - foo-2
31+ - foo-foo
Original file line number Diff line number Diff line change 1+ items :
2+ - outputs/api/EnumApi.java
3+ - outputs/<model>/Foo.java
4+ - outputs/spring/StringToEnumConverterFactory.java
Original file line number Diff line number Diff line change 1+ package generated .api ;
2+
3+ import generated .model .Foo ;
4+ import generated .support .Generated ;
5+ import org .springframework .web .bind .annotation .GetMapping ;
6+ import org .springframework .web .bind .annotation .RequestParam ;
7+
8+ @ Generated (value = "openapi-processor-spring" , version = "test" )
9+ public interface EnumApi {
10+
11+ @ GetMapping (path = "/endpoint" )
12+ void getEndpoint (@ RequestParam (name = "foo" ) Foo foo );
13+
14+ }
Original file line number Diff line number Diff line change 1+ package generated .model ;
2+
3+ import com .fasterxml .jackson .annotation .JsonCreator ;
4+ import com .fasterxml .jackson .annotation .JsonValue ;
5+ import generated .support .Generated ;
6+
7+ @ Generated (value = "openapi-processor-spring" , version = "test" )
8+ public enum Foo {
9+ FOO ("foo" ),
10+ FOO_2 ("foo-2" ),
11+ FOO_FOO ("foo-foo" );
12+
13+ private final String value ;
14+
15+ Foo (String value ) {
16+ this .value = value ;
17+ }
18+
19+ @ JsonValue
20+ public String getValue () {
21+ return this .value ;
22+ }
23+
24+ @ JsonCreator
25+ public static Foo fromValue (String value ) {
26+ for (Foo val : Foo .values ()) {
27+ if (val .value .equals (value )) {
28+ return val ;
29+ }
30+ }
31+ throw new IllegalArgumentException (value );
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ package generated .spring ;
2+
3+ import org .springframework .core .convert .converter .Converter ;
4+ import org .springframework .core .convert .converter .ConverterFactory ;
5+ import org .springframework .stereotype .Component ;
6+
7+ import java .util .EnumSet ;
8+ import java .util .function .Supplier ;
9+
10+ public class StringToEnumConverterFactory <T extends Enum <T > & Supplier <String >>
11+ implements ConverterFactory <String , T > {
12+
13+ @ Override
14+ @ SuppressWarnings ({"unchecked" , "rawtypes" })
15+ public <E extends T > Converter <String , E > getConverter (Class <E > targetType ) {
16+ return new StringToEnumConverter (targetType );
17+ }
18+
19+ static class StringToEnumConverter <T extends Enum <T > & Supplier <String >> implements Converter <String , T > {
20+
21+ private final Class <T > enumType ;
22+
23+ public StringToEnumConverter (Class <T > enumType ) {
24+ this .enumType = enumType ;
25+ }
26+
27+ public T convert (String source ) {
28+ String sourceValue = source .trim ();
29+
30+ for (T e : EnumSet .allOf (enumType )) {
31+ if (e .get ().equals (sourceValue )) {
32+ return e ;
33+ }
34+ }
35+
36+ throw new IllegalArgumentException (String .format ("No enum constant of %s has the value %s" ,
37+ enumType .getCanonicalName (),
38+ sourceValue ));
39+ }
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments