Skip to content

Commit 78906a2

Browse files
christopher-cudennecfrantuma
authored andcommitted
fix: Support enums with unbounded wildcards
1 parent 6b928ac commit 78906a2

File tree

2 files changed

+54
-25
lines changed

2 files changed

+54
-25
lines changed

modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -947,28 +947,32 @@ protected void _addEnumProps(Class<?> propClass, Schema property) {
947947
Class<Enum<?>> enumClass = (Class<Enum<?>>) propClass;
948948

949949
Enum<?>[] enumConstants = enumClass.getEnumConstants();
950-
String[] enumValues = _intr.findEnumValues(propClass, enumConstants, new String[enumConstants.length]);
951-
952-
for (Enum<?> en : enumConstants) {
953-
String n;
954-
955-
String enumValue = enumValues[en.ordinal()];
956-
String s = jsonValueMethod.flatMap(m -> ReflectionUtils.safeInvoke(m, en)).map(Object::toString).orElse(null);
957-
958-
if (s != null) {
959-
n = s;
960-
} else if (enumValue != null) {
961-
n = enumValue;
962-
} else if (useIndex) {
963-
n = String.valueOf(en.ordinal());
964-
} else if (useToString) {
965-
n = en.toString();
966-
} else {
967-
n = _intr.findEnumValue(en);
968-
}
969-
if (property instanceof StringSchema) {
970-
StringSchema sp = (StringSchema) property;
971-
sp.addEnumItem(n);
950+
if (enumConstants != null) {
951+
String[] enumValues = _intr.findEnumValues(propClass, enumConstants,
952+
new String[enumConstants.length]);
953+
954+
for (Enum<?> en : enumConstants) {
955+
String n;
956+
957+
String enumValue = enumValues[en.ordinal()];
958+
String s = jsonValueMethod.flatMap(m -> ReflectionUtils.safeInvoke(m, en))
959+
.map(Object::toString).orElse(null);
960+
961+
if (s != null) {
962+
n = s;
963+
} else if (enumValue != null) {
964+
n = enumValue;
965+
} else if (useIndex) {
966+
n = String.valueOf(en.ordinal());
967+
} else if (useToString) {
968+
n = en.toString();
969+
} else {
970+
n = _intr.findEnumValue(en);
971+
}
972+
if (property instanceof StringSchema) {
973+
StringSchema sp = (StringSchema) property;
974+
sp.addEnumItem(n);
975+
}
972976
}
973977
}
974978
}

modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/EnumTest.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515

1616
import static org.testng.Assert.assertEquals;
1717
import static org.testng.Assert.assertNotNull;
18-
import static org.testng.Assert.assertNull;
1918
import static org.testng.Assert.assertTrue;
2019

2120
public class EnumTest extends SwaggerTestBase {
2221

2322
@Test
24-
public void testEnum() throws Exception {
23+
public void testEnum() {
2524
final ModelResolver modelResolver = new ModelResolver(mapper());
2625
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
2726

@@ -41,11 +40,37 @@ public void testEnum() throws Exception {
4140
final StringSchema strProperty = (StringSchema) property;
4241
assertNotNull(strProperty.getEnum());
4342
final Collection<String> values =
44-
new ArrayList<String>(Collections2.transform(Arrays.asList(Currency.values()), Functions.toStringFunction()));
43+
new ArrayList<>(Collections2.transform(Arrays.asList(Currency.values()), Functions.toStringFunction()));
4544
assertEquals(strProperty.getEnum(), values);
4645
}
4746

47+
@Test
48+
public void testEnumGenerics() {
49+
final ModelResolver modelResolver = new ModelResolver(mapper());
50+
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
51+
52+
final Schema model = context.resolve((new AnnotatedType().type(Contract.class)));
53+
assertNotNull(model);
54+
assertEquals(model.getName(), "Contract");
55+
assertTrue(model.getProperties().containsKey("type"));
56+
assertNotNull(model.getProperties().get("type"));
57+
}
58+
4859
public enum Currency {
4960
USA, CANADA
5061
}
62+
63+
public static class Contract {
64+
65+
private Enum<?> type;
66+
67+
public Enum<?> getType() {
68+
return type;
69+
}
70+
71+
public Contract setType(Enum<?> type) {
72+
this.type = type;
73+
return this;
74+
}
75+
}
5176
}

0 commit comments

Comments
 (0)