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 #4711: check allowableValues for enum #4712

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -1720,6 +1720,9 @@ public static Optional<? extends Schema> 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<Schema> arraySchema = AnnotationsUtils.getArraySchema(arrayAnnotation, components, jsonViewAnnotation, openapi31, null);
if (arraySchema.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object> expected) throws NoSuchMethodException {
final Method method = getClass().getDeclaredMethod(methodName);
Content annotationContent = method.getAnnotation(ApiResponse.class).content()[0];
Optional<? extends Schema> 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;
}
}