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

fix: Create sub schemas for each top properties #144

Merged
merged 1 commit into from
Sep 22, 2023
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
44 changes: 44 additions & 0 deletions packages/camel-catalog/assembly/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@
<groupId>org.apache.camel</groupId>
<artifactId>camel-yaml-dsl-deserializers</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -117,6 +137,30 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>testCompile</id>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.kaoto.camelcatalog;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import java.nio.file.Paths;

public class CamelCatalogTestSupport {
protected static final ObjectMapper jsonMapper = new ObjectMapper();
protected static final ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());
protected static final JsonFactory jsonFactory = new JsonFactory();

private static ObjectNode index = null;

protected ObjectNode getIndex() throws Exception {
if (CamelCatalogTestSupport.index == null) {
var path = Paths.get("..").resolve("dist").resolve("index.json");
CamelCatalogTestSupport.index = (ObjectNode) jsonMapper.readTree(path.toFile());
}
return CamelCatalogTestSupport.index;
}

protected ObjectNode getSchema(String name) throws Exception {
var index = getIndex();
for (JsonNode schema : index.withArray("schemas")) {
var fileName = schema.get("file").asText();
var tokens = fileName.split("-");
if ("camelYamlDsl".equals(tokens[0]) && name.isEmpty() && tokens[1].matches("\\d+.*")) {
return (ObjectNode) jsonMapper.readTree(Paths.get("..").resolve("dist").resolve(fileName).toFile());
} else if ("camelYamlDsl".equals(tokens[0]) && tokens[1].equals(name)) {
return (ObjectNode) jsonMapper.readTree(Paths.get("..").resolve("dist").resolve(fileName).toFile());
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.kaoto.camelcatalog;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class CamelYamlDslSchemaTest extends CamelCatalogTestSupport {

@Test
public void testRootSchema() throws Exception {
var rootSchema = getSchema("");
assertEquals(rootSchema.get("type").asText(), "array");
var definitions = rootSchema.withObject("/items").withObject("/definitions");
assertTrue(definitions.has("org.apache.camel.model.ProcessorDefinition"));
}

@Test
public void testBeans() throws Exception {
var beansSchema = getSchema("beans");
assertEquals(beansSchema.get("type").asText(), "array");
var definitions = beansSchema.withObject("/definitions");
assertEquals(1, definitions.size());
assertTrue(definitions.has("org.apache.camel.model.app.RegistryBeanDefinition"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.kaoto.camelcatalog;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class IndexTest extends CamelCatalogTestSupport {

@Test
public void test() throws Exception {
var index = getIndex();
assertTrue(index.has("catalogs"));
var catalogs = index.withObject("/catalogs");
assertTrue(catalogs.has("models"));
assertTrue(catalogs.has("components"));
assertTrue(catalogs.has("languages"));
assertTrue(catalogs.has("kamelets"));
assertTrue(catalogs.has("dataformats"));
assertTrue(index.has("schemas"));
var schemas = index.withArray("/schemas");
List<String> props = new ArrayList<>();
List<String> crds = new ArrayList<>();
schemas.forEach(schema -> {
var tokens = schema.get("file").asText().split("-");
if ("camelYamlDsl".equals(tokens[0]) && !tokens[1].matches("^\\d+.*")) {
props.add(tokens[1]);
} else if ("crd".equals(tokens[0])) {
crds.add(tokens[2]);
}
});
assertTrue(props.contains("beans"));
assertTrue(props.contains("errorHandler"));
assertTrue(props.contains("from"));
assertTrue(props.contains("intercept"));
assertTrue(props.contains("interceptFrom"));
assertTrue(props.contains("interceptSendToEndpoint"));
assertTrue(props.contains("onCompletion"));
assertTrue(props.contains("onException"));
assertTrue(props.contains("rest"));
assertTrue(props.contains("restConfiguration"));
assertTrue(props.contains("route"));
assertTrue(props.contains("routeConfiguration"));
assertTrue(props.contains("routeTemplate"));
assertTrue(props.contains("templatedRoute"));
assertTrue(crds.contains("integrations"));
assertTrue(crds.contains("kamelets"));
assertTrue(crds.contains("kameletbindings"));
assertTrue(crds.contains("pipes"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.nio.file.StandardCopyOption;
import java.util.List;

import com.fasterxml.jackson.databind.JsonNode;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand All @@ -31,6 +32,7 @@
import org.apache.maven.plugins.annotations.ResolutionScope;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
Expand Down Expand Up @@ -68,6 +70,7 @@ public class KaotoCamelCatalogMojo extends AbstractMojo {

private static final ObjectMapper jsonMapper = new ObjectMapper();
private static final ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());
private static final JsonFactory jsonFactory = new JsonFactory();

@Parameter(required = true)
private File inputDirectory;
Expand Down Expand Up @@ -128,6 +131,79 @@ private void processSchema(Path inputDir, Index index) {
}
var indexEntry = new Entry("Camel YAML DSL JSON schema", camelVersion, outputFileName);
index.getSchemas().add(indexEntry);

try {
var rootSchema = (ObjectNode) jsonMapper.readTree(schema.toFile());
var items = rootSchema.withObject("/items");
var properties = items.withObject("/properties");
var definitions = items.withObject("/definitions");
var relocatedDefinitions = relocateToRootDefinitions(definitions);
properties.properties().forEach(p -> processSubSchema(p, relocatedDefinitions, rootSchema, index));
} catch (Exception e) {
getLog().error(e);
}
}

private ObjectNode relocateToRootDefinitions(ObjectNode definitions) {
var relocatedDefinitions = definitions.deepCopy();
relocatedDefinitions.findParents("$ref").stream()
.map(ObjectNode.class::cast)
.forEach(n -> n.put("$ref", getRelocatedRef(n)));
return relocatedDefinitions;
}

private String getRelocatedRef(ObjectNode parent) {
return parent.get("$ref").asText().replace("#/items/definitions/", "#/definitions/");
}

private void processSubSchema(
java.util.Map.Entry<String, JsonNode> prop,
ObjectNode definitions,
ObjectNode rootSchema,
Index index
) {
var propName = prop.getKey();
var answer = (ObjectNode) prop.getValue().deepCopy();
if (answer.has("$ref") && definitions.has(getNameFromRef((ObjectNode)answer))) {
answer = definitions.withObject("/" + getNameFromRef((ObjectNode)answer)).deepCopy();

}
answer.set("$schema", rootSchema.get("$schema"));
populateDefinitions(answer, definitions);
var outputFileName = String.format("%s-%s-%s.json", CAMEL_YAML_DSL, propName, camelVersion);
var output = outputDirectory.toPath().resolve(outputFileName);
try {
output.getParent().toFile().mkdirs();
var writer = new FileWriter(output.toFile());
JsonGenerator gen = jsonFactory.createGenerator(writer).useDefaultPrettyPrinter();
jsonMapper.writeTree(gen, answer);
var indexEntry = new Entry("Camel YAML DSL JSON schema: " + propName, camelVersion, outputFileName);
index.getSchemas().add(indexEntry);
} catch (Exception e) {
getLog().error(e);
}
}

private String getNameFromRef(ObjectNode parent) {
var ref = parent.get("$ref").asText();
return ref.contains("items") ? ref.replace("#/items/definitions/", "")
: ref.replace("#/definitions/", "");
}

private void populateDefinitions(ObjectNode schema, ObjectNode definitions) {
var schemaDefinitions = schema.withObject("/definitions");
boolean added = true;
while(added) {
added = false;
for (JsonNode refParent : schema.findParents("$ref")) {
var name = getNameFromRef((ObjectNode) refParent);
if (!schemaDefinitions.has(name)) {
schemaDefinitions.set(name, definitions.withObject("/" + name));
added = true;
break;
}
}
}
}

private void processCatalog(Path inputDir, Index index) {
Expand Down
13 changes: 13 additions & 0 deletions packages/camel-catalog/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<version.camel-kamelets>4.0.0</version.camel-kamelets>
<version.jackson>2.15.2</version.jackson>
<version.java>17</version.java>
<version.junit>5.10.0</version.junit>
<version.kubernetes-model>6.8.1</version.kubernetes-model>
<version.maven-antrun-plugin>3.1.0</version.maven-antrun-plugin>
<version.maven-compiler-plugin>3.11.0</version.maven-compiler-plugin>
Expand Down Expand Up @@ -93,6 +94,18 @@
<artifactId>kubernetes-model</artifactId>
<version>${version.kubernetes-model}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${version.junit}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${version.junit}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
8 changes: 6 additions & 2 deletions packages/camel-catalog/src/json-schema-to-typescript.mts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ async function main() {
let filename = schema.name;

if (schema.file.includes('camelYamlDsl')) {
addTitleToDefinitions(schemaContent);
filename = 'camelYamlDsl';
if (schema.file.match(/camelYamlDsl-\d+.*\.json/)) {
addTitleToDefinitions(schemaContent);
filename = 'camelYamlDsl';
} else {
return;
}
}

/** Remove the -4.0.0.json section of the filename */
Expand Down
Loading