Skip to content

Commit a70f518

Browse files
committed
All request parameters marked as required for Java controllers in mixed projects in 2.0.3. Fixes #2170
1 parent 57fb0f6 commit a70f518

File tree

5 files changed

+116
-9
lines changed

5 files changed

+116
-9
lines changed

springdoc-openapi-kotlin/src/main/java/org/springdoc/kotlin/SpringDocKotlinConfiguration.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class SpringDocKotlinConfiguration(objectMapperProvider: ObjectMapperProvider) {
8888
if (parameterDoc != null && parameterDoc.required)
8989
parameterModel.required = parameterDoc.required
9090
// parameter is not required if a default value is provided in @RequestParam
91-
else if (requestParam != null && requestParam.defaultValue != ValueConstants.DEFAULT_NONE)
91+
else if (requestParam != null && ((requestParam.defaultValue != ValueConstants.DEFAULT_NONE) || !requestParam.required))
9292
parameterModel.required = false
9393
else
9494
parameterModel.required = kParameter.type.isMarkedNullable == false

springdoc-openapi-kotlin/src/test/java/test/org/springdoc/api/AbstractSpringDocTest.java

+19-8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
import org.junit.jupiter.api.Test;
2727
import org.skyscreamer.jsonassert.JSONAssert;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
2830
import org.springdoc.core.Constants;
2931

3032
import org.springframework.beans.factory.annotation.Autowired;
@@ -33,12 +35,15 @@
3335
import org.springframework.test.web.reactive.server.EntityExchangeResult;
3436
import org.springframework.test.web.reactive.server.WebTestClient;
3537

38+
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
39+
3640
@WebFluxTest
3741
@ActiveProfiles("test")
3842
public abstract class AbstractSpringDocTest {
3943

4044
@Autowired
4145
private WebTestClient webTestClient;
46+
protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractSpringDocTest.class);
4247

4348
public static String getContent(String fileName) {
4449
try {
@@ -53,14 +58,20 @@ public static String getContent(String fileName) {
5358

5459
@Test
5560
public void testApp() throws Exception {
56-
EntityExchangeResult<byte[]> getResult = webTestClient.get().uri(Constants.DEFAULT_API_DOCS_URL).exchange()
57-
.expectStatus().isOk().expectBody().returnResult();
58-
59-
String result = new String(getResult.getResponseBody());
60-
String className = getClass().getSimpleName();
61-
String testNumber = className.replaceAll("[^0-9]", "");
61+
String result = null;
62+
try {
63+
EntityExchangeResult<byte[]> getResult = webTestClient.get().uri(Constants.DEFAULT_API_DOCS_URL).exchange()
64+
.expectStatus().isOk().expectBody().returnResult();
6265

63-
String expected = getContent("results/app" + testNumber + ".json");
64-
JSONAssert.assertEquals(expected, result, true);
66+
result = new String(getResult.getResponseBody());
67+
String className = getClass().getSimpleName();
68+
String testNumber = className.replaceAll("[^0-9]", "");
69+
String expected = getContent("results/app" + testNumber + ".json");
70+
assertEquals(expected, result, true);
71+
}
72+
catch (java.lang.AssertionError e) {
73+
LOGGER.error(result);
74+
throw e;
75+
}
6576
}
6677
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package test.org.springdoc.api.app21;
2+
3+
import io.swagger.v3.oas.annotations.Parameter;
4+
import io.swagger.v3.oas.annotations.tags.Tag;
5+
6+
import org.springframework.http.MediaType;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestParam;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
@Tag(name = "Test")
13+
@RestController
14+
@RequestMapping(value = "/api/v2/test", produces = MediaType.APPLICATION_JSON_VALUE)
15+
public class HelloController {
16+
17+
@GetMapping("/")
18+
public void greet(@RequestParam(required = false) @Parameter(required = false) final String name) {
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * *
6+
* * * * * Copyright 2019-2022 the original author or authors.
7+
* * * * *
8+
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
9+
* * * * * you may not use this file except in compliance with the License.
10+
* * * * * You may obtain a copy of the License at
11+
* * * * *
12+
* * * * * https://www.apache.org/licenses/LICENSE-2.0
13+
* * * * *
14+
* * * * * Unless required by applicable law or agreed to in writing, software
15+
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
16+
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* * * * * See the License for the specific language governing permissions and
18+
* * * * * limitations under the License.
19+
* * * *
20+
* * *
21+
* *
22+
*
23+
*/
24+
25+
package test.org.springdoc.api.app21;
26+
import test.org.springdoc.api.AbstractSpringDocTest;
27+
28+
import org.springframework.boot.autoconfigure.SpringBootApplication;
29+
import org.springframework.context.annotation.ComponentScan;
30+
31+
32+
public class SpringDocApp21Test extends AbstractSpringDocTest {
33+
34+
@SpringBootApplication
35+
@ComponentScan(basePackages = { "org.springdoc", "test.org.springdoc.api.app21" })
36+
static class SpringDocTestApp {}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/api/v2/test/": {
15+
"get": {
16+
"tags": [
17+
"Test"
18+
],
19+
"operationId": "greet",
20+
"parameters": [
21+
{
22+
"name": "name",
23+
"in": "query",
24+
"required": false,
25+
"schema": {
26+
"type": "string"
27+
}
28+
}
29+
],
30+
"responses": {
31+
"200": {
32+
"description": "OK"
33+
}
34+
}
35+
}
36+
}
37+
},
38+
"components": {}
39+
}

0 commit comments

Comments
 (0)