Skip to content

Commit 95538f6

Browse files
Jalander Ramagiriafrittoli
authored andcommitted
Generate SDK with spec 0.4.1
Signed-off-by: Jalander Ramagiri <[email protected]>
1 parent 0221e04 commit 95538f6

File tree

161 files changed

+12804
-537
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+12804
-537
lines changed

DEVELOPMENT.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,44 @@ To run all targets, before creating a commit:
7878
```shell
7979
./mvnw verify
8080
```
81+
82+
## 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),
84+
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.
85+
- Steps involved to generate latest SDK
86+
1. Update spec submodule to the latest release
87+
- Create a private working branch from [cdevents/sdk-java](https://github.com/cdevents/sdk-java)
88+
- Checkout the latest spec version and update the submodule from spec root directory
89+
````bash
90+
cd spec
91+
git checkout main && git pull
92+
git checkout <latest-release-tag>
93+
cd ../
94+
````
95+
- 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)
98+
- Add/update executions for `jsonschema2pojo` plugin configuration in the [generator's pom.xml file](./generator/pom.xml) as below,
99+
````pom
100+
<execution>
101+
<id>generate-<subject>-<predicate>-from-schema</id>
102+
<phase>generate-sources</phase>
103+
<goals>
104+
<goal>generate</goal>
105+
</goals>
106+
<configuration>
107+
<sourcePaths>
108+
<sourcePath>${parent.project.dir}/spec/schemas/<schema-file-name>.json</sourcePath>
109+
</sourcePaths>
110+
<targetPackage>dev.cdevents.models.<subject>.<predicate></targetPackage>
111+
</configuration>
112+
</execution>
113+
````
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)
115+
116+
2. Generate CDEvent classes using [mustache template](./generator/src/main/resources/template/event-template.mustache)
117+
- [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
118+
- All the [CDEvents](./sdk/src/main/java/dev/cdevents/events) generated by using `event-template.mustache`
119+
- Run `./mvnw verify` and fix if any issues found during the build process
120+
- Add/Update test cases for the new/updated events to validate that they are generated correctly in [sdk](./sdk)
121+

generator/pom.xml

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@
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>
6888
<plugin>
6989
<groupId>org.jsonschema2pojo</groupId>
7090
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
@@ -77,7 +97,68 @@
7797
<targetVersion>8</targetVersion>
7898
<includeGeneratedAnnotation>true</includeGeneratedAnnotation>
7999
</configuration>
80-
<executions>
100+
<executions>
101+
<execution>
102+
<id>generate-embedded-links-from-schema</id>
103+
<phase>generate-sources</phase>
104+
<goals>
105+
<goal>generate</goal>
106+
</goals>
107+
<configuration>
108+
<sourcePaths>
109+
<sourcePath>${parent.project.dir}/spec/schemas/links/embeddedlinkend.json</sourcePath>
110+
<sourcePath>${parent.project.dir}/spec/schemas/links/embeddedlinkpath.json</sourcePath>
111+
<sourcePath>${parent.project.dir}/spec/schemas/links/embeddedlinkrelation.json</sourcePath>
112+
<sourcePath>${parent.project.dir}/spec/schemas/links/embeddedlinksarray.json</sourcePath>
113+
</sourcePaths>
114+
<!--<sourceDirectory>${parent.project.dir}/spec/schemas/links</sourceDirectory>
115+
<includes>embedded.*\.json</includes>-->
116+
<targetPackage>dev.cdevents.models.links.embedded</targetPackage>
117+
</configuration>
118+
</execution>
119+
<execution>
120+
<id>generate-external-links-from-schema</id>
121+
<phase>generate-sources</phase>
122+
<goals>
123+
<goal>generate</goal>
124+
</goals>
125+
<configuration>
126+
<sourcePaths>
127+
<sourcePath>${parent.project.dir}/spec/schemas/links/linkend.json</sourcePath>
128+
<sourcePath>${parent.project.dir}/spec/schemas/links/linkpath.json</sourcePath>
129+
<sourcePath>${parent.project.dir}/spec/schemas/links/linkrelation.json</sourcePath>
130+
<sourcePath>${parent.project.dir}/spec/schemas/links/linkstart.json</sourcePath>
131+
</sourcePaths>
132+
<targetPackage>dev.cdevents.models.links</targetPackage>
133+
</configuration>
134+
</execution>
135+
<execution>
136+
<id>generate-artifact-deleted-from-schema</id>
137+
<phase>generate-sources</phase>
138+
<goals>
139+
<goal>generate</goal>
140+
</goals>
141+
<configuration>
142+
<sourcePaths>
143+
<sourcePath>${parent.project.dir}/spec/schemas/artifactdeleted.json</sourcePath>
144+
</sourcePaths>
145+
<targetPackage>dev.cdevents.models.artifact.deleted</targetPackage>
146+
</configuration>
147+
</execution>
148+
149+
<execution>
150+
<id>generate-artifact-downloaded-from-schema</id>
151+
<phase>generate-sources</phase>
152+
<goals>
153+
<goal>generate</goal>
154+
</goals>
155+
<configuration>
156+
<sourcePaths>
157+
<sourcePath>${parent.project.dir}/spec/schemas/artifactdownloaded.json</sourcePath>
158+
</sourcePaths>
159+
<targetPackage>dev.cdevents.models.artifact.downloaded</targetPackage>
160+
</configuration>
161+
</execution>
81162
<execution>
82163
<id>generate-artifact-packaged-from-schema</id>
83164
<phase>generate-sources</phase>
@@ -521,6 +602,19 @@
521602
<targetPackage>dev.cdevents.models.testcaserun.queued</targetPackage>
522603
</configuration>
523604
</execution>
605+
<execution>
606+
<id>generate-testcaserun-skipped-from-schema</id>
607+
<phase>generate-sources</phase>
608+
<goals>
609+
<goal>generate</goal>
610+
</goals>
611+
<configuration>
612+
<sourcePaths>
613+
<sourcePath>${parent.project.dir}/spec/schemas/testcaserunskipped.json</sourcePath>
614+
</sourcePaths>
615+
<targetPackage>dev.cdevents.models.testcaserun.skipped</targetPackage>
616+
</configuration>
617+
</execution>
524618
<execution>
525619
<id>generate-testcaserun-started-from-schema</id>
526620
<phase>generate-sources</phase>
@@ -586,6 +680,45 @@
586680
<targetPackage>dev.cdevents.models.testsuiterun.started</targetPackage>
587681
</configuration>
588682
</execution>
683+
<execution>
684+
<id>generate-ticket-closed-from-schema</id>
685+
<phase>generate-sources</phase>
686+
<goals>
687+
<goal>generate</goal>
688+
</goals>
689+
<configuration>
690+
<sourcePaths>
691+
<sourcePath>${parent.project.dir}/spec/schemas/ticketclosed.json</sourcePath>
692+
</sourcePaths>
693+
<targetPackage>dev.cdevents.models.ticket.closed</targetPackage>
694+
</configuration>
695+
</execution>
696+
<execution>
697+
<id>generate-ticket-created-from-schema</id>
698+
<phase>generate-sources</phase>
699+
<goals>
700+
<goal>generate</goal>
701+
</goals>
702+
<configuration>
703+
<sourcePaths>
704+
<sourcePath>${parent.project.dir}/spec/schemas/ticketcreated.json</sourcePath>
705+
</sourcePaths>
706+
<targetPackage>dev.cdevents.models.ticket.created</targetPackage>
707+
</configuration>
708+
</execution>
709+
<execution>
710+
<id>generate-ticket-updated-from-schema</id>
711+
<phase>generate-sources</phase>
712+
<goals>
713+
<goal>generate</goal>
714+
</goals>
715+
<configuration>
716+
<sourcePaths>
717+
<sourcePath>${parent.project.dir}/spec/schemas/ticketupdated.json</sourcePath>
718+
</sourcePaths>
719+
<targetPackage>dev.cdevents.models.ticket.updated</targetPackage>
720+
</configuration>
721+
</execution>
589722
</executions>
590723
</plugin>
591724
<plugin>

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ private static SchemaData buildCDEventDataFromJsonSchema(File file) {
113113
String upperCaseSubject = getUpperCaseSubjectType(subjectType);
114114
//set the Schema JsonNode required values to schemaData
115115
schemaData.setSchemaURL(schemaURL);
116+
schemaData.setBaseURI(schemaURL.substring(0, schemaURL.lastIndexOf("/") + 1));
116117
schemaData.setSubject(subject);
117118
schemaData.setPredicate(predicate);
118119
schemaData.setCapitalizedSubject(capitalizedSubject);
@@ -153,15 +154,29 @@ private static void updateSubjectContentProperties(SchemaData schemaData, JsonNo
153154
String capitalizedContentField = StringUtils.capitalize(contentField);
154155
JsonNode contentNode = contentMap.getValue();
155156
String dataType = "";
156-
if (!contentNode.get("type").asText().equals("object")) {
157+
if (contentNode.get("type") != null && !contentNode.get("type").asText().equals("object")) {
157158
if (contentNode.get("format") != null && contentNode.get("format").asText().equalsIgnoreCase("uri")) {
158159
dataType = "URI";
159160
} else if (contentNode.get("enum") != null) {
160161
dataType = "Content." + capitalizedContentField;
162+
} else if (contentNode.get("type").asText().equals("array")) {
163+
JsonNode itemsNode = contentNode.get("items");
164+
dataType = itemsNode.get("type").asText();
165+
if(dataType.equals("string")){
166+
dataType = "List<String>";
167+
}
161168
} else {
162169
dataType = "String";
163170
}
164171
contentFields.add(new SchemaData.ContentField(contentField, capitalizedContentField, dataType));
172+
} else if (contentNode.get("anyOf") != null) {
173+
JsonNode contentAnyOfNode = contentNode.get("anyOf").elements().next();
174+
if (contentAnyOfNode.get("format") != null && contentAnyOfNode.get("format").asText().equalsIgnoreCase("uri")) {
175+
dataType = "URI";
176+
} else {
177+
dataType = "String";
178+
}
179+
contentFields.add(new SchemaData.ContentField(contentField, capitalizedContentField, dataType));
165180
} else {
166181
contentObjects.add(new SchemaData.ContentObject(capitalizedContentField));
167182
JsonNode contentObjectNode = contentNode.get("properties");
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class SchemaData {
1616

1717
private String schemaURL;
1818

19+
private String baseURI;
20+
1921
private List<ContentField> contentFields;
2022
private List<ContentObjectField> contentObjectFields;
2123
private List<ContentObject> contentObjects;
@@ -137,6 +139,22 @@ public void setSchemaURL(String schemaURL) {
137139
this.schemaURL = schemaURL;
138140
}
139141

142+
/**
143+
*
144+
* @return baseURI
145+
*/
146+
public String getBaseURI() {
147+
return baseURI;
148+
}
149+
150+
/**
151+
*
152+
* @param baseURI
153+
*/
154+
public void setBaseURI(String baseURI) {
155+
this.baseURI = baseURI;
156+
}
157+
140158
/**
141159
* @return the Content fields of an event
142160
*/

0 commit comments

Comments
 (0)