This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new fe4c0f36d3b CAMEL-6766 Generated produces string now includes media types from all responses (#16221) fe4c0f36d3b is described below commit fe4c0f36d3b6755ce97d57c097b5403cdd6786c0 Author: Calle <calleanders...@users.noreply.github.com> AuthorDate: Mon Nov 11 19:41:59 2024 +0100 CAMEL-6766 Generated produces string now includes media types from all responses (#16221) * CAMEL-21430 Generated produces string now includes media types from all responses * CAMEL-21430 Formatting code. --- .../camel/component/rest/openapi/OpenApiUtils.java | 7 ++- .../component/rest/openapi/OpenApiUtilsTest.java | 54 ++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/OpenApiUtils.java b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/OpenApiUtils.java index c5ed4bdcbdb..77c860f19d9 100644 --- a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/OpenApiUtils.java +++ b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/OpenApiUtils.java @@ -92,12 +92,17 @@ public class OpenApiUtils { public String getProduces(Operation operation) { // the operation may have specific information what it can produce if (operation.getResponses() != null) { + HashSet<String> mediaTypes = new HashSet<>(); for (var apiResponse : operation.getResponses().values()) { Content content = apiResponse.getContent(); if (content != null) { - return content.keySet().stream().sorted().collect(Collectors.joining(",")); + mediaTypes.addAll(content.keySet()); } } + + if (!mediaTypes.isEmpty()) { + return mediaTypes.stream().sorted().collect(Collectors.joining(",")); + } } return null; } diff --git a/components/camel-rest-openapi/src/test/java/org/apache/camel/component/rest/openapi/OpenApiUtilsTest.java b/components/camel-rest-openapi/src/test/java/org/apache/camel/component/rest/openapi/OpenApiUtilsTest.java new file mode 100644 index 00000000000..ee302af3825 --- /dev/null +++ b/components/camel-rest-openapi/src/test/java/org/apache/camel/component/rest/openapi/OpenApiUtilsTest.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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 org.apache.camel.component.rest.openapi; + +import io.swagger.v3.oas.models.Operation; +import io.swagger.v3.oas.models.media.Content; +import io.swagger.v3.oas.models.media.MediaType; +import io.swagger.v3.oas.models.responses.ApiResponse; +import io.swagger.v3.oas.models.responses.ApiResponses; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class OpenApiUtilsTest { + @Test + public void shouldReturnAllProduces() { + Operation operation = new Operation(); + + ApiResponses responses = new ApiResponses(); + responses.addApiResponse("200", createResponse("application/json", "application/xml")); + responses.addApiResponse("400", createResponse("application/problem+json")); + responses.addApiResponse("404", createResponse("application/problem+json")); + operation.setResponses(responses); + + OpenApiUtils utils = new OpenApiUtils(null, null, null); + assertThat(utils.getProduces(operation)).isEqualTo("application/json,application/problem+json,application/xml"); + } + + private ApiResponse createResponse(String... contentTypes) { + ApiResponse response = new ApiResponse(); + + Content content = new Content(); + for (String contentType : contentTypes) { + content.addMediaType(contentType, new MediaType()); + } + response.setContent(content); + + return response; + } +}