diff --git a/springdoc-openapi-starter-webmvc-api/src/test/java/test/org/springdoc/api/v30/app273/NullableReferenceController.java b/springdoc-openapi-starter-webmvc-api/src/test/java/test/org/springdoc/api/v30/app273/NullableReferenceController.java new file mode 100644 index 000000000..ea8734a95 --- /dev/null +++ b/springdoc-openapi-starter-webmvc-api/src/test/java/test/org/springdoc/api/v30/app273/NullableReferenceController.java @@ -0,0 +1,42 @@ +/* + * 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.v30.app273; + +import jakarta.annotation.Nullable; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * Controller exposing a nullable Java component reference. + * + * @author Janek Lasocki-Biczysko + */ +@RestController +public class NullableReferenceController { + + @GetMapping("/nullable-reference") + public NullableReferenceResponse getNullableReference() { + return new NullableReferenceResponse(null); + } + + public record NullableReferenceResponse(@Nullable NullableReferenceChild child) { + } + + public record NullableReferenceChild(String value) { + } +} diff --git a/springdoc-openapi-starter-webmvc-api/src/test/java/test/org/springdoc/api/v30/app273/SpringDocApp273Test.java b/springdoc-openapi-starter-webmvc-api/src/test/java/test/org/springdoc/api/v30/app273/SpringDocApp273Test.java new file mode 100644 index 000000000..9a855e67e --- /dev/null +++ b/springdoc-openapi-starter-webmvc-api/src/test/java/test/org/springdoc/api/v30/app273/SpringDocApp273Test.java @@ -0,0 +1,50 @@ +/* + * 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.v30.app273; + +import org.junit.jupiter.api.Test; +import org.springdoc.core.utils.Constants; +import test.org.springdoc.api.AbstractCommonTest; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.test.context.TestPropertySource; + +import static org.hamcrest.Matchers.is; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +/** + * Verifies that nullability for a Java component reference is scoped to its property. + * + * @author Janek Lasocki-Biczysko + */ +@TestPropertySource(properties = { "springdoc.api-docs.version=openapi_3_0" }) +public class SpringDocApp273Test extends AbstractCommonTest { + + @Test + void nullableJavaComponentReferenceIsScopedToTheProperty() throws Exception { + mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL)).andExpect(status().isOk()) + .andExpect(jsonPath("$.components.schemas.NullableReferenceResponse.properties.child.nullable", is(true))) + .andExpect(jsonPath("$.components.schemas.NullableReferenceResponse.properties.child.allOf[0]['$ref']", + is("#/components/schemas/NullableReferenceChild"))); + } + + @SpringBootApplication + static class SpringDocTestApp { + } +}