Skip to content

Commit

Permalink
All request parameters marked as required for Java controllers in mix…
Browse files Browse the repository at this point in the history
…ed projects in 2.0.3. Fixes #2170
  • Loading branch information
bnasslahsen committed Mar 30, 2023
1 parent 57fb0f6 commit a70f518
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class SpringDocKotlinConfiguration(objectMapperProvider: ObjectMapperProvider) {
if (parameterDoc != null && parameterDoc.required)
parameterModel.required = parameterDoc.required
// parameter is not required if a default value is provided in @RequestParam
else if (requestParam != null && requestParam.defaultValue != ValueConstants.DEFAULT_NONE)
else if (requestParam != null && ((requestParam.defaultValue != ValueConstants.DEFAULT_NONE) || !requestParam.required))
parameterModel.required = false
else
parameterModel.required = kParameter.type.isMarkedNullable == false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springdoc.core.Constants;

import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -33,12 +35,15 @@
import org.springframework.test.web.reactive.server.EntityExchangeResult;
import org.springframework.test.web.reactive.server.WebTestClient;

import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;

@WebFluxTest
@ActiveProfiles("test")
public abstract class AbstractSpringDocTest {

@Autowired
private WebTestClient webTestClient;
protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractSpringDocTest.class);

public static String getContent(String fileName) {
try {
Expand All @@ -53,14 +58,20 @@ public static String getContent(String fileName) {

@Test
public void testApp() throws Exception {
EntityExchangeResult<byte[]> getResult = webTestClient.get().uri(Constants.DEFAULT_API_DOCS_URL).exchange()
.expectStatus().isOk().expectBody().returnResult();

String result = new String(getResult.getResponseBody());
String className = getClass().getSimpleName();
String testNumber = className.replaceAll("[^0-9]", "");
String result = null;
try {
EntityExchangeResult<byte[]> getResult = webTestClient.get().uri(Constants.DEFAULT_API_DOCS_URL).exchange()
.expectStatus().isOk().expectBody().returnResult();

String expected = getContent("results/app" + testNumber + ".json");
JSONAssert.assertEquals(expected, result, true);
result = new String(getResult.getResponseBody());
String className = getClass().getSimpleName();
String testNumber = className.replaceAll("[^0-9]", "");
String expected = getContent("results/app" + testNumber + ".json");
assertEquals(expected, result, true);
}
catch (java.lang.AssertionError e) {
LOGGER.error(result);
throw e;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package test.org.springdoc.api.app21;

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "Test")
@RestController
@RequestMapping(value = "/api/v2/test", produces = MediaType.APPLICATION_JSON_VALUE)
public class HelloController {

@GetMapping("/")
public void greet(@RequestParam(required = false) @Parameter(required = false) final String name) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
*
* *
* * *
* * * *
* * * * * Copyright 2019-2022 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.app21;
import test.org.springdoc.api.AbstractSpringDocTest;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;


public class SpringDocApp21Test extends AbstractSpringDocTest {

@SpringBootApplication
@ComponentScan(basePackages = { "org.springdoc", "test.org.springdoc.api.app21" })
static class SpringDocTestApp {}
}
39 changes: 39 additions & 0 deletions springdoc-openapi-kotlin/src/test/resources/results/app21.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "",
"description": "Generated server url"
}
],
"paths": {
"/api/v2/test/": {
"get": {
"tags": [
"Test"
],
"operationId": "greet",
"parameters": [
{
"name": "name",
"in": "query",
"required": false,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
}
},
"components": {}
}

0 comments on commit a70f518

Please sign in to comment.