Skip to content

Commit 4c89038

Browse files
committed
Fixed a bug that a NullPointerException is thrown when the description field of RequestBody is null and there is a javadoc description. Fixes springdoc#2089
1 parent c664a83 commit 4c89038

File tree

5 files changed

+174
-2
lines changed

5 files changed

+174
-2
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/RequestBodyService.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,12 @@ else if (!methodAttributes.isWithResponseBodySchemaDoc()) {
288288
mergeContent(requestBody, methodAttributes, schema);
289289
}
290290

291-
292291
// Add requestBody javadoc
293292
if (StringUtils.isBlank(requestBody.getDescription()) && parameterBuilder.getJavadocProvider() != null
294293
&& parameterBuilder.isRequestBodyPresent(parameterInfo)) {
295294
String paramJavadocDescription = parameterBuilder.getParamJavadoc(parameterBuilder.getJavadocProvider(), parameterInfo.getMethodParameter());
296295
if (!StringUtils.isBlank(paramJavadocDescription)) {
297-
requestBodyInfo.getRequestBody().setDescription(paramJavadocDescription);
296+
requestBody.setDescription(paramJavadocDescription);
298297
}
299298
}
300299
return requestBody;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
*
3+
* * Copyright 2019-2023 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app168;
20+
21+
import io.swagger.v3.oas.annotations.media.Content;
22+
import io.swagger.v3.oas.annotations.media.Schema;
23+
24+
import org.springframework.web.bind.annotation.PostMapping;
25+
import org.springframework.web.bind.annotation.RequestBody;
26+
import org.springframework.web.bind.annotation.RequestMapping;
27+
import org.springframework.web.bind.annotation.RestController;
28+
29+
/**
30+
* If the RequestBody description field is null, get the description from the javadoc.
31+
*/
32+
@RestController
33+
@RequestMapping("description-in-requestbody-is-null")
34+
public class DescriptionFieldInRequestBodyIsNullController {
35+
36+
/**
37+
* Person person.
38+
*
39+
* @param person the person
40+
*/
41+
@PostMapping
42+
public void person(
43+
@io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(schema = @Schema(implementation = Person.class))) @RequestBody Person person) {
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package test.org.springdoc.api.app168;
2+
3+
/**
4+
* The type Person.
5+
*/
6+
public class Person {
7+
/**
8+
* The Id.
9+
*/
10+
private long id;
11+
12+
/**
13+
* Gets id.
14+
*
15+
* @return the id
16+
*/
17+
public long getId() {
18+
return id;
19+
}
20+
21+
/**
22+
* Sets id.
23+
*
24+
* @param id the id
25+
*/
26+
public void setId(long id) {
27+
this.id = id;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
*
3+
* * Copyright 2019-2023 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app168;
20+
21+
import test.org.springdoc.api.AbstractSpringDocTest;
22+
23+
import org.springframework.boot.autoconfigure.SpringBootApplication;
24+
25+
/**
26+
* The type Spring doc app 168 test.
27+
*/
28+
public class SpringDocApp168Test extends AbstractSpringDocTest {
29+
30+
/**
31+
* The type Spring doc test app.
32+
*/
33+
@SpringBootApplication
34+
static class SpringDocTestApp {
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"tags": [
14+
{
15+
"name": "description-field-in-request-body-is-null-controller",
16+
"description": "If the RequestBody description field is null, get the description from the javadoc."
17+
}
18+
],
19+
"paths": {
20+
"/description-in-requestbody-is-null": {
21+
"post": {
22+
"tags": [
23+
"description-field-in-request-body-is-null-controller"
24+
],
25+
"operationId": "person",
26+
"summary": "Person person.",
27+
"description": "Person person.",
28+
"operationId": "person",
29+
"requestBody": {
30+
"description": "the person",
31+
"content": {
32+
"application/json": {
33+
"schema": {
34+
"$ref": "#/components/schemas/Person"
35+
}
36+
}
37+
},
38+
"required": true
39+
},
40+
"responses": {
41+
"200": {
42+
"description": "OK"
43+
}
44+
}
45+
}
46+
}
47+
},
48+
"components": {
49+
"schemas": {
50+
"Person": {
51+
"type": "object",
52+
"properties": {
53+
"id": {
54+
"type": "integer",
55+
"description": "The Id.",
56+
"format": "int64"
57+
}
58+
},
59+
"description": "The type Person."
60+
}
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)