Skip to content

Commit

Permalink
fix: derive an enumeration's type from the value type (#1974)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar authored Sep 2, 2024
1 parent 2c55d43 commit 1438760
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -556,15 +555,15 @@ public static Schema enumToSchema(final AnnotationScannerContext context, Type e
ClassInfo enumKlazz = context.getIndex().getClassByName(TypeUtil.getName(enumType));
AnnotationInstance schemaAnnotation = context.annotations().getAnnotation(enumKlazz, SchemaConstant.DOTNAME_SCHEMA);
Schema enumSchema = new SchemaImpl();
Type enumValueType = enumeration.isEmpty() ? Type.create(String.class) : Type.create(enumeration.get(0).getClass());

if (schemaAnnotation != null) {
Map<String, Object> defaults = new HashMap<>(2);
defaults.put(SchemaConstant.PROP_TYPE, SchemaType.STRING);
Map<String, Object> defaults = new LinkedHashMap<>(TypeUtil.getTypeAttributes(enumValueType));
defaults.put(SchemaConstant.PROP_ENUMERATION, enumeration);

enumSchema = readSchema(context, enumSchema, schemaAnnotation, enumKlazz, defaults);
} else {
enumSchema.setType(SchemaType.STRING);
TypeUtil.applyTypeAttributes(enumValueType, enumSchema);
enumSchema.setEnumeration(enumeration);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;

Expand Down Expand Up @@ -44,6 +45,78 @@ void testWildcardSchemaIsEmpty() {
assertNull(result.getType());
}

@org.eclipse.microprofile.openapi.annotations.media.Schema(description = "An example enum with a value-driven schema type")
public static enum ExampleEnum1 {
ONE(1),
TWO(2);

private final Integer value;

ExampleEnum1(final Integer value) {
this.value = value;
}

@com.fasterxml.jackson.annotation.JsonValue
// type derived as int with format int32 due to JsonValue return type
public Integer getValue() {
return value;
}
}

@Test
void testEnumToSchemaTypeUsesValueWithAnnotation() {
Index index = indexOf(ExampleEnum1.class);
AnnotationScannerContext context = new AnnotationScannerContext(index, ClassLoaderUtil.getDefaultClassLoader(),
emptyConfig());
Schema result = SchemaFactory.enumToSchema(context, Type.create(ExampleEnum1.class));
assertEquals(Schema.SchemaType.INTEGER, result.getType());
assertEquals("int32", result.getFormat());
assertEquals("An example enum with a value-driven schema type", result.getDescription());
}

public static enum ExampleEnum2 {
ONE(1L),
TWO(2L);

private final Long value;

ExampleEnum2(final Long value) {
this.value = value;
}

@com.fasterxml.jackson.annotation.JsonValue
// type derived as int with format int64 due to JsonValue return type
public Long getValue() {
return value;
}
}

@Test
void testEnumToSchemaTypeUsesValueWithoutAnnotation() {
Index index = indexOf(ExampleEnum2.class);
AnnotationScannerContext context = new AnnotationScannerContext(index, ClassLoaderUtil.getDefaultClassLoader(),
emptyConfig());
Schema result = SchemaFactory.enumToSchema(context, Type.create(ExampleEnum2.class));
assertEquals(Schema.SchemaType.INTEGER, result.getType());
assertEquals("int64", result.getFormat());
assertNull(result.getDescription());
}

@org.eclipse.microprofile.openapi.annotations.media.Schema(description = "An example enum with no values", enumeration = {
"VAL1", "VAL2" })
public static enum ExampleEnum3 {
}

@Test
void testEnumToSchemaTypeWithEmptyEnum() {
Index index = indexOf(ExampleEnum3.class);
AnnotationScannerContext context = new AnnotationScannerContext(index, ClassLoaderUtil.getDefaultClassLoader(),
emptyConfig());
Schema result = SchemaFactory.enumToSchema(context, Type.create(ExampleEnum3.class));
assertEquals(Schema.SchemaType.STRING, result.getType());
assertEquals(Arrays.asList("VAL1", "VAL2"), result.getEnumeration());
}

@Test
void testParseSchemaType() {
for (SchemaType type : SchemaType.values()) {
Expand Down

0 comments on commit 1438760

Please sign in to comment.