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 #3581: recognize Hidden annotation on enum values. #4394

Merged
merged 1 commit into from
Jun 13, 2023
Merged
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 @@ -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