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
commit 4dd276c164d6019a495e93d155c405b67c4ba892 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Wed Nov 23 07:37:26 2022 +0100 CAMEL-18628: REST DSL: Add option to disable REST endpoints. --- .../apache/camel/openapi/RestOpenApiReader.java | 15 +- .../openapi/RestOpenApiReaderDisabledTest.java | 151 +++++++++++++++++++++ .../apache/camel/swagger/RestSwaggerReader.java | 19 ++- .../swagger/RestSwaggerReaderDisabledTest.java | 106 +++++++++++++++ 4 files changed, 286 insertions(+), 5 deletions(-) diff --git a/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiReader.java b/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiReader.java index c9f259abc5e..767bb043a6f 100644 --- a/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiReader.java +++ b/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiReader.java @@ -157,7 +157,10 @@ public class RestOpenApiReader { } for (RestDefinition rest : rests) { - parse(camelContext, openApi, rest, camelContextId, classResolver); + Boolean disabled = CamelContextHelper.parseBoolean(camelContext, rest.getDisabled()); + if (disabled == null || !disabled) { + parse(camelContext, openApi, rest, camelContextId, classResolver); + } } shortenClassNames(openApi); @@ -191,7 +194,15 @@ public class RestOpenApiReader { ClassResolver classResolver) throws ClassNotFoundException { - List<VerbDefinition> verbs = new ArrayList<>(rest.getVerbs()); + // only include enabled verbs + List<VerbDefinition> filter = new ArrayList<>(); + for (VerbDefinition verb : rest.getVerbs()) { + Boolean disabled = CamelContextHelper.parseBoolean(camelContext, verb.getDisabled()); + if (disabled == null || !disabled) { + filter.add(verb); + } + } + List<VerbDefinition> verbs = new ArrayList<>(filter); // must sort the verbs by uri so we group them together when an uri has multiple operations verbs.sort(new VerbOrdering(camelContext)); diff --git a/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderDisabledTest.java b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderDisabledTest.java new file mode 100644 index 00000000000..38b16c86903 --- /dev/null +++ b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderDisabledTest.java @@ -0,0 +1,151 @@ +/* + * 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.openapi; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import io.apicurio.datamodels.Library; +import io.apicurio.datamodels.openapi.models.OasDocument; +import io.apicurio.datamodels.openapi.v2.models.Oas20Info; +import io.apicurio.datamodels.openapi.v3.models.Oas30Info; +import org.apache.camel.BindToRegistry; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.engine.DefaultClassResolver; +import org.apache.camel.model.rest.RestParamType; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class RestOpenApiReaderDisabledTest extends CamelTestSupport { + + private Logger log = LoggerFactory.getLogger(getClass()); + + @BindToRegistry("dummy-rest") + private DummyRestConsumerFactory factory = new DummyRestConsumerFactory(); + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + rest("/hello").consumes("application/json").produces("application/json").get("/hi/{name}") + .description("Saying hi").param().name("name").type(RestParamType.path) + .dataType("string").description("Who is it").example("Donald Duck").endParam() + .param().name("filter").description("Filters to apply to the entity.").type(RestParamType.query) + .dataType("array").arrayType("date-time").endParam().to("log:hi") + .get("/bye/{name}").disabled().description("Saying bye").param().name("name") + .type(RestParamType.path).dataType("string").description("Who is it").example("Donald Duck").endParam() + .responseMessage().code(200).message("A reply number") + .responseModel(float.class).example("success", "123").example("error", "-1").endResponseMessage() + .to("log:bye").post("/bye").disabled("true") + .description("To update the greeting message").consumes("application/xml").produces("application/xml") + .param().name("greeting").type(RestParamType.body) + .dataType("string").description("Message to use as greeting") + .example("application/xml", "<hello>Hi</hello>").endParam().to("log:bye"); + } + }; + } + + @Test + public void testReaderRead() throws Exception { + BeanConfig config = new BeanConfig(); + config.setHost("localhost:8080"); + config.setSchemes(new String[] { "http" }); + config.setBasePath("/api"); + Oas20Info info = new Oas20Info(); + config.setInfo(info); + config.setVersion("2.0"); + RestOpenApiReader reader = new RestOpenApiReader(); + + OasDocument openApi = reader.read(context, context.getRestDefinitions(), config, context.getName(), + new DefaultClassResolver()); + assertNotNull(openApi); + + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.INDENT_OUTPUT); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + Object dump = Library.writeNode(openApi); + String json = mapper.writeValueAsString(dump); + + log.info(json); + + assertTrue(json.contains("\"host\" : \"localhost:8080\"")); + assertTrue(json.contains("\"basePath\" : \"/api\"")); + assertFalse(json.contains("\"/hello/bye\"")); + assertFalse(json.contains("\"summary\" : \"To update the greeting message\"")); + assertFalse(json.contains("\"/hello/bye/{name}\"")); + assertFalse(json.contains("\"/api/hello/bye/{name}\"")); + assertTrue(json.contains("\"/hello/hi/{name}\"")); + assertFalse(json.contains("\"/api/hello/hi/{name}\"")); + assertFalse(json.contains("\"type\" : \"number\"")); + assertFalse(json.contains("\"format\" : \"float\"")); + assertFalse(json.contains("\"application/xml\" : \"<hello>Hi</hello>\"")); + assertTrue(json.contains("\"x-example\" : \"Donald Duck\"")); + assertFalse(json.contains("\"success\" : \"123\"")); + assertFalse(json.contains("\"error\" : \"-1\"")); + assertTrue(json.contains("\"type\" : \"array\"")); + + context.stop(); + } + + @Test + public void testReaderReadV3() throws Exception { + BeanConfig config = new BeanConfig(); + config.setHost("localhost:8080"); + config.setSchemes(new String[] { "http" }); + config.setBasePath("/api"); + Oas30Info info = new Oas30Info(); + config.setInfo(info); + RestOpenApiReader reader = new RestOpenApiReader(); + + OasDocument openApi = reader.read(context, context.getRestDefinitions(), config, context.getName(), + new DefaultClassResolver()); + assertNotNull(openApi); + + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.INDENT_OUTPUT); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + Object dump = Library.writeNode(openApi); + String json = mapper.writeValueAsString(dump); + + log.info(json); + + assertTrue(json.contains("\"url\" : \"http://localhost:8080/api\"")); + assertFalse(json.contains("\"/hello/bye\"")); + assertFalse(json.contains("\"summary\" : \"To update the greeting message\"")); + assertFalse(json.contains("\"/hello/bye/{name}\"")); + assertFalse(json.contains("\"/api/hello/bye/{name}\"")); + assertTrue(json.contains("\"/hello/hi/{name}\"")); + assertFalse(json.contains("\"/api/hello/hi/{name}\"")); + assertFalse(json.contains("\"type\" : \"number\"")); + assertFalse(json.contains("\"format\" : \"float\"")); + assertFalse(json.contains("\"application/xml\" : \"<hello>Hi</hello>\"")); + assertTrue(json.contains("\"x-example\" : \"Donald Duck\"")); + assertFalse(json.contains("\"success\" : \"123\"")); + assertFalse(json.contains("\"error\" : \"-1\"")); + assertTrue(json.contains("\"type\" : \"array\"")); + + context.stop(); + } + +} diff --git a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java index 0a4e6389fb8..5de65ccde4b 100644 --- a/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java +++ b/components/camel-swagger-java/src/main/java/org/apache/camel/swagger/RestSwaggerReader.java @@ -74,6 +74,7 @@ import org.apache.camel.model.rest.RestSecurityDefinition; import org.apache.camel.model.rest.SecurityDefinition; import org.apache.camel.model.rest.VerbDefinition; import org.apache.camel.spi.ClassResolver; +import org.apache.camel.support.CamelContextHelper; import org.apache.camel.support.ObjectHelper; import org.apache.camel.util.FileUtil; @@ -94,7 +95,7 @@ public class RestSwaggerReader { * @param config the swagger configuration * @param classResolver class resolver to use * @return the swagger model - * @throws ClassNotFoundException + * @throws ClassNotFoundException is thrown if error loading class */ public Swagger read( CamelContext camelContext, @@ -103,7 +104,10 @@ public class RestSwaggerReader { Swagger swagger = new Swagger(); for (RestDefinition rest : rests) { - parse(camelContext, swagger, rest, camelContextId, classResolver); + Boolean disabled = CamelContextHelper.parseBoolean(camelContext, rest.getDisabled()); + if (disabled == null || !disabled) { + parse(camelContext, swagger, rest, camelContextId, classResolver); + } } // configure before returning @@ -114,7 +118,16 @@ public class RestSwaggerReader { private void parse( CamelContext camelContext, Swagger swagger, RestDefinition rest, String camelContextId, ClassResolver classResolver) throws ClassNotFoundException { - List<VerbDefinition> verbs = new ArrayList<>(rest.getVerbs()); + + // only include enabled verbs + List<VerbDefinition> filter = new ArrayList<>(); + for (VerbDefinition verb : rest.getVerbs()) { + Boolean disabled = CamelContextHelper.parseBoolean(camelContext, verb.getDisabled()); + if (disabled == null || !disabled) { + filter.add(verb); + } + } + List<VerbDefinition> verbs = new ArrayList<>(filter); // must sort the verbs by uri so we group them together when an uri has multiple operations verbs.sort(new VerbOrdering()); diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderDisabledTest.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderDisabledTest.java new file mode 100644 index 00000000000..5332f2811e6 --- /dev/null +++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderDisabledTest.java @@ -0,0 +1,106 @@ +/* + * 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.swagger; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import io.swagger.jaxrs.config.BeanConfig; +import io.swagger.models.Swagger; +import org.apache.camel.BindToRegistry; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.engine.DefaultClassResolver; +import org.apache.camel.model.rest.RestParamType; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class RestSwaggerReaderDisabledTest extends CamelTestSupport { + + private static final Logger LOG = LoggerFactory.getLogger(RestSwaggerReaderDisabledTest.class); + + @BindToRegistry("dummy-rest") + private DummyRestConsumerFactory factory = new DummyRestConsumerFactory(); + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + rest("/hello").consumes("application/json").produces("application/json").get("/hi/{name}") + .description("Saying hi").deprecated().param().name("name").type(RestParamType.path) + .dataType("string").description("Who is it").example("Donald Duck").endParam().to("log:hi") + .get("/bye/{name}").disabled().description("Saying bye").deprecated().param().name("name") + .type(RestParamType.path).dataType("string").description("Who is it").example("Donald Duck").endParam() + .responseMessage().code(200).message("A reply number") + .responseModel(float.class).example("success", "123").example("error", "-1").endResponseMessage() + .to("log:bye").post("/bye").disabled("true") + .description("To update the greeting message").consumes("application/xml").produces("application/xml") + .param().name("greeting").type(RestParamType.body) + .dataType("string").description("Message to use as greeting") + .example("application/xml", "<hello>Hi</hello>").endParam().to("log:bye"); + } + }; + } + + @Test + public void testReaderRead() throws Exception { + BeanConfig config = new BeanConfig(); + config.setHost("localhost:8080"); + config.setSchemes(new String[] { "http" }); + config.setBasePath("/api"); + RestSwaggerReader reader = new RestSwaggerReader(); + + Swagger swagger + = reader.read(context, context.getRestDefinitions(), config, context.getName(), new DefaultClassResolver()); + assertNotNull(swagger); + + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.INDENT_OUTPUT); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + String json = mapper.writeValueAsString(swagger); + + LOG.info(json); + + assertTrue(json.contains("\"host\" : \"localhost:8080\"")); + assertTrue(json.contains("\"basePath\" : \"/api\"")); + assertFalse(json.contains("\"/hello/bye\"")); + assertFalse(json.contains("\"summary\" : \"To update the greeting message\"")); + assertFalse(json.contains("\"/hello/bye/{name}\"")); + assertTrue(json.contains("\"/hello/hi/{name}\"")); + assertFalse(json.contains("\"type\" : \"number\"")); + assertFalse(json.contains("\"format\" : \"float\"")); + assertFalse(json.contains("\"application/xml\" : \"<hello>Hi</hello>\"")); + assertTrue(json.contains("\"x-example\" : \"Donald Duck\"")); + assertFalse(json.contains("\"success\" : \"123\"")); + assertFalse(json.contains("\"error\" : \"-1\"")); + assertTrue(json.contains("\"type\" : \"string\"")); + assertTrue(json.contains("\"deprecated\" : true")); + + JsonNode jsonNode = new ObjectMapper().readValue(json, JsonNode.class); + assertNotNull(jsonNode.get("paths").get("/hello/hi/{name}").get("get").get("deprecated")); + assertTrue(jsonNode.get("paths").get("/hello/hi/{name}").get("get").get("deprecated").asBoolean()); + + context.stop(); + } +}