From 71454f018f7b1622e52f62f9b38da421639db84f Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 9 Mar 2023 15:44:54 +0900 Subject: [PATCH] Fixed a bug that a NullPointerException is thrown when the description field of RequestBody is null and there is a javadoc description. Fixes #2089 --- .../springdoc/core/RequestBodyService.java | 2 +- ...ionFieldInRequestBodyIsNullController.java | 45 +++++++++++++ .../test/org/springdoc/api/app168/Person.java | 29 +++++++++ .../api/app168/SpringDocApp168Test.java | 36 +++++++++++ .../src/test/resources/results/app168.json | 63 +++++++++++++++++++ 5 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app168/DescriptionFieldInRequestBodyIsNullController.java create mode 100644 springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app168/Person.java create mode 100644 springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app168/SpringDocApp168Test.java create mode 100644 springdoc-openapi-javadoc/src/test/resources/results/app168.json diff --git a/springdoc-openapi-common/src/main/java/org/springdoc/core/RequestBodyService.java b/springdoc-openapi-common/src/main/java/org/springdoc/core/RequestBodyService.java index 3735de3b2..98a5e22a7 100644 --- a/springdoc-openapi-common/src/main/java/org/springdoc/core/RequestBodyService.java +++ b/springdoc-openapi-common/src/main/java/org/springdoc/core/RequestBodyService.java @@ -287,7 +287,7 @@ else if (!methodAttributes.isWithResponseBodySchemaDoc()) { && parameterBuilder.isRequestBodyPresent(parameterInfo)) { String paramJavadocDescription = parameterBuilder.getParamJavadoc(parameterBuilder.getJavadocProvider(), parameterInfo.getMethodParameter()); if (!StringUtils.isBlank(paramJavadocDescription)) { - requestBodyInfo.getRequestBody().setDescription(paramJavadocDescription); + requestBody.setDescription(paramJavadocDescription); } } return requestBody; diff --git a/springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app168/DescriptionFieldInRequestBodyIsNullController.java b/springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app168/DescriptionFieldInRequestBodyIsNullController.java new file mode 100644 index 000000000..d3a2cb85e --- /dev/null +++ b/springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app168/DescriptionFieldInRequestBodyIsNullController.java @@ -0,0 +1,45 @@ +/* + * + * * Copyright 2019-2023 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.app168; + +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; + +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * If the RequestBody description field is null, get the description from the javadoc. + */ +@RestController +@RequestMapping("description-in-requestbody-is-null") +public class DescriptionFieldInRequestBodyIsNullController { + + /** + * Person person. + * + * @param person the person + */ + @PostMapping + public void person( + @io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(schema = @Schema(implementation = Person.class))) @RequestBody Person person) { + } +} diff --git a/springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app168/Person.java b/springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app168/Person.java new file mode 100644 index 000000000..4103c0ad0 --- /dev/null +++ b/springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app168/Person.java @@ -0,0 +1,29 @@ +package test.org.springdoc.api.app168; + +/** + * The type Person. + */ +public class Person { + /** + * The Id. + */ + private long id; + + /** + * Gets id. + * + * @return the id + */ + public long getId() { + return id; + } + + /** + * Sets id. + * + * @param id the id + */ + public void setId(long id) { + this.id = id; + } +} diff --git a/springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app168/SpringDocApp168Test.java b/springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app168/SpringDocApp168Test.java new file mode 100644 index 000000000..5a86ed683 --- /dev/null +++ b/springdoc-openapi-javadoc/src/test/java/test/org/springdoc/api/app168/SpringDocApp168Test.java @@ -0,0 +1,36 @@ +/* + * + * * Copyright 2019-2023 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.app168; + +import test.org.springdoc.api.AbstractSpringDocTest; + +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * The type Spring doc app 168 test. + */ +public class SpringDocApp168Test extends AbstractSpringDocTest { + + /** + * The type Spring doc test app. + */ + @SpringBootApplication + static class SpringDocTestApp { + } +} diff --git a/springdoc-openapi-javadoc/src/test/resources/results/app168.json b/springdoc-openapi-javadoc/src/test/resources/results/app168.json new file mode 100644 index 000000000..3bbe11c68 --- /dev/null +++ b/springdoc-openapi-javadoc/src/test/resources/results/app168.json @@ -0,0 +1,63 @@ +{ + "openapi": "3.0.1", + "info": { + "title": "OpenAPI definition", + "version": "v0" + }, + "servers": [ + { + "url": "http://localhost", + "description": "Generated server url" + } + ], + "tags": [ + { + "name": "description-field-in-request-body-is-null-controller", + "description": "If the RequestBody description field is null, get the description from the javadoc." + } + ], + "paths": { + "/description-in-requestbody-is-null": { + "post": { + "tags": [ + "description-field-in-request-body-is-null-controller" + ], + "operationId": "person", + "summary": "Person person.", + "description": "Person person.", + "operationId": "person", + "requestBody": { + "description": "the person", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Person" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "OK" + } + } + } + } + }, + "components": { + "schemas": { + "Person": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "description": "The Id.", + "format": "int64" + } + }, + "description": "The type Person." + } + } + } +}