diff --git a/openapi-specification/src/main/kotlin/io/javalin/openapi/OpenApiAnnotations.kt b/openapi-specification/src/main/kotlin/io/javalin/openapi/OpenApiAnnotations.kt index ef574c9..9fb21b0 100644 --- a/openapi-specification/src/main/kotlin/io/javalin/openapi/OpenApiAnnotations.kt +++ b/openapi-specification/src/main/kotlin/io/javalin/openapi/OpenApiAnnotations.kt @@ -161,6 +161,10 @@ annotation class OpenApiSecurity( @Retention(RUNTIME) annotation class OpenApiIgnore +@Target(FUNCTION, PROPERTY_GETTER, PROPERTY_SETTER) +@Retention(RUNTIME) +annotation class OpenApiRequired + @Target(CLASS, FUNCTION, PROPERTY_GETTER, PROPERTY_SETTER) @Retention(RUNTIME) annotation class OpenApiName( diff --git a/openapi-specification/src/main/kotlin/io/javalin/openapi/experimental/processor/generators/TypeSchemaGenerator.kt b/openapi-specification/src/main/kotlin/io/javalin/openapi/experimental/processor/generators/TypeSchemaGenerator.kt index 912fc7b..bdaec23 100644 --- a/openapi-specification/src/main/kotlin/io/javalin/openapi/experimental/processor/generators/TypeSchemaGenerator.kt +++ b/openapi-specification/src/main/kotlin/io/javalin/openapi/experimental/processor/generators/TypeSchemaGenerator.kt @@ -13,6 +13,7 @@ import io.javalin.openapi.OpenApiExample import io.javalin.openapi.OpenApiIgnore import io.javalin.openapi.OpenApiName import io.javalin.openapi.OpenApiPropertyType +import io.javalin.openapi.OpenApiRequired import io.javalin.openapi.Visibility import io.javalin.openapi.experimental.AnnotationProcessorContext import io.javalin.openapi.experimental.ClassDefinition @@ -266,12 +267,17 @@ internal fun ClassDefinition.findAllProperties(requireNonNulls: Boolean): Collec else -> false } + val required = when { + property.getAnnotation(OpenApiRequired::class.java) != null -> true + else -> requireNonNulls && isNotNull + } + properties.add( Property( name = name, type = propertyType.toClassDefinition(), composition = findCompositionInElement(context, property), - required = requireNonNulls && isNotNull, + required = required, extra = property.findExtra(context) ) )