Skip to content

Commit bce1cc0

Browse files
Jalander Ramagiriafrittoli
Jalander Ramagiri
authored andcommitted
Add module preprocessor and fix linter issues
Signed-off-by: Jalander Ramagiri <[email protected]>
1 parent 95538f6 commit bce1cc0

File tree

11 files changed

+280
-249
lines changed

11 files changed

+280
-249
lines changed

.github/linters/sun_checks.xml

+3-2
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,10 @@
120120
<module name="IllegalImport"/>
121121
<!-- defaults to sun.* packages -->
122122
<module name="RedundantImport"/>
123-
<module name="UnusedImports">
123+
<!-- Commenting for now, not suitable for generated events -->
124+
<!--<module name="UnusedImports">
124125
<property name="processJavadoc" value="false"/>
125-
</module>
126+
</module>-->
126127

127128
<!-- Checks for Size Violations. -->
128129
<!-- See https://checkstyle.org/config_sizes.html -->

.github/workflows/linter.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- name: Lint Code Base
1616
uses: github/super-linter/slim@v4
1717
env:
18-
FILTER_REGEX_INCLUDE: .*sdk/src/main/.*|.*generator/src/main/.*
18+
FILTER_REGEX_INCLUDE: .*sdk/src/main/.*|.*generator/src/main/.*|.*preprocessor/src/main/.*
1919
VALIDATE_ALL_CODEBASE: false
2020
DEFAULT_BRANCH: main
2121
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

DEVELOPMENT.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ To run all targets, before creating a commit:
8080
```
8181

8282
## Upgrade SDK to the Latest CDEvents Spec
83-
The [Java SDK Generator](./generator) is utilized to create events from the [CDEvents Spec schemas](./spec/schemas),
83+
The [Java SDK Generator](./generator) is utilized to create events from the CDEvents Spec schemas,
8484
and the [CDEvents Spec repository](https://github.com/cdevents/spec/) is added as a Git Submodule to this repository which needs to be updated with latest Spec release.
8585
- Steps involved to generate latest SDK
8686
1. Update spec submodule to the latest release
@@ -93,8 +93,8 @@ and the [CDEvents Spec repository](https://github.com/cdevents/spec/) is added a
9393
cd ../
9494
````
9595
- Verify the latest Spec is applied and Push the modified spec to your private working branch
96-
2. Generate model classes from [CDEvents Spec schemas](./spec/schemas) using maven plugin `jsonschema2pojo`
97-
- Compare the existing `jsonschema2pojo` plugin configuration in the [generator's pom.xml file](./generator/pom.xml) with the available [CDEvents Spec schemas](./spec/schemas)
96+
2. Generate model classes from CDEvents Spec schemas using maven plugin `jsonschema2pojo`
97+
- Compare the existing `jsonschema2pojo` plugin configuration in the [generator's pom.xml file](./generator/pom.xml) with the available CDEvents Spec schemas
9898
- Add/update executions for `jsonschema2pojo` plugin configuration in the [generator's pom.xml file](./generator/pom.xml) as below,
9999
````pom
100100
<execution>
@@ -111,7 +111,7 @@ and the [CDEvents Spec repository](https://github.com/cdevents/spec/) is added a
111111
</configuration>
112112
</execution>
113113
````
114-
- Where, `id`, `sourcePath` needs to be updated with the actual file name and `targetPackage` directory structure needs to be created in [sdk models](./sdk/src/main/java/dev/cdevents/models) for each new schema file if you find in [CDEvents Spec schemas](./spec/schemas)
114+
- Where, `id`, `sourcePath` needs to be updated with the actual file name and `targetPackage` directory structure needs to be created in [sdk models](./sdk/src/main/java/dev/cdevents/models) for each new schema file if you find in CDEvents Spec schemas
115115

116116
2. Generate CDEvent classes using [mustache template](./generator/src/main/resources/template/event-template.mustache)
117117
- [event-template.mustache](./generator/src/main/resources/template/event-template.mustache) needs an update If there is any change in the `context` or `subject` of a CDEvent

generator/pom.xml

-20
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,6 @@
6565

6666
<build>
6767
<plugins>
68-
<plugin>
69-
<groupId>org.codehaus.mojo</groupId>
70-
<artifactId>exec-maven-plugin</artifactId>
71-
<version>3.1.0</version>
72-
<executions>
73-
<execution>
74-
<id>run-PreprocessSchemas-main-class</id>
75-
<phase>generate-sources</phase>
76-
<goals>
77-
<goal>java</goal>
78-
</goals>
79-
<configuration>
80-
<mainClass>dev.cdevents.generator.PreprocessSchemas</mainClass>
81-
<arguments>
82-
<argument>${parent.project.dir}</argument>
83-
</arguments>
84-
</configuration>
85-
</execution>
86-
</executions>
87-
</plugin>
8868
<plugin>
8969
<groupId>org.jsonschema2pojo</groupId>
9070
<artifactId>jsonschema2pojo-maven-plugin</artifactId>

generator/src/main/java/dev/cdevents/generator/CDEventsGenerator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private static void updateSubjectContentProperties(SchemaData schemaData, JsonNo
162162
} else if (contentNode.get("type").asText().equals("array")) {
163163
JsonNode itemsNode = contentNode.get("items");
164164
dataType = itemsNode.get("type").asText();
165-
if(dataType.equals("string")){
165+
if (dataType.equals("string")) {
166166
dataType = "List<String>";
167167
}
168168
} else {

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
</scm>
8787

8888
<modules>
89+
<module>preprocessor</module>
8990
<module>generator</module>
9091
<module>sdk</module>
9192
</modules>

preprocessor/pom.xml

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>dev.cdevents</groupId>
8+
<artifactId>cdevents-sdk-java-parent</artifactId>
9+
<version>0.3.2-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>cdevents-sdk-java-preprocessor</artifactId>
13+
14+
<name>cdevents-sdk-java-preprocessor</name>
15+
<description>Preprocessor for SDK Source code generator</description>
16+
<url>https://github.com/cdevents</url>
17+
18+
<properties>
19+
<maven.javadoc.skip>true</maven.javadoc.skip>
20+
<maven.source.skip>true</maven.source.skip>
21+
<maven.deploy.skip>true</maven.deploy.skip>
22+
<maven.compiler.source>11</maven.compiler.source>
23+
<maven.compiler.target>11</maven.compiler.target>
24+
<sdk.project.dir>${project.basedir}/../sdk</sdk.project.dir>
25+
<parent.project.dir>${project.basedir}/../</parent.project.dir>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>com.fasterxml.jackson.core</groupId>
31+
<artifactId>jackson-databind</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.slf4j</groupId>
35+
<artifactId>slf4j-api</artifactId>
36+
<version>${slf4j.version}</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.slf4j</groupId>
40+
<artifactId>slf4j-simple</artifactId>
41+
<version>${slf4j.version}</version>
42+
<scope>runtime</scope>
43+
</dependency>
44+
</dependencies>
45+
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.codehaus.mojo</groupId>
50+
<artifactId>exec-maven-plugin</artifactId>
51+
<version>3.1.0</version>
52+
<executions>
53+
<execution>
54+
<id>run-PreprocessSchemas-main-class</id>
55+
<phase>process-classes</phase>
56+
<goals>
57+
<goal>java</goal>
58+
</goals>
59+
<configuration>
60+
<mainClass>dev.cdevents.preprocessor.PreprocessSchemas</mainClass>
61+
<arguments>
62+
<argument>${parent.project.dir}</argument>
63+
</arguments>
64+
</configuration>
65+
</execution>
66+
</executions>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
</project>
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,79 @@
1-
package dev.cdevents.generator;
2-
3-
import com.fasterxml.jackson.databind.JsonNode;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import com.fasterxml.jackson.databind.node.ObjectNode;
6-
import org.slf4j.Logger;
7-
import org.slf4j.LoggerFactory;
8-
9-
import java.io.File;
10-
import java.io.IOException;
11-
import java.nio.file.Files;
12-
import java.nio.file.Path;
13-
import java.nio.file.Paths;
14-
import java.util.Iterator;
15-
16-
public class PreprocessSchemas {
17-
18-
private static final ObjectMapper objectMapper = new ObjectMapper();
19-
20-
private static final Logger log = LoggerFactory.getLogger(PreprocessSchemas.class);
21-
22-
public static void main(String[] args) throws IOException {
23-
if (args == null || args.length != 1) {
24-
System.err.println("Usage: PreprocessSchemas <schema_directory_path>");
25-
throw new IllegalArgumentException("Schema directory path arguments not passed to PreprocessSchemas");
26-
}
27-
String parentBaseDir = args[0];
28-
String specSchemaDir = parentBaseDir + File.separator + "spec" + File.separator + "schemas";
29-
Files.walk(Paths.get(specSchemaDir))
30-
.filter(Files::isRegularFile)
31-
.filter(path -> path.toString().endsWith(".json"))
32-
.forEach(PreprocessSchemas::processFile);
33-
}
34-
35-
private static void processFile(Path filePath) {
36-
log.info("processing schema file {} to update $ref path with json extension.", filePath.getFileName());
37-
try {
38-
JsonNode rootNode = objectMapper.readTree(filePath.toFile());
39-
updateRefs(rootNode);
40-
objectMapper.writerWithDefaultPrettyPrinter().writeValue(filePath.toFile(), rootNode);
41-
} catch (IOException e) {
42-
log.error("Exception occurred while process schema file to update ref {}", e.getMessage());
43-
throw new IllegalStateException("Exception occurred while process schema file to update ref ", e);
44-
}
45-
}
46-
47-
private static void updateRefs(JsonNode node) {
48-
if (node.isObject()) {
49-
ObjectNode objectNode = (ObjectNode) node;
50-
Iterator<String> fieldNames = objectNode.fieldNames();
51-
while (fieldNames.hasNext()) {
52-
String fieldName = fieldNames.next();
53-
JsonNode childNode = objectNode.get(fieldName);
54-
if ("$ref".equals(fieldName) && !childNode.asText().endsWith(".json")) {
55-
objectNode.put(fieldName, childNode.asText() + ".json");
56-
} else {
57-
updateRefs(childNode);
58-
}
59-
}
60-
} else if (node.isArray()) {
61-
for (JsonNode arrayItem : node) {
62-
updateRefs(arrayItem);
63-
}
64-
}
65-
}
66-
}
1+
package dev.cdevents.preprocessor;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.fasterxml.jackson.databind.node.ObjectNode;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.nio.file.Paths;
14+
import java.util.Iterator;
15+
16+
public class PreprocessSchemas {
17+
18+
private PreprocessSchemas() {
19+
}
20+
21+
private static ObjectMapper objectMapper = new ObjectMapper();
22+
23+
private static Logger log = LoggerFactory.getLogger(PreprocessSchemas.class);
24+
25+
/**
26+
*
27+
* Main method to update schema files.
28+
* @param args [0] - parent directory for the cdevents-sdk-java
29+
*/
30+
public static void main(String[] args) {
31+
if (args == null || args.length != 1) {
32+
System.err.println("Usage: PreprocessSchemas <schema_directory_path>");
33+
throw new IllegalArgumentException("Prent directory path argument not passed to PreprocessSchemas");
34+
}
35+
String parentBaseDir = args[0];
36+
String specSchemaDir = parentBaseDir + File.separator + "spec" + File.separator + "schemas";
37+
try {
38+
Files.walk(Paths.get(specSchemaDir))
39+
.filter(Files::isRegularFile)
40+
.filter(path -> path.toString().endsWith(".json"))
41+
.forEach(PreprocessSchemas::processFile);
42+
} catch (IOException e) {
43+
log.error("Exception occurred while processing schema directory {}", e.getMessage());
44+
throw new IllegalStateException("Exception occurred while processing schema directory", e);
45+
}
46+
}
47+
48+
private static void processFile(Path filePath) {
49+
log.info("processing schema file {} to update $ref path with json extension.", filePath.getFileName());
50+
try {
51+
JsonNode rootNode = objectMapper.readTree(filePath.toFile());
52+
updateRefs(rootNode);
53+
objectMapper.writerWithDefaultPrettyPrinter().writeValue(filePath.toFile(), rootNode);
54+
} catch (IOException e) {
55+
log.error("Exception occurred while process schema file to update ref {}", e.getMessage());
56+
throw new IllegalStateException("Exception occurred while process schema file to update ref ", e);
57+
}
58+
}
59+
60+
private static void updateRefs(JsonNode node) {
61+
if (node.isObject()) {
62+
ObjectNode objectNode = (ObjectNode) node;
63+
Iterator<String> fieldNames = objectNode.fieldNames();
64+
while (fieldNames.hasNext()) {
65+
String fieldName = fieldNames.next();
66+
JsonNode childNode = objectNode.get(fieldName);
67+
if ("$ref".equals(fieldName) && !childNode.asText().endsWith(".json")) {
68+
objectNode.put(fieldName, childNode.asText() + ".json");
69+
} else {
70+
updateRefs(childNode);
71+
}
72+
}
73+
} else if (node.isArray()) {
74+
for (JsonNode arrayItem : node) {
75+
updateRefs(arrayItem);
76+
}
77+
}
78+
}
79+
}

sdk/src/main/java/dev/cdevents/CDEvents.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public static boolean validateCDEventJson(String cdEventJson) {
131131
private static Set<ValidationMessage> getJsonSchemaValidationMessages(CDEvent cdEvent) {
132132
Map<String, String> schemaMap = new HashMap<>();
133133
schemaMap.put(cdEvent.schemaURL(), SCHEMA_CLASSPATH + cdEvent.schemaFileName());
134-
schemaMap.put(cdEvent.baseURI() + "links/embeddedlinksarray", SCHEMA_CLASSPATH+ "links/embeddedlinksarray.json");
134+
schemaMap.put(cdEvent.baseURI() + "links/embeddedlinksarray", SCHEMA_CLASSPATH + "links/embeddedlinksarray.json");
135135
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012, builder ->
136136
// This creates a mapping from $id which starts with https://cdevents.dev/0.4.0/schema to the retrieval URI classpath:schema/
137137
builder.schemaMappers(schemaMappers -> schemaMappers.mappings(schemaMap))

0 commit comments

Comments
 (0)