Skip to content

Commit a367617

Browse files
committed
Merge branch 'jakvbs/fix-consumes-and-produces' of https://github.com/jakvbs/springdoc-openapi into jakvbs-jakvbs/fix-consumes-and-produces
2 parents c849b98 + d6da952 commit a367617

File tree

4 files changed

+162
-21
lines changed

4 files changed

+162
-21
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/models/MethodAttributes.java

+35-21
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,22 @@
2525
package org.springdoc.core.models;
2626

2727
import java.lang.reflect.Method;
28+
import java.util.Arrays;
29+
import java.util.HashSet;
2830
import java.util.LinkedHashMap;
2931
import java.util.Locale;
3032
import java.util.Map;
3133

3234
import com.fasterxml.jackson.annotation.JsonView;
3335
import io.swagger.v3.oas.models.responses.ApiResponse;
3436
import io.swagger.v3.oas.models.responses.ApiResponses;
37+
import java.util.Set;
38+
import java.util.stream.Collectors;
3539
import org.apache.commons.lang3.ArrayUtils;
3640
import org.apache.commons.lang3.StringUtils;
3741

42+
import org.jetbrains.annotations.Nullable;
3843
import org.springframework.core.annotation.AnnotatedElementUtils;
39-
import org.springframework.util.CollectionUtils;
4044
import org.springframework.web.bind.annotation.DeleteMapping;
4145
import org.springframework.web.bind.annotation.GetMapping;
4246
import org.springframework.web.bind.annotation.PostMapping;
@@ -275,28 +279,38 @@ else if (reqMappingClass != null) {
275279
* @param headers the headers
276280
*/
277281
private void fillMethods(String[] produces, String[] consumes, String[] headers) {
278-
if (ArrayUtils.isEmpty(methodProduces)) {
279-
if (ArrayUtils.isNotEmpty(produces))
280-
methodProduces = produces;
281-
else if (ArrayUtils.isNotEmpty(classProduces))
282-
methodProduces = classProduces;
283-
else
284-
methodProduces = new String[] { defaultProducesMediaType };
285-
}
286-
287-
if (ArrayUtils.isEmpty(methodConsumes)) {
288-
if (ArrayUtils.isNotEmpty(consumes))
289-
methodConsumes = consumes;
290-
else if (ArrayUtils.isNotEmpty(classConsumes))
291-
methodConsumes = classConsumes;
292-
else
293-
methodConsumes = new String[] { defaultConsumesMediaType };
294-
}
295-
296-
if (CollectionUtils.isEmpty(this.headers))
297-
setHeaders(headers);
282+
if (ArrayUtils.isNotEmpty(produces)) {
283+
methodProduces = mergeArrays(methodProduces, produces);
284+
} else if (ArrayUtils.isNotEmpty(classProduces)) {
285+
methodProduces = mergeArrays(methodProduces, classProduces);
286+
} else if (ArrayUtils.isEmpty(methodProduces)) {
287+
methodProduces = new String[] {defaultProducesMediaType};
288+
}
289+
290+
if (ArrayUtils.isNotEmpty(consumes)) {
291+
methodConsumes = mergeArrays(methodConsumes, consumes);
292+
} else if (ArrayUtils.isNotEmpty(classConsumes)) {
293+
methodConsumes = mergeArrays(methodConsumes, classConsumes);
294+
} else if (ArrayUtils.isEmpty(methodConsumes)) {
295+
methodConsumes = new String[] {defaultConsumesMediaType};
296+
}
297+
298+
setHeaders(headers);
298299
}
299300

301+
/**
302+
* Merge string arrays into one array with unique values
303+
*
304+
* @param array1 the array1
305+
* @param array2 the array2
306+
* @return the string [ ]
307+
*/
308+
private String[] mergeArrays(@Nullable String[] array1, String[] array2) {
309+
Set<String> uniqueValues = array1 == null ? new HashSet<>() : Arrays.stream(array1).collect(Collectors.toSet());
310+
uniqueValues.addAll(Arrays.asList(array2));
311+
return uniqueValues.toArray(new String[0]);
312+
}
313+
300314
/**
301315
* Is method overloaded boolean.
302316
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.v30.app219;
26+
27+
import org.springframework.http.ResponseEntity;
28+
import org.springframework.web.bind.annotation.RequestBody;
29+
import org.springframework.web.bind.annotation.RequestMapping;
30+
import org.springframework.web.bind.annotation.RequestMethod;
31+
import org.springframework.web.bind.annotation.RestController;
32+
33+
@RestController
34+
@RequestMapping(value = "/api", produces = {"application/xml"}, consumes = {"application/json"})
35+
public class HelloController {
36+
37+
@RequestMapping(value = "/testpost", method = RequestMethod.POST, produces = {"application/json"},
38+
consumes = {"application/json;charset=UTF-8", "application/json; charset=UTF-8"})
39+
public ResponseEntity<TestObject> testpost(@RequestBody TestObject dto) {
40+
return ResponseEntity.ok(dto);
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.v30.app219;
26+
27+
import org.springframework.boot.autoconfigure.SpringBootApplication;
28+
import org.springframework.test.context.ActiveProfiles;
29+
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
30+
31+
@ActiveProfiles("219")
32+
public class SpringDocApp219Test extends AbstractSpringDocV30Test {
33+
34+
@SpringBootApplication
35+
static class SpringDocTestApp {}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.v30.app219;
26+
27+
import java.time.LocalDateTime;
28+
29+
public class TestObject {
30+
public String stringValue;
31+
32+
public LocalDateTime localDateTime;
33+
34+
public String getStringValue() {
35+
return stringValue;
36+
}
37+
38+
public void setStringValue(String stringValue) {
39+
this.stringValue = stringValue;
40+
}
41+
42+
public LocalDateTime getLocalDateTime() {
43+
return localDateTime;
44+
}
45+
46+
public void setLocalDateTime(LocalDateTime localDateTime) {
47+
this.localDateTime = localDateTime;
48+
}
49+
}

0 commit comments

Comments
 (0)