Skip to content

Commit 70b17d9

Browse files
SimY4frantuma
authored andcommitted
fix 3581: recognize Hidden annotation on enum values.
1 parent f1ec883 commit 70b17d9

File tree

8 files changed

+41
-6
lines changed

8 files changed

+41
-6
lines changed

modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,11 @@ protected void _addEnumProps(Class<?> propClass, Schema property) {
10641064
for (Enum<?> en : enumConstants) {
10651065
String n;
10661066

1067+
Field enumField = ReflectionUtils.findField(en.name(), enumClass);
1068+
if (null != enumField && enumField.isAnnotationPresent(Hidden.class)) {
1069+
continue;
1070+
}
1071+
10671072
String enumValue = enumValues[en.ordinal()];
10681073
String methodValue = jsonValueMethod.flatMap(m -> ReflectionUtils.safeInvoke(m, en)).map(Object::toString).orElse(null);
10691074
String fieldValue = jsonValueField.flatMap(f -> ReflectionUtils.safeGet(f, en)).map(Object::toString).orElse(null);

modules/swagger-core/src/main/java/io/swagger/v3/core/util/ReflectionUtils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,24 @@ public static Method getOverriddenMethod(Method method) {
160160
return result;
161161
}
162162

163+
/**
164+
* Searches the field name in given class cls. If the field is found returns it, else return null.
165+
*
166+
* @param name is the field to search
167+
* @param cls is the class or interface where to search
168+
* @return field if it is found
169+
*/
170+
public static Field findField(String name, Class<?> cls) {
171+
if (cls == null) {
172+
return null;
173+
}
174+
try {
175+
return cls.getField(name);
176+
} catch (NoSuchFieldException nsfe) {
177+
return null;
178+
}
179+
}
180+
163181
/**
164182
* Searches the method methodToFind in given class cls. If the method is found returns it, else return null.
165183
*

modules/swagger-core/src/test/java/io/swagger/v3/core/oas/models/JacksonNumberValueEnum.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package io.swagger.v3.core.oas.models;
22

33
import com.fasterxml.jackson.annotation.JsonValue;
4+
import io.swagger.v3.oas.annotations.Hidden;
45

56
/**
67
* Enum holds values different from names. Schema model will derive Integer value from jackson annotation JsonValue on public method.
78
*/
89
public enum JacksonNumberValueEnum {
910
FIRST(2),
1011
SECOND(4),
11-
THIRD(6);
12+
THIRD(6),
13+
@Hidden HIDDEN(-1);
1214

1315
private final int value;
1416

modules/swagger-core/src/test/java/io/swagger/v3/core/oas/models/JacksonNumberValueFieldEnum.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package io.swagger.v3.core.oas.models;
22

33
import com.fasterxml.jackson.annotation.JsonValue;
4+
import io.swagger.v3.oas.annotations.Hidden;
45

56
/**
67
* Enum holds values different from names. Schema model will derive Integer value from jackson annotation JsonValue on private field.
78
*/
89
public enum JacksonNumberValueFieldEnum {
910
FIRST(2),
1011
SECOND(4),
11-
THIRD(6);
12+
THIRD(6),
13+
@Hidden HIDDEN(-1);
1214

1315
@JsonValue
1416
private final int value;
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package io.swagger.v3.core.oas.models;
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import io.swagger.v3.oas.annotations.Hidden;
45

56
public enum JacksonPropertyEnum {
67
@JsonProperty("p1") PRIVATE,
78
@JsonProperty("p2") PUBLIC,
89
SYSTEM,
9-
INVITE_ONLY
10+
INVITE_ONLY,
11+
@Hidden HIDDEN
1012
}

modules/swagger-core/src/test/java/io/swagger/v3/core/oas/models/JacksonValueEnum.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package io.swagger.v3.core.oas.models;
22

33
import com.fasterxml.jackson.annotation.JsonValue;
4+
import io.swagger.v3.oas.annotations.Hidden;
45

56
/**
67
* Enum holds values different from names. Schema model will derive String value from jackson annotation JsonValue on public method.
78
*/
89
public enum JacksonValueEnum {
910
FIRST("one"),
1011
SECOND("two"),
11-
THIRD("three");
12+
THIRD("three"),
13+
@Hidden HIDDEN("hidden");
1214

1315
private final String value;
1416

modules/swagger-core/src/test/java/io/swagger/v3/core/oas/models/JacksonValueFieldEnum.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package io.swagger.v3.core.oas.models;
22

33
import com.fasterxml.jackson.annotation.JsonValue;
4+
import io.swagger.v3.oas.annotations.Hidden;
45

56
/**
67
* Enum holds values different from names. Schema model will derive String value from jackson annotation JsonValue on private field.
78
*/
89
public enum JacksonValueFieldEnum {
910
FIRST("one"),
1011
SECOND("two"),
11-
THIRD("three");
12+
THIRD("three"),
13+
@Hidden HIDDEN("hidden");
1214

1315
@JsonValue
1416
private final String value;

modules/swagger-core/src/test/java/io/swagger/v3/core/oas/models/JacksonValuePrivateEnum.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package io.swagger.v3.core.oas.models;
22

33
import com.fasterxml.jackson.annotation.JsonValue;
4+
import io.swagger.v3.oas.annotations.Hidden;
45

56
/**
67
* Enum holds values different from names. Schema model will derive String value from jackson annotation JsonValue on private method.
78
*/
89
public enum JacksonValuePrivateEnum {
910
FIRST("one"),
1011
SECOND("two"),
11-
THIRD("three");
12+
THIRD("three"),
13+
@Hidden HIDDEN("hidden");
1214

1315
private final String value;
1416

0 commit comments

Comments
 (0)