Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support enums with unbounded wildcards #4055

Merged
merged 1 commit into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -947,28 +947,32 @@ protected void _addEnumProps(Class<?> propClass, Schema property) {
Class<Enum<?>> enumClass = (Class<Enum<?>>) propClass;

Enum<?>[] enumConstants = enumClass.getEnumConstants();
String[] enumValues = _intr.findEnumValues(propClass, enumConstants, new String[enumConstants.length]);

for (Enum<?> en : enumConstants) {
String n;

String enumValue = enumValues[en.ordinal()];
String s = jsonValueMethod.flatMap(m -> ReflectionUtils.safeInvoke(m, en)).map(Object::toString).orElse(null);

if (s != null) {
n = s;
} else if (enumValue != null) {
n = enumValue;
} else if (useIndex) {
n = String.valueOf(en.ordinal());
} else if (useToString) {
n = en.toString();
} else {
n = _intr.findEnumValue(en);
}
if (property instanceof StringSchema) {
StringSchema sp = (StringSchema) property;
sp.addEnumItem(n);
if (enumConstants != null) {
String[] enumValues = _intr.findEnumValues(propClass, enumConstants,
new String[enumConstants.length]);

for (Enum<?> en : enumConstants) {
String n;

String enumValue = enumValues[en.ordinal()];
String s = jsonValueMethod.flatMap(m -> ReflectionUtils.safeInvoke(m, en))
.map(Object::toString).orElse(null);

if (s != null) {
n = s;
} else if (enumValue != null) {
n = enumValue;
} else if (useIndex) {
n = String.valueOf(en.ordinal());
} else if (useToString) {
n = en.toString();
} else {
n = _intr.findEnumValue(en);
}
if (property instanceof StringSchema) {
StringSchema sp = (StringSchema) property;
sp.addEnumItem(n);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;

public class EnumTest extends SwaggerTestBase {

@Test
public void testEnum() throws Exception {
public void testEnum() {
final ModelResolver modelResolver = new ModelResolver(mapper());
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);

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

@Test
public void testEnumGenerics() {
final ModelResolver modelResolver = new ModelResolver(mapper());
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);

final Schema model = context.resolve((new AnnotatedType().type(Contract.class)));
assertNotNull(model);
assertEquals(model.getName(), "Contract");
assertTrue(model.getProperties().containsKey("type"));
assertNotNull(model.getProperties().get("type"));
}

public enum Currency {
USA, CANADA
}

public static class Contract {

private Enum<?> type;

public Enum<?> getType() {
return type;
}

public Contract setType(Enum<?> type) {
this.type = type;
return this;
}
}
}