Skip to content
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

CAMEL-18942. Copy the description of the path/operation to the genera… #9147

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -19,8 +19,10 @@
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;
Expand Down Expand Up @@ -129,23 +131,27 @@ public String generate(final CamelContext context, boolean generateRoutes) throw
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
node = fixRootNode(xmlMapper, node);

// 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));
Expand Down Expand Up @@ -292,8 +298,8 @@ private static void fixVerbNodes(XmlMapper xmlMapper, JsonNode node, String verb
/**
* 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;
Expand All @@ -309,7 +315,8 @@ private static List<String> fixToTags(XmlMapper xmlMapper, JsonNode node, String
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;
Expand Down
@@ -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);
}

}
@@ -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"
@@ -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"
}
}
}
}
}