Skip to content

Commit

Permalink
Merge pull request #1486 from apixandru/unique_enum_values
Browse files Browse the repository at this point in the history
Remove duplicate enum values

fixes #1485
  • Loading branch information
dilipkrish committed Sep 12, 2016
2 parents 7c94d80 + e4ed945 commit 773e22c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
Expand Up @@ -27,11 +27,14 @@
import springfox.documentation.service.AllowableValues;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import static com.google.common.base.Strings.*;
import static com.google.common.collect.Lists.*;
import static java.util.Arrays.asList;

public class Enums {

Expand All @@ -48,7 +51,7 @@ public static AllowableValues allowableValues(Class<?> type) {
}

static List<String> getEnumValues(final Class<?> subject) {
return transform(Arrays.asList(subject.getEnumConstants()), new Function<Object, String>() {
return transformUnique(subject.getEnumConstants(), new Function<Object, String>() {
@Override
public String apply(Object input) {
Optional<String> jsonValue = findJsonValueAnnotatedMethod(input)
Expand All @@ -74,6 +77,12 @@ public String apply(Method input) {
};
}

private static <E> List<String> transformUnique(E[] values, Function<E, String> mapper) {
List<String> nonUniqueValues = transform(asList(values), mapper);
Set<String> uniqueValues = new LinkedHashSet<String>(nonUniqueValues);
return new ArrayList<String>(uniqueValues);
}

private static Optional<Method> findJsonValueAnnotatedMethod(Object enumConstant) {
for (Method each : enumConstant.getClass().getMethods()) {
JsonValue jsonValue = AnnotationUtils.findAnnotation(each, JsonValue.class);
Expand Down
Expand Up @@ -53,4 +53,12 @@ class EnumsSpec extends Specification {
then:
thrown(UnsupportedOperationException)
}

def "we shouldn't have duplicate enum representations"() {
given:
def expected = Arrays.asList("one", "two")
expect:
expected.equals(Enums.getEnumValues(DuplicateRepresentationEnum))
}

}
@@ -0,0 +1,43 @@
/*
*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package springfox.documentation.schema;

/**
*
* A real life example of this would be org.springframework.http.HttpStatus
*
* @author Alexandru-Constantin Bledea
* @since Sep 12, 2016
*/
public enum DuplicateRepresentationEnum {

ONE,
TWO,

@Deprecated
one,
@Deprecated
two;

@Override
public String toString() {
return name().toLowerCase();
}

}

0 comments on commit 773e22c

Please sign in to comment.