This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-3.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.x by this push:
new cda5c712c62 CAMEL-18942. Copy the description of the path/operation to
the generated route (#9147)
cda5c712c62 is described below
commit cda5c712c62632609093a96f22a8731a58aad915
Author: Marat Gubaidullin <[email protected]>
AuthorDate: Wed Jan 18 00:47:30 2023 -0500
CAMEL-18942. Copy the description of the path/operation to the generated
route (#9147)
---
.../generator/openapi/RestDslYamlGenerator.java | 19 ++-
...GeneratorV3SimpleWithRoutesDescriptionTest.java | 61 +++++++++
...piV3PetstoreSimpleWithRoutesDescriptionYaml.txt | 15 +++
.../openapi/openapi-spec-description.json | 143 +++++++++++++++++++++
4 files changed, 232 insertions(+), 6 deletions(-)
diff --git
a/tooling/openapi-rest-dsl-generator/src/main/java/org/apache/camel/generator/openapi/RestDslYamlGenerator.java
b/tooling/openapi-rest-dsl-generator/src/main/java/org/apache/camel/generator/openapi/RestDslYamlGenerator.java
index b191e478781..6236ec2901d 100644
---
a/tooling/openapi-rest-dsl-generator/src/main/java/org/apache/camel/generator/openapi/RestDslYamlGenerator.java
+++
b/tooling/openapi-rest-dsl-generator/src/main/java/org/apache/camel/generator/openapi/RestDslYamlGenerator.java
@@ -19,8 +19,10 @@ package org.apache.camel.generator.openapi;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
@@ -139,13 +141,13 @@ public class RestDslYamlGenerator extends
RestDslGenerator<RestDslYamlGenerator>
XmlMapper xmlMapper = new XmlMapper();
JsonNode node = xmlMapper.readTree(newXml.getBytes());
- List<String> toTagUris = new ArrayList<>();
+ Map<String, String> toTagData = new HashMap<>();
for (String v : VERBS) {
fixVerbNodes(xmlMapper, node, v);
fixParamNodes(xmlMapper, node, v);
sortVerb(node, v);
- toTagUris.addAll(fixToTags(xmlMapper, node, v));
+ toTagData.putAll(fixToTags(xmlMapper, node, v));
}
// the root tag should be an array
@@ -153,9 +155,13 @@ public class RestDslYamlGenerator extends
RestDslGenerator<RestDslYamlGenerator>
// add Routes
if (generateRoutes) {
- for (String uri : toTagUris) {
+ for (String uri : toTagData.keySet()) {
ObjectNode from = JsonNodeFactory.instance.objectNode();
from.set("uri", new TextNode(uri));
+ String description = toTagData.get(uri);
+ if (description != null && !description.isBlank()) {
+ from.set("description", new TextNode(description));
+ }
ObjectNode route = JsonNodeFactory.instance.objectNode();
route.set("from", from);
((ArrayNode)
node).add(xmlMapper.createObjectNode().set("route", route));
@@ -302,8 +308,8 @@ public class RestDslYamlGenerator extends
RestDslGenerator<RestDslYamlGenerator>
/**
* to tag should be in implicit mode, ex: to: "direct:directX"
*/
- private static List<String> fixToTags(XmlMapper xmlMapper, JsonNode node,
String verb) {
- List<String> toTags = new ArrayList<>();
+ private static Map<String, String> fixToTags(XmlMapper xmlMapper, JsonNode
node, String verb) {
+ Map<String, String> toTags = new HashMap<>();
JsonNode verbs = node.path("rest").path(verb);
if (verbs == null || verbs.isMissingNode()) {
return toTags;
@@ -319,7 +325,8 @@ public class RestDslYamlGenerator extends
RestDslGenerator<RestDslYamlGenerator>
ObjectNode on = (ObjectNode) n;
JsonNode uri = n.get("to").get("uri");
on.set("to", uri);
- toTags.add(uri.textValue());
+ String description = n.has("description") ?
n.get("description").asText() : "";
+ toTags.put(uri.textValue(), description);
}
}
return toTags;
diff --git
a/tooling/openapi-rest-dsl-generator/src/test/java/org/apache/camel/generator/openapi/RestDslYamlGeneratorV3SimpleWithRoutesDescriptionTest.java
b/tooling/openapi-rest-dsl-generator/src/test/java/org/apache/camel/generator/openapi/RestDslYamlGeneratorV3SimpleWithRoutesDescriptionTest.java
new file mode 100644
index 00000000000..bf605476cdb
--- /dev/null
+++
b/tooling/openapi-rest-dsl-generator/src/test/java/org/apache/camel/generator/openapi/RestDslYamlGeneratorV3SimpleWithRoutesDescriptionTest.java
@@ -0,0 +1,61 @@
+/*
+ * 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.generator.openapi;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import io.apicurio.datamodels.Library;
+import io.apicurio.datamodels.openapi.models.OasDocument;
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import java.io.InputStream;
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class RestDslYamlGeneratorV3SimpleWithRoutesDescriptionTest {
+
+ static OasDocument document;
+
+ @BeforeAll
+ public static void readOpenApiDoc() throws Exception {
+ final ObjectMapper mapper = new ObjectMapper();
+ try (InputStream is
+ =
RestDslYamlGeneratorV3SimpleWithRoutesDescriptionTest.class.getResourceAsStream("openapi-spec-description.json"))
{
+ final JsonNode node = mapper.readTree(is);
+ document = (OasDocument) Library.readDocument(node);
+ }
+ }
+
+ @Test
+ public void shouldGenerateYamlWithDefaults() throws Exception {
+ final CamelContext context = new DefaultCamelContext();
+
+ final String yaml =
RestDslGenerator.toYaml(document).generate(context, true);
+ final URI file =
RestDslGeneratorTest.class.getResource("/OpenApiV3PetstoreSimpleWithRoutesDescriptionYaml.txt").toURI();
+ final String expectedContent = new
String(Files.readAllBytes(Paths.get(file)), StandardCharsets.UTF_8);
+
+ assertThat(yaml).isEqualTo(expectedContent);
+ }
+
+}
diff --git
a/tooling/openapi-rest-dsl-generator/src/test/resources/OpenApiV3PetstoreSimpleWithRoutesDescriptionYaml.txt
b/tooling/openapi-rest-dsl-generator/src/test/resources/OpenApiV3PetstoreSimpleWithRoutesDescriptionYaml.txt
new file mode 100644
index 00000000000..2f5e1e3387e
--- /dev/null
+++
b/tooling/openapi-rest-dsl-generator/src/test/resources/OpenApiV3PetstoreSimpleWithRoutesDescriptionYaml.txt
@@ -0,0 +1,15 @@
+- rest:
+ put:
+ - path: "/pet"
+ description: "Update Pet Data"
+ consumes: "application/json,text/xml"
+ produces: "application/json,text/xml"
+ param:
+ - name: "body"
+ required: true
+ type: "body"
+ to: "direct:rest1"
+- route:
+ from:
+ uri: "direct:rest1"
+ description: "Update Pet Data"
diff --git
a/tooling/openapi-rest-dsl-generator/src/test/resources/org/apache/camel/generator/openapi/openapi-spec-description.json
b/tooling/openapi-rest-dsl-generator/src/test/resources/org/apache/camel/generator/openapi/openapi-spec-description.json
new file mode 100644
index 00000000000..81653158393
--- /dev/null
+++
b/tooling/openapi-rest-dsl-generator/src/test/resources/org/apache/camel/generator/openapi/openapi-spec-description.json
@@ -0,0 +1,143 @@
+{
+ "openapi": "3.0.2",
+ "info": {
+ "title": "OpenApi Simple PetStore",
+ "version": "1.0.0",
+ "description": "Minimal example for testing."
+ },
+ "paths": {
+ "/pet": {
+ "put": {
+ "description": "Update Pet Data",
+ "requestBody": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/Pet"
+ }
+ },
+ "text/xml": {
+ "schema": {
+ "$ref": "#/components/schemas/Pet"
+ }
+ }
+ },
+ "required": true
+ },
+ "responses": {
+ "200": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/Pet"
+ }
+ },
+ "text/xml": {
+ "schema": {
+ "$ref": "#/components/schemas/Pet"
+ }
+ }
+ },
+ "description": "Updated pet."
+ },
+ "201": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/Pet"
+ }
+ }
+ },
+ "description": "Created pet."
+ }
+ }
+ }
+ }
+ },
+ "components": {
+ "schemas": {
+ "Pet": {
+ "required": [
+ "name",
+ "photoUrls"
+ ],
+ "type": "object",
+ "properties": {
+ "id": {
+ "format": "int64",
+ "type": "integer"
+ },
+ "category": {
+ "$ref": "#/components/schemas/Category"
+ },
+ "name": {
+ "type": "string",
+ "example": "doggie"
+ },
+ "photoUrls": {
+ "type": "array",
+ "items": {
+ "type": "string"
+ },
+ "xml": {
+ "name": "photoUrl",
+ "wrapped": true
+ }
+ },
+ "tags": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/Tag"
+ },
+ "xml": {
+ "name": "tag",
+ "wrapped": true
+ }
+ },
+ "status": {
+ "description": "pet status in the store",
+ "enum": [
+ "available",
+ "pending",
+ "sold"
+ ],
+ "type": "string"
+ }
+ },
+ "xml": {
+ "name": "Pet"
+ }
+ },
+ "Category": {
+ "type": "object",
+ "properties": {
+ "id": {
+ "format": "int64",
+ "type": "integer"
+ },
+ "name": {
+ "type": "string"
+ }
+ },
+ "xml": {
+ "name": "Category"
+ }
+ },
+ "Tag": {
+ "type": "object",
+ "properties": {
+ "id": {
+ "format": "int64",
+ "type": "integer"
+ },
+ "name": {
+ "type": "string"
+ }
+ },
+ "xml": {
+ "name": "Tag"
+ }
+ }
+ }
+ }
+}
\ No newline at end of file