Skip to content

Commit

Permalink
fix swagger-api#4711: check allowableValues for enum
Browse files Browse the repository at this point in the history
  • Loading branch information
buzzerrookie committed Jul 18, 2024
1 parent e6d3fa3 commit 6a591f2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
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;
}
}

0 comments on commit 6a591f2

Please sign in to comment.