swagger-core icon indicating copy to clipboard operation
swagger-core copied to clipboard

Can not hide an enum value using @Hidden

Open nkavian opened this issue 5 years ago • 4 comments

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.

nkavian avatar Jun 05 '20 02:06 nkavian

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;
}

NiazBikbaev avatar Apr 18 '21 14:04 NiazBikbaev

Thanks, is it possible to get a pure Java version? :)

nkavian avatar Jul 13 '21 21:07 nkavian

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.

nkavian avatar Jul 15 '21 02:07 nkavian

Hello, is there a springfox replacement for this workaround?

tanerilyazov avatar Jun 21 '22 22:06 tanerilyazov

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 ?

RajuReddy7777 avatar Feb 17 '23 14:02 RajuReddy7777

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.

SimY4 avatar Jun 13 '23 02:06 SimY4