Skip to content

Commit 104a93b

Browse files
committed
#198, test enum converter factory creation
1 parent 139e800 commit 104a93b

File tree

8 files changed

+164
-0
lines changed

8 files changed

+164
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
items:
2+
- inputs/openapi30.yaml
3+
- inputs/openapi31.yaml
4+
- inputs/mapping.yaml
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
openapi-processor-mapping: v5
2+
3+
options:
4+
package-name: generated
5+
format-code: false
6+
enum-type: framework
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
items:
2+
- outputs/api/EnumApi.java
3+
- outputs/<model>/Foo.java
4+
- outputs/spring/StringToEnumConverterFactory.java
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
}

0 commit comments

Comments
 (0)