Skip to content

Fix consumes and produces calculation. Fixes #2596 #2600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@
package org.springdoc.core.models;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import org.jetbrains.annotations.Nullable;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -275,28 +279,38 @@ else if (reqMappingClass != null) {
* @param headers the headers
*/
private void fillMethods(String[] produces, String[] consumes, String[] headers) {
if (ArrayUtils.isEmpty(methodProduces)) {
if (ArrayUtils.isNotEmpty(produces))
methodProduces = produces;
else if (ArrayUtils.isNotEmpty(classProduces))
methodProduces = classProduces;
else
methodProduces = new String[] { defaultProducesMediaType };
}

if (ArrayUtils.isEmpty(methodConsumes)) {
if (ArrayUtils.isNotEmpty(consumes))
methodConsumes = consumes;
else if (ArrayUtils.isNotEmpty(classConsumes))
methodConsumes = classConsumes;
else
methodConsumes = new String[] { defaultConsumesMediaType };
}

if (CollectionUtils.isEmpty(this.headers))
setHeaders(headers);
if (ArrayUtils.isNotEmpty(produces)) {
methodProduces = mergeArrays(methodProduces, produces);
} else if (ArrayUtils.isNotEmpty(classProduces)) {
methodProduces = mergeArrays(methodProduces, classProduces);
} else if (ArrayUtils.isEmpty(methodProduces)) {
methodProduces = new String[] {defaultProducesMediaType};
}

if (ArrayUtils.isNotEmpty(consumes)) {
methodConsumes = mergeArrays(methodConsumes, consumes);
} else if (ArrayUtils.isNotEmpty(classConsumes)) {
methodConsumes = mergeArrays(methodConsumes, classConsumes);
} else if (ArrayUtils.isEmpty(methodConsumes)) {
methodConsumes = new String[] {defaultConsumesMediaType};
}

setHeaders(headers);
}

/**
* Merge string arrays into one array with unique values
*
* @param array1 the array1
* @param array2 the array2
* @return the string [ ]
*/
private String[] mergeArrays(@Nullable String[] array1, String[] array2) {
Set<String> uniqueValues = array1 == null ? new HashSet<>() : Arrays.stream(array1).collect(Collectors.toSet());
uniqueValues.addAll(Arrays.asList(array2));
return uniqueValues.toArray(new String[0]);
}

/**
* Is method overloaded boolean.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
*
* *
* * *
* * * *
* * * * * 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.v30.app219;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/api", produces = {"application/xml"}, consumes = {"application/json"})
public class HelloController {

@RequestMapping(value = "/testpost", method = RequestMethod.POST, produces = {"application/json"},
consumes = {"application/json;charset=UTF-8", "application/json; charset=UTF-8"})
public ResponseEntity<TestObject> testpost(@RequestBody TestObject dto) {
return ResponseEntity.ok(dto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
*
* *
* * *
* * * *
* * * * * 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.v30.app219;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.test.context.ActiveProfiles;
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;

@ActiveProfiles("219")
public class SpringDocApp219Test extends AbstractSpringDocV30Test {

@SpringBootApplication
static class SpringDocTestApp {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
*
* *
* * *
* * * *
* * * * * 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.v30.app219;

import java.time.LocalDateTime;

public class TestObject {
public String stringValue;

public LocalDateTime localDateTime;

public String getStringValue() {
return stringValue;
}

public void setStringValue(String stringValue) {
this.stringValue = stringValue;
}

public LocalDateTime getLocalDateTime() {
return localDateTime;
}

public void setLocalDateTime(LocalDateTime localDateTime) {
this.localDateTime = localDateTime;
}
}