swagger-core
swagger-core copied to clipboard
Can not hide an enum value using @Hidden
Here is an explicit example where all 3 enum values are still rendered in the swagger output. Using the latest swagger core version with Java 8.
public enum Foo {
Value1,
@Hidden
Value2,
Value3,
}
Expected output is to only render Value1 and Value3 in the output, not all 3.
Here is my workaround solution for this issue. I'm using spring-boot + springdoc
import com.fasterxml.jackson.annotation.JsonEnumDefaultValue
import com.fasterxml.jackson.databind.type.SimpleType
import io.swagger.v3.core.converter.AnnotatedType
import io.swagger.v3.oas.annotations.Hidden
import io.swagger.v3.oas.models.media.Schema
import org.springdoc.core.customizers.PropertyCustomizer
import org.springframework.stereotype.Component
@Component
class HiddenEnumPropertyCustomizer : PropertyCustomizer {
private val processedEnumsCache: MutableMap<String, Schema<*>> = mutableMapOf()
override fun customize(property: Schema<*>, type: AnnotatedType): Schema<*> {
if (type.type is SimpleType && (type.type as SimpleType).rawClass.isEnum) {
return processedEnumsCache.computeIfAbsent((type.type as SimpleType).rawClass.simpleName) { process(type, property) }
}
return property
}
private fun process(type: AnnotatedType, property: Schema<*>): Schema<*> {
val enumClass = (type.type as SimpleType).rawClass
val notHiddenEnumConstants = enumClass.declaredFields.asSequence()
.filter { it.annotations.filterIsInstance<Hidden>().isEmpty() }
.map { it.name }
.toList()
property.enum = enumClass.enumConstants.asSequence()
.filterIsInstance<Enum<*>>()
.filter { it.name in notHiddenEnumConstants }
.map { it.name }
.toList()
return property
}
}
enum class Color {
RED,
BLUE,
@JsonEnumDefaultValue
@Hidden
UNKNOWN;
}
Thanks, is it possible to get a pure Java version? :)
I created my own version in Java, really appreciate the Kotlin example.
I also recommend anyone that uses this to also implement ParameterCustomizer so that enums used as Spring @PathVariable and @RequestParam are also handled appropriately.
Hello, is there a springfox replacement for this workaround?
Seems like it should work by default as @Hidden has the Target of TYPE which covers Class, interface (including annotation interface), enum, or record declaration
Why it's not working as expected ?
Call to maintainers: can you please have a look at proposed PR. It's been raised and left unchecked by maintainers for quite some time.