Skip to content

Commit 1a10e35

Browse files
committed
Cannot referenced example in RequestBody. Fixes #1276
1 parent 1591a01 commit 1a10e35

File tree

8 files changed

+363
-3
lines changed

8 files changed

+363
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ public Optional<RequestBody> buildRequestBodyFromDoc(
124124
* @param requestBodyObject the request body object
125125
*/
126126
private void buildResquestBodyContent(io.swagger.v3.oas.annotations.parameters.RequestBody requestBody, RequestBody requestBodyOp, MethodAttributes methodAttributes, Components components, JsonView jsonViewAnnotation, String[] classConsumes, String[] methodConsumes, RequestBody requestBodyObject) {
127-
Optional<Content> optionalContent = AnnotationsUtils
128-
.getContent(requestBody.content(), getConsumes(classConsumes),
127+
Optional<Content> optionalContent = SpringDocAnnotationsUtils
128+
.getContent(requestBody.content(),getConsumes(classConsumes),
129129
getConsumes(methodConsumes), null, components, jsonViewAnnotation);
130130
if (requestBodyOp == null) {
131131
if (optionalContent.isPresent()) {

springdoc-openapi-common/src/main/java/org/springdoc/core/SpringDocAnnotationsUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public static Optional<Content> getContent(io.swagger.v3.oas.annotations.media.C
169169
content.addMediaType(annotationContent.mediaType(), mediaType);
170170
}
171171
else {
172-
if (mediaType.getSchema() != null)
172+
if (mediaType.getSchema() != null || mediaType.getEncoding() != null || mediaType.getExample() != null || mediaType.getExamples() != null || mediaType.getExtensions() != null)
173173
applyTypes(classTypes, methodTypes, content, mediaType);
174174
}
175175
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 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.app163;
20+
21+
import io.swagger.v3.oas.annotations.media.Schema;
22+
23+
@Schema
24+
public class CommissionDto {
25+
private String email;
26+
27+
private String firstName;
28+
29+
private String lastName;
30+
31+
public CommissionDto() {
32+
}
33+
34+
public CommissionDto(final String email, final String firstName, final String lastName) {
35+
this.email = email;
36+
this.firstName = firstName;
37+
this.lastName = lastName;
38+
}
39+
40+
public String getEmail() {
41+
return email;
42+
}
43+
44+
public void setEmail(final String email) {
45+
this.email = email;
46+
}
47+
48+
public String getFirstName() {
49+
return firstName;
50+
}
51+
52+
public void setFirstName(final String firstName) {
53+
this.firstName = firstName;
54+
}
55+
56+
public String getLastName() {
57+
return lastName;
58+
}
59+
60+
public void setLastName(final String lastName) {
61+
this.lastName = lastName;
62+
}
63+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
*
3+
* * Copyright 2019-2021 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.app163;
20+
21+
import javax.validation.Valid;
22+
23+
import io.swagger.v3.oas.annotations.Operation;
24+
import io.swagger.v3.oas.annotations.Parameter;
25+
import io.swagger.v3.oas.annotations.media.Content;
26+
import io.swagger.v3.oas.annotations.media.ExampleObject;
27+
import io.swagger.v3.oas.annotations.media.Schema;
28+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
29+
30+
import org.springframework.http.ResponseEntity;
31+
import org.springframework.web.bind.annotation.PathVariable;
32+
import org.springframework.web.bind.annotation.PutMapping;
33+
import org.springframework.web.bind.annotation.RequestBody;
34+
import org.springframework.web.bind.annotation.RestController;
35+
36+
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
37+
import static org.springframework.http.ResponseEntity.accepted;
38+
import static test.org.springdoc.api.app163.Examples.PUT_COMMISSION_REQUEST_BODY_EXAMPLE_KEY;
39+
import static test.org.springdoc.api.app163.Examples.PUT_COMMISSION_RESPONSE_BODY_EXAMPLE_KEY;
40+
41+
@RestController
42+
public class CommissionsResource {
43+
44+
@PutMapping(value = "{id}", consumes = APPLICATION_JSON_VALUE)
45+
@Operation(description = "updateCommission", summary = "Update a commission")
46+
@ApiResponse(responseCode = "202", description = "Commission updated", content = @Content(schema = @Schema(implementation = CommissionDto.class), examples = @ExampleObject(name = "202", ref = PUT_COMMISSION_RESPONSE_BODY_EXAMPLE_KEY)))
47+
public ResponseEntity<CommissionDto> updateCommission(
48+
@Parameter(description = "Commission's id", required = true) @PathVariable("id") String commissionId,
49+
@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "A commission to update", required = true, content = @Content(schema = @Schema(implementation = CommissionDto.class), examples = @ExampleObject(name="requestExample", ref = PUT_COMMISSION_REQUEST_BODY_EXAMPLE_KEY))) @RequestBody(required = true) @Valid CommissionDto commission) {
50+
return accepted().body(commission);
51+
}
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package test.org.springdoc.api.app163;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
import io.swagger.v3.oas.models.OpenAPI;
7+
import io.swagger.v3.oas.models.examples.Example;
8+
import org.springdoc.core.customizers.OpenApiCustomiser;
9+
10+
import org.springframework.stereotype.Component;
11+
12+
@Component
13+
public class ExampleRegistrationCustomizer implements OpenApiCustomiser {
14+
15+
private final List<Map.Entry<String, Example>> examplesToRegister;
16+
17+
public ExampleRegistrationCustomizer(List<Map.Entry<String, Example>> examplesToRegister) {
18+
this.examplesToRegister = examplesToRegister;
19+
}
20+
21+
@Override
22+
public void customise(OpenAPI openApi) {
23+
examplesToRegister.forEach(entry -> openApi.getComponents().addExamples(entry.getKey(), entry.getValue()));
24+
}
25+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 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.app163;
20+
21+
import java.util.AbstractMap;
22+
import java.util.Map;
23+
24+
import io.swagger.v3.oas.models.examples.Example;
25+
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Configuration;
28+
29+
/**
30+
* This class contributes OpenAPI <a href=
31+
* "https://swagger.io/docs/specification/adding-examples/">examples</a>.
32+
*
33+
* Each example is contributed through a bean typed as a map entry
34+
* (Map.Entry<String, Example>) and referenced through a constant by a resource
35+
* operation's @ApiResponse.
36+
*/
37+
@Configuration
38+
public class Examples {
39+
40+
public static final String PREFIX = "#/components/examples/";
41+
42+
public static final String PUT_COMMISSION_REQUEST_BODY_EXAMPLE_KEY = "httpPutCommissionRequestBodyExample";
43+
public static final String PUT_COMMISSION_REQUEST_BODY_EXAMPLE = PREFIX + PUT_COMMISSION_REQUEST_BODY_EXAMPLE_KEY;
44+
45+
public static final String PUT_COMMISSION_RESPONSE_BODY_EXAMPLE_KEY = "httpPutCommissionResponseBodyExample";
46+
public static final String PUT_COMMISSION_RESPONSE_BODY_EXAMPLE = PREFIX + PUT_COMMISSION_RESPONSE_BODY_EXAMPLE_KEY;
47+
48+
@Bean
49+
public Map.Entry<String, Example> httpPutCommissionRequestBodyExample() {
50+
Example httpPutCommissionRequestBodyExample = new Example();
51+
Map.Entry<String, Example> entry = new AbstractMap.SimpleEntry<>(PUT_COMMISSION_REQUEST_BODY_EXAMPLE_KEY,
52+
httpPutCommissionRequestBodyExample);
53+
httpPutCommissionRequestBodyExample
54+
.setSummary("HTTP 202 JSON Body request example for updateCommission operation");
55+
56+
CommissionDto commission = new CommissionDto("esteban@dugueperoux.com", "Esteban",
57+
"DUGUEPEROUX");
58+
59+
httpPutCommissionRequestBodyExample.setValue(commission);
60+
61+
return entry;
62+
}
63+
64+
@Bean
65+
public Map.Entry<String, Example> httpPutCommissionResponseBodyExample() {
66+
Example httpPutCommissionResponseBodyExample = new Example();
67+
Map.Entry<String, Example> entry = new AbstractMap.SimpleEntry<>(PUT_COMMISSION_RESPONSE_BODY_EXAMPLE_KEY,
68+
httpPutCommissionResponseBodyExample);
69+
httpPutCommissionResponseBodyExample
70+
.setSummary("HTTP 202 JSON Body response example for updateCommission operation");
71+
72+
CommissionDto commission = new CommissionDto("esteban@dugueperoux.com", "Esteban",
73+
"DUGUEPEROUX");
74+
75+
httpPutCommissionResponseBodyExample.setValue(commission);
76+
77+
return entry;
78+
}
79+
80+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
*
3+
* * Copyright 2019-2021 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.app163;
20+
21+
import test.org.springdoc.api.AbstractSpringDocTest;
22+
23+
import org.springframework.boot.autoconfigure.SpringBootApplication;
24+
import org.springframework.context.annotation.Import;
25+
26+
public class SpringDocApp163Test extends AbstractSpringDocTest {
27+
28+
@SpringBootApplication
29+
@Import({ Examples.class, ExampleRegistrationCustomizer.class })
30+
static class SpringDocTestApp {
31+
32+
}
33+
34+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
"paths": {
14+
"/{id}": {
15+
"put": {
16+
"tags": [
17+
"commissions-resource"
18+
],
19+
"summary": "Update a commission",
20+
"description": "updateCommission",
21+
"operationId": "updateCommission",
22+
"parameters": [
23+
{
24+
"name": "id",
25+
"in": "path",
26+
"description": "Commission's id",
27+
"required": true,
28+
"schema": {
29+
"type": "string"
30+
}
31+
}
32+
],
33+
"requestBody": {
34+
"description": "A commission to update",
35+
"content": {
36+
"application/json": {
37+
"schema": {
38+
"$ref": "#/components/schemas/CommissionDto"
39+
},
40+
"examples": {
41+
"requestExample": {
42+
"$ref": "#/components/examples/httpPutCommissionRequestBodyExample"
43+
}
44+
}
45+
}
46+
},
47+
"required": true
48+
},
49+
"responses": {
50+
"202": {
51+
"description": "Commission updated",
52+
"content": {
53+
"*/*": {
54+
"schema": {
55+
"$ref": "#/components/schemas/CommissionDto"
56+
},
57+
"examples": {
58+
"202": {
59+
"$ref": "#/components/examples/httpPutCommissionResponseBodyExample"
60+
}
61+
}
62+
}
63+
}
64+
}
65+
}
66+
}
67+
}
68+
},
69+
"components": {
70+
"schemas": {
71+
"CommissionDto": {
72+
"type": "object",
73+
"properties": {
74+
"email": {
75+
"type": "string"
76+
},
77+
"firstName": {
78+
"type": "string"
79+
},
80+
"lastName": {
81+
"type": "string"
82+
}
83+
}
84+
}
85+
},
86+
"examples": {
87+
"httpPutCommissionRequestBodyExample": {
88+
"summary": "HTTP 202 JSON Body request example for updateCommission operation",
89+
"value": {
90+
"email": "esteban@dugueperoux.com",
91+
"firstName": "Esteban",
92+
"lastName": "DUGUEPEROUX"
93+
}
94+
},
95+
"httpPutCommissionResponseBodyExample": {
96+
"summary": "HTTP 202 JSON Body response example for updateCommission operation",
97+
"value": {
98+
"email": "esteban@dugueperoux.com",
99+
"firstName": "Esteban",
100+
"lastName": "DUGUEPEROUX"
101+
}
102+
}
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)