Skip to content

Commit

Permalink
Generate SDK with spec 0.4.1
Browse files Browse the repository at this point in the history
Signed-off-by: Jalander Ramagiri <[email protected]>
  • Loading branch information
Jalander Ramagiri committed May 22, 2024
1 parent ddd02fb commit 0b345f8
Show file tree
Hide file tree
Showing 161 changed files with 12,804 additions and 537 deletions.
41 changes: 41 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,44 @@ To run all targets, before creating a commit:
```shell
./mvnw verify
```

## Upgrade SDK to the Latest CDEvents Spec
The [Java SDK Generator](./generator) is utilized to create events from the [CDEvents Spec schemas](./spec/schemas),
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.
- Steps involved to generate latest SDK
1. Update spec submodule to the latest release
- Create a private working branch from [cdevents/sdk-java](https://github.com/cdevents/sdk-java)
- Checkout the latest spec version and update the submodule from spec root directory
````bash
cd spec
git checkout main && git pull
git checkout <latest-release-tag>
cd ../
````
- Verify the latest Spec is applied and Push the modified spec to your private working branch
2. Generate model classes from [CDEvents Spec schemas](./spec/schemas) using maven plugin `jsonschema2pojo`
- Compare the existing `jsonschema2pojo` plugin configuration in the [generator's pom.xml file](./generator/pom.xml) with the available [CDEvents Spec schemas](./spec/schemas)
- Add/update executions for `jsonschema2pojo` plugin configuration in the [generator's pom.xml file](./generator/pom.xml) as below,
````pom
<execution>
<id>generate-<subject>-<predicate>-from-schema</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<sourcePaths>
<sourcePath>${parent.project.dir}/spec/schemas/<schema-file-name>.json</sourcePath>
</sourcePaths>
<targetPackage>dev.cdevents.models.<subject>.<predicate></targetPackage>
</configuration>
</execution>
````
- 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)

2. Generate CDEvent classes using [mustache template](./generator/src/main/resources/template/event-template.mustache)
- [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
- All the [CDEvents](./sdk/src/main/java/dev/cdevents/events) generated by using `event-template.mustache`
- Run `./mvnw verify` and fix if any issues found during the build process
- Add/Update test cases for the new/updated events to validate that they are generated correctly in [sdk](./sdk)

135 changes: 134 additions & 1 deletion generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>run-PreprocessSchemas-main-class</id>
<phase>generate-sources</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>dev.cdevents.generator.PreprocessSchemas</mainClass>
<arguments>
<argument>${parent.project.dir}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
Expand All @@ -77,7 +97,68 @@
<targetVersion>8</targetVersion>
<includeGeneratedAnnotation>true</includeGeneratedAnnotation>
</configuration>
<executions>
<executions>
<execution>
<id>generate-embedded-links-from-schema</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<sourcePaths>
<sourcePath>${parent.project.dir}/spec/schemas/links/embeddedlinkend.json</sourcePath>
<sourcePath>${parent.project.dir}/spec/schemas/links/embeddedlinkpath.json</sourcePath>
<sourcePath>${parent.project.dir}/spec/schemas/links/embeddedlinkrelation.json</sourcePath>
<sourcePath>${parent.project.dir}/spec/schemas/links/embeddedlinksarray.json</sourcePath>
</sourcePaths>
<!--<sourceDirectory>${parent.project.dir}/spec/schemas/links</sourceDirectory>
<includes>embedded.*\.json</includes>-->
<targetPackage>dev.cdevents.models.links.embedded</targetPackage>
</configuration>
</execution>
<execution>
<id>generate-external-links-from-schema</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<sourcePaths>
<sourcePath>${parent.project.dir}/spec/schemas/links/linkend.json</sourcePath>
<sourcePath>${parent.project.dir}/spec/schemas/links/linkpath.json</sourcePath>
<sourcePath>${parent.project.dir}/spec/schemas/links/linkrelation.json</sourcePath>
<sourcePath>${parent.project.dir}/spec/schemas/links/linkstart.json</sourcePath>
</sourcePaths>
<targetPackage>dev.cdevents.models.links</targetPackage>
</configuration>
</execution>
<execution>
<id>generate-artifact-deleted-from-schema</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<sourcePaths>
<sourcePath>${parent.project.dir}/spec/schemas/artifactdeleted.json</sourcePath>
</sourcePaths>
<targetPackage>dev.cdevents.models.artifact.deleted</targetPackage>
</configuration>
</execution>

<execution>
<id>generate-artifact-downloaded-from-schema</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<sourcePaths>
<sourcePath>${parent.project.dir}/spec/schemas/artifactdownloaded.json</sourcePath>
</sourcePaths>
<targetPackage>dev.cdevents.models.artifact.downloaded</targetPackage>
</configuration>
</execution>
<execution>
<id>generate-artifact-packaged-from-schema</id>
<phase>generate-sources</phase>
Expand Down Expand Up @@ -521,6 +602,19 @@
<targetPackage>dev.cdevents.models.testcaserun.queued</targetPackage>
</configuration>
</execution>
<execution>
<id>generate-testcaserun-skipped-from-schema</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<sourcePaths>
<sourcePath>${parent.project.dir}/spec/schemas/testcaserunskipped.json</sourcePath>
</sourcePaths>
<targetPackage>dev.cdevents.models.testcaserun.skipped</targetPackage>
</configuration>
</execution>
<execution>
<id>generate-testcaserun-started-from-schema</id>
<phase>generate-sources</phase>
Expand Down Expand Up @@ -586,6 +680,45 @@
<targetPackage>dev.cdevents.models.testsuiterun.started</targetPackage>
</configuration>
</execution>
<execution>
<id>generate-ticket-closed-from-schema</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<sourcePaths>
<sourcePath>${parent.project.dir}/spec/schemas/ticketclosed.json</sourcePath>
</sourcePaths>
<targetPackage>dev.cdevents.models.ticket.closed</targetPackage>
</configuration>
</execution>
<execution>
<id>generate-ticket-created-from-schema</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<sourcePaths>
<sourcePath>${parent.project.dir}/spec/schemas/ticketcreated.json</sourcePath>
</sourcePaths>
<targetPackage>dev.cdevents.models.ticket.created</targetPackage>
</configuration>
</execution>
<execution>
<id>generate-ticket-updated-from-schema</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<sourcePaths>
<sourcePath>${parent.project.dir}/spec/schemas/ticketupdated.json</sourcePath>
</sourcePaths>
<targetPackage>dev.cdevents.models.ticket.updated</targetPackage>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ private static SchemaData buildCDEventDataFromJsonSchema(File file) {
String upperCaseSubject = getUpperCaseSubjectType(subjectType);
//set the Schema JsonNode required values to schemaData
schemaData.setSchemaURL(schemaURL);
schemaData.setBaseURI(schemaURL.substring(0, schemaURL.lastIndexOf("/") + 1));
schemaData.setSubject(subject);
schemaData.setPredicate(predicate);
schemaData.setCapitalizedSubject(capitalizedSubject);
Expand Down Expand Up @@ -153,15 +154,29 @@ private static void updateSubjectContentProperties(SchemaData schemaData, JsonNo
String capitalizedContentField = StringUtils.capitalize(contentField);
JsonNode contentNode = contentMap.getValue();
String dataType = "";
if (!contentNode.get("type").asText().equals("object")) {
if (contentNode.get("type") != null && !contentNode.get("type").asText().equals("object")) {
if (contentNode.get("format") != null && contentNode.get("format").asText().equalsIgnoreCase("uri")) {
dataType = "URI";
} else if (contentNode.get("enum") != null) {
dataType = "Content." + capitalizedContentField;
} else if (contentNode.get("type").asText().equals("array")) {
JsonNode itemsNode = contentNode.get("items");
dataType = itemsNode.get("type").asText();
if(dataType.equals("string")){
dataType = "List<String>";
}
} else {
dataType = "String";
}
contentFields.add(new SchemaData.ContentField(contentField, capitalizedContentField, dataType));
} else if (contentNode.get("anyOf") != null) {
JsonNode contentAnyOfNode = contentNode.get("anyOf").elements().next();
if (contentAnyOfNode.get("format") != null && contentAnyOfNode.get("format").asText().equalsIgnoreCase("uri")) {
dataType = "URI";
} else {
dataType = "String";
}
contentFields.add(new SchemaData.ContentField(contentField, capitalizedContentField, dataType));
} else {
contentObjects.add(new SchemaData.ContentObject(capitalizedContentField));
JsonNode contentObjectNode = contentNode.get("properties");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package dev.cdevents.generator;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;

public class PreprocessSchemas {

private static final ObjectMapper objectMapper = new ObjectMapper();

private static final Logger log = LoggerFactory.getLogger(PreprocessSchemas.class);

public static void main(String[] args) throws IOException {
if (args == null || args.length != 1) {
System.err.println("Usage: PreprocessSchemas <schema_directory_path>");
throw new IllegalArgumentException("Schema directory path arguments not passed to PreprocessSchemas");
}
String parentBaseDir = args[0];
String specSchemaDir = parentBaseDir + File.separator + "spec" + File.separator + "schemas";
Files.walk(Paths.get(specSchemaDir))
.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(".json"))
.forEach(PreprocessSchemas::processFile);
}

private static void processFile(Path filePath) {
log.info("processing schema file {} to update $ref path with json extension.", filePath.getFileName());
try {
JsonNode rootNode = objectMapper.readTree(filePath.toFile());
updateRefs(rootNode);
objectMapper.writerWithDefaultPrettyPrinter().writeValue(filePath.toFile(), rootNode);
} catch (IOException e) {
log.error("Exception occurred while process schema file to update ref {}", e.getMessage());
throw new IllegalStateException("Exception occurred while process schema file to update ref ", e);
}
}

private static void updateRefs(JsonNode node) {
if (node.isObject()) {
ObjectNode objectNode = (ObjectNode) node;
Iterator<String> fieldNames = objectNode.fieldNames();
while (fieldNames.hasNext()) {
String fieldName = fieldNames.next();
JsonNode childNode = objectNode.get(fieldName);
if ("$ref".equals(fieldName) && !childNode.asText().endsWith(".json")) {
objectNode.put(fieldName, childNode.asText() + ".json");
} else {
updateRefs(childNode);
}
}
} else if (node.isArray()) {
for (JsonNode arrayItem : node) {
updateRefs(arrayItem);
}
}
}
}
18 changes: 18 additions & 0 deletions generator/src/main/java/dev/cdevents/generator/SchemaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class SchemaData {

private String schemaURL;

private String baseURI;

private List<ContentField> contentFields;
private List<ContentObjectField> contentObjectFields;
private List<ContentObject> contentObjects;
Expand Down Expand Up @@ -137,6 +139,22 @@ public void setSchemaURL(String schemaURL) {
this.schemaURL = schemaURL;
}

/**
*
* @return baseURI
*/
public String getBaseURI() {
return baseURI;
}

/**
*
* @param baseURI
*/
public void setBaseURI(String baseURI) {
this.baseURI = baseURI;
}

/**
* @return the Content fields of an event
*/
Expand Down
Loading

0 comments on commit 0b345f8

Please sign in to comment.