Skip to content
Open
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 @@ -387,7 +387,8 @@ Schema calculateSchema(Components components, ParameterInfo parameterInfo, Reque
Arrays.stream(paramAnnotations),
Arrays.stream(typeAnnotations)
).toArray(Annotation[]::new);
if (type instanceof Class && !((Class<?>) type).isEnum() && optionalWebConversionServiceProvider.isPresent()) {
// Kotlin inline types are already resolved; their erased JVM type (e.g. String) must not be remapped via Spring converters
if (!resolved.kotlinInline() && type instanceof Class && !((Class<?>) type).isEnum() && optionalWebConversionServiceProvider.isPresent()) {
WebConversionServiceProvider webConversionServiceProvider = optionalWebConversionServiceProvider.get();
if (!MethodParameterPojoExtractor.isSwaggerPrimitiveType((Class) type) && Arrays.stream(mergedAnnotations)
.noneMatch(a -> a.annotationType() == io.swagger.v3.oas.annotations.media.Schema.class)) {
Expand Down Expand Up @@ -440,7 +441,7 @@ private TypeAndTypeAnnotations resolveTypeAndTypeAnnotationsForParameter(MethodP
&& type == String.class) {
Class<?> restored = KotlinInlineParameterResolver.resolveInlineType(methodParameter, type);
return restored != null
? new TypeAndTypeAnnotations(restored, Arrays.asList(restored.getAnnotations()))
? new TypeAndTypeAnnotations(restored, Arrays.asList(restored.getAnnotations()), true)
: new TypeAndTypeAnnotations(type, new ArrayList<>());
}

Expand Down Expand Up @@ -471,7 +472,11 @@ private static AnnotatedType getParameterAnnotatedType(MethodParameter methodPar
/**
* Pair of resolved Java type and type annotations merged with parameter annotations for {@code extractSchema}.
*/
private record TypeAndTypeAnnotations(Type type, List<Annotation> typeAnnotations) {
private record TypeAndTypeAnnotations(Type type, List<Annotation> typeAnnotations, boolean kotlinInline) {

TypeAndTypeAnnotations(Type type, List<Annotation> typeAnnotations) {
this(type, typeAnnotations, false);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package org.springdoc.core.service

import io.swagger.v3.core.converter.ModelConverters
import io.swagger.v3.oas.models.Components
import io.swagger.v3.oas.models.media.Schema
import jakarta.validation.constraints.Size
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.Mockito.mock
import org.mockito.Mockito.never
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`
import org.springdoc.core.converters.KotlinInlineClassUnwrappingConverter
import org.springdoc.core.models.ParameterInfo
import org.springdoc.core.properties.SpringDocConfigProperties
import org.springdoc.core.providers.ObjectMapperProvider
import org.springdoc.core.providers.WebConversionServiceProvider
import org.springdoc.core.utils.PropertyResolverUtils
import org.springframework.core.MethodParameter
import org.springframework.web.bind.annotation.RequestParam
import java.util.Optional

/**
* Tests the schema calculation of [GenericParameterService] for Kotlin value class parameters.
*/
class GenericParameterServiceKotlinInlineTest {

@JvmInline
value class Name(val value: String)

class Plain(val value: String)

class TestController {
fun hello(@RequestParam @Size(max = 255) name: Name): String = "Hello"

fun plain(@RequestParam plain: Plain): String = "Hello"
}

private val properties = SpringDocConfigProperties()

private val objectMapperProvider = ObjectMapperProvider(properties)

private val inlineConverter = KotlinInlineClassUnwrappingConverter(objectMapperProvider)

private val webConversionServiceProvider = mock(WebConversionServiceProvider::class.java)

private val genericParameterService = GenericParameterService(
PropertyResolverUtils(null, null, properties),
Optional.of(webConversionServiceProvider),
objectMapperProvider,
Optional.empty()
)

@BeforeEach
fun registerConverter() {
ModelConverters.getInstance(true).addConverter(inlineConverter)
}

@AfterEach
fun removeConverter() {
ModelConverters.getInstance(true).removeConverter(inlineConverter)
}

@Test
fun inlineValueClassParameterIsNotRemappedBySpringConverters() {
// Simulates the converter lookup returning an arbitrary source type for String
`when`(webConversionServiceProvider.getSpringConvertedType(String::class.java))
.thenReturn(Int::class.javaObjectType)

val schema = calculateSchema("hello", "name")

assertThat(typeOf(schema)).isEqualTo("string")
assertThat(schema.maxLength).isEqualTo(255)
assertThat(schema.minLength).isEqualTo(0)
verify(webConversionServiceProvider, never()).getSpringConvertedType(String::class.java)
}

@Test
fun nonInlineParameterStillUsesSpringConvertedType() {
`when`(webConversionServiceProvider.getSpringConvertedType(Plain::class.java))
.thenReturn(String::class.java)

val schema = calculateSchema("plain", "plain")

assertThat(typeOf(schema)).isEqualTo("string")
verify(webConversionServiceProvider).getSpringConvertedType(Plain::class.java)
}

private fun calculateSchema(methodNamePrefix: String, parameterName: String): Schema<*> {
// Methods taking value classes get a mangled JVM name (e.g. hello-imKZr6c)
val method = TestController::class.java.declaredMethods.single { it.name.startsWith(methodNamePrefix) }
val parameterInfo = ParameterInfo(parameterName, MethodParameter(method, 0), genericParameterService, null)
return genericParameterService.calculateSchema(Components(), parameterInfo, null, null)
}

private fun typeOf(schema: Schema<*>): String? = schema.type ?: schema.types?.singleOrNull()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2019-2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package test.org.springdoc.api.v31.app28

import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.validation.constraints.Size
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam

@Controller
@RequestMapping("/api/v2/hello")
@Tag(name = "Hello")
class HelloController {
@GetMapping
@Operation(method = "hello")
fun hello(@RequestParam @Size(max = 255) name: Name): String = "Hello"
}

@JvmInline
value class Name(val value: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2019-2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package test.org.springdoc.api.v31.app28

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.context.annotation.ComponentScan
import test.org.springdoc.api.v31.AbstractKotlinSpringDocMVCTest

/**
* A String-backed Kotlin value class request parameter is documented as a string,
* with its validation constraints applied as string constraints.
*
* @see <a href="https://github.com/springdoc/springdoc-openapi/issues/3459">#3459</a>
*/
class SpringDocApp28Test : AbstractKotlinSpringDocMVCTest() {

@SpringBootApplication
@ComponentScan(basePackages = ["org.springdoc", "test.org.springdoc.api.v31.app28"])
class DemoApplication
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"openapi": "3.1.0",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost",
"description": "Generated server url"
}
],
"paths": {
"/api/v2/hello": {
"get": {
"tags": [
"Hello"
],
"operationId": "hello-qHrriFU",
"parameters": [
{
"name": "name",
"in": "query",
"required": true,
"schema": {
"type": "string",
"maxLength": 255,
"minLength": 0
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
},
"components": {}
}