From e4b5f27d3673bda169216dcc00d5b37f55188366 Mon Sep 17 00:00:00 2001 From: Mattias-Sehlstedt <60173714+Mattias-Sehlstedt@users.noreply.github.com> Date: Tue, 1 Dec 2026 18:07:09 +0100 Subject: [PATCH] fix: nondeterministic schema type for Kotlin value class parameters For a Kotlin value class parameter, the JVM sees the underlying String type. calculateSchema passed that String to WebConversionServiceProvider.getSpringConvertedType(). That method returns the source type of any converter that targets String. It picks the first match from a HashMap, so the result depended on hash ordering. The parameter schema could come out as string, number or array between runs, and @Size was applied as maxLength, maximum or maxItems to match. Skip the Spring conversion lookup when the parameter type has already been resolved to a Kotlin value class. --- .../core/service/GenericParameterService.java | 11 ++- ...GenericParameterServiceKotlinInlineTest.kt | 99 +++++++++++++++++++ .../api/v31/app28/HelloController.kt | 37 +++++++ .../api/v31/app28/SpringDocApp28Test.kt | 34 +++++++ .../test/resources/results/3.1.0/app28.json | 48 +++++++++ 5 files changed, 226 insertions(+), 3 deletions(-) create mode 100644 springdoc-openapi-starter-common/src/test/kotlin/org/springdoc/core/service/GenericParameterServiceKotlinInlineTest.kt create mode 100644 springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/kotlin/test/org/springdoc/api/v31/app28/HelloController.kt create mode 100644 springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/kotlin/test/org/springdoc/api/v31/app28/SpringDocApp28Test.kt create mode 100644 springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/resources/results/3.1.0/app28.json diff --git a/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/GenericParameterService.java b/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/GenericParameterService.java index 85ebeaaade..415e24918b 100644 --- a/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/GenericParameterService.java +++ b/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/GenericParameterService.java @@ -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)) { @@ -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<>()); } @@ -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 typeAnnotations) { + private record TypeAndTypeAnnotations(Type type, List typeAnnotations, boolean kotlinInline) { + + TypeAndTypeAnnotations(Type type, List typeAnnotations) { + this(type, typeAnnotations, false); + } } /** diff --git a/springdoc-openapi-starter-common/src/test/kotlin/org/springdoc/core/service/GenericParameterServiceKotlinInlineTest.kt b/springdoc-openapi-starter-common/src/test/kotlin/org/springdoc/core/service/GenericParameterServiceKotlinInlineTest.kt new file mode 100644 index 0000000000..cb109c8a24 --- /dev/null +++ b/springdoc-openapi-starter-common/src/test/kotlin/org/springdoc/core/service/GenericParameterServiceKotlinInlineTest.kt @@ -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() +} diff --git a/springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/kotlin/test/org/springdoc/api/v31/app28/HelloController.kt b/springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/kotlin/test/org/springdoc/api/v31/app28/HelloController.kt new file mode 100644 index 0000000000..dca7c80367 --- /dev/null +++ b/springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/kotlin/test/org/springdoc/api/v31/app28/HelloController.kt @@ -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) diff --git a/springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/kotlin/test/org/springdoc/api/v31/app28/SpringDocApp28Test.kt b/springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/kotlin/test/org/springdoc/api/v31/app28/SpringDocApp28Test.kt new file mode 100644 index 0000000000..38da3f582d --- /dev/null +++ b/springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/kotlin/test/org/springdoc/api/v31/app28/SpringDocApp28Test.kt @@ -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 #3459 + */ +class SpringDocApp28Test : AbstractKotlinSpringDocMVCTest() { + + @SpringBootApplication + @ComponentScan(basePackages = ["org.springdoc", "test.org.springdoc.api.v31.app28"]) + class DemoApplication +} diff --git a/springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/resources/results/3.1.0/app28.json b/springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/resources/results/3.1.0/app28.json new file mode 100644 index 0000000000..d1500ae97a --- /dev/null +++ b/springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/resources/results/3.1.0/app28.json @@ -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": {} +}