diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java index e1ed50c9f2..c5fa3ff1b6 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/AnnotationsUtils.java @@ -1720,6 +1720,9 @@ public static Optional getSchema(io.swagger.v3.oas.annotations if (StringUtils.isNotBlank(schemaAnnotation.format())) { schemaObject.setFormat(schemaAnnotation.format()); } + if (schemaAnnotation.allowableValues().length != 0) { + schemaObject.setEnum(Arrays.asList(schemaAnnotation.allowableValues())); + } if (isArray) { Optional arraySchema = AnnotationsUtils.getArraySchema(arrayAnnotation, components, jsonViewAnnotation, openapi31, null); if (arraySchema.isPresent()) { diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/util/AnnotationsUtilsTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/util/AnnotationsUtilsTest.java index 08f95abd10..320a2ff181 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/util/AnnotationsUtilsTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/util/AnnotationsUtilsTest.java @@ -16,6 +16,7 @@ import java.math.BigInteger; import java.net.URI; import java.net.URL; +import java.util.Arrays; import java.util.Date; import java.util.Map; import java.util.Optional; @@ -97,4 +98,38 @@ private void dummyType() { class DummyClass implements Serializable {} + @DataProvider + private Object[][] expectedEnumSchema() { + return new Object[][]{ + {"emptyImplementationSchemaEnum", ImmutableMap.of("type", "string", "enum", Arrays.asList("foo", "bar"))}, + {"stringImplementationSchemaEnum", ImmutableMap.of("type", "string", "enum", Arrays.asList("foo", "bar"))}, + {"enumSchema", ImmutableMap.of("type", "string", "enum", Arrays.asList("FOO", "BAR"))}, + }; + } + + @Test(dataProvider = "expectedEnumSchema") + public void getEnumSchema(String methodName, Map expected) throws NoSuchMethodException { + final Method method = getClass().getDeclaredMethod(methodName); + Content annotationContent = method.getAnnotation(ApiResponse.class).content()[0]; + Optional schema = AnnotationsUtils.getSchema(annotationContent, new Components(), null, false); + Assert.assertTrue(schema.isPresent()); + Assert.assertEquals(schema.get().getType(), expected.get("type")); + Assert.assertEquals(schema.get().getEnum(), expected.get("enum")); + } + + @ApiResponse(content = @Content(schema = @io.swagger.v3.oas.annotations.media.Schema(allowableValues = {"foo", "bar"}))) + private void emptyImplementationSchemaEnum() { + } + + @ApiResponse(content = @Content(schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = String.class, allowableValues = {"foo", "bar"}))) + private void stringImplementationSchemaEnum() { + } + + @ApiResponse(content = @Content(schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Foo.class))) + private void enumSchema() { + } + + enum Foo { + FOO, BAR; + } }