Skip to content

Commit

Permalink
fix 3581: recognize Hidden annotation on enum values.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimY4 authored and frantuma committed Jun 13, 2023
1 parent f1ec883 commit 70b17d9
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,11 @@ protected void _addEnumProps(Class<?> propClass, Schema property) {
for (Enum<?> en : enumConstants) {
String n;

Field enumField = ReflectionUtils.findField(en.name(), enumClass);
if (null != enumField && enumField.isAnnotationPresent(Hidden.class)) {
continue;
}

String enumValue = enumValues[en.ordinal()];
String methodValue = jsonValueMethod.flatMap(m -> ReflectionUtils.safeInvoke(m, en)).map(Object::toString).orElse(null);
String fieldValue = jsonValueField.flatMap(f -> ReflectionUtils.safeGet(f, en)).map(Object::toString).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,24 @@ public static Method getOverriddenMethod(Method method) {
return result;
}

/**
* Searches the field name in given class cls. If the field is found returns it, else return null.
*
* @param name is the field to search
* @param cls is the class or interface where to search
* @return field if it is found
*/
public static Field findField(String name, Class<?> cls) {
if (cls == null) {
return null;
}
try {
return cls.getField(name);
} catch (NoSuchFieldException nsfe) {
return null;
}
}

/**
* Searches the method methodToFind in given class cls. If the method is found returns it, else return null.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package io.swagger.v3.core.oas.models;

import com.fasterxml.jackson.annotation.JsonValue;
import io.swagger.v3.oas.annotations.Hidden;

/**
* Enum holds values different from names. Schema model will derive Integer value from jackson annotation JsonValue on public method.
*/
public enum JacksonNumberValueEnum {
FIRST(2),
SECOND(4),
THIRD(6);
THIRD(6),
@Hidden HIDDEN(-1);

private final int value;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package io.swagger.v3.core.oas.models;

import com.fasterxml.jackson.annotation.JsonValue;
import io.swagger.v3.oas.annotations.Hidden;

/**
* Enum holds values different from names. Schema model will derive Integer value from jackson annotation JsonValue on private field.
*/
public enum JacksonNumberValueFieldEnum {
FIRST(2),
SECOND(4),
THIRD(6);
THIRD(6),
@Hidden HIDDEN(-1);

@JsonValue
private final int value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.swagger.v3.core.oas.models;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.Hidden;

public enum JacksonPropertyEnum {
@JsonProperty("p1") PRIVATE,
@JsonProperty("p2") PUBLIC,
SYSTEM,
INVITE_ONLY
INVITE_ONLY,
@Hidden HIDDEN
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package io.swagger.v3.core.oas.models;

import com.fasterxml.jackson.annotation.JsonValue;
import io.swagger.v3.oas.annotations.Hidden;

/**
* Enum holds values different from names. Schema model will derive String value from jackson annotation JsonValue on public method.
*/
public enum JacksonValueEnum {
FIRST("one"),
SECOND("two"),
THIRD("three");
THIRD("three"),
@Hidden HIDDEN("hidden");

private final String value;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package io.swagger.v3.core.oas.models;

import com.fasterxml.jackson.annotation.JsonValue;
import io.swagger.v3.oas.annotations.Hidden;

/**
* Enum holds values different from names. Schema model will derive String value from jackson annotation JsonValue on private field.
*/
public enum JacksonValueFieldEnum {
FIRST("one"),
SECOND("two"),
THIRD("three");
THIRD("three"),
@Hidden HIDDEN("hidden");

@JsonValue
private final String value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package io.swagger.v3.core.oas.models;

import com.fasterxml.jackson.annotation.JsonValue;
import io.swagger.v3.oas.annotations.Hidden;

/**
* Enum holds values different from names. Schema model will derive String value from jackson annotation JsonValue on private method.
*/
public enum JacksonValuePrivateEnum {
FIRST("one"),
SECOND("two"),
THIRD("three");
THIRD("three"),
@Hidden HIDDEN("hidden");

private final String value;

Expand Down

0 comments on commit 70b17d9

Please sign in to comment.