Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Oct 18, 2023
1 parent f21e8e7 commit 2cd605a
Show file tree
Hide file tree
Showing 14 changed files with 320 additions and 3 deletions.
17 changes: 17 additions & 0 deletions docs/integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,20 @@ To be on the safeside, only enable the user to chose between the internal option

## Integrate Modelina in an AsyncAPI generator template
TODO

## Integrate Modelina into Maven

There are at least two ways you can integrate Modelina into your build process for Maven projects, either with the AsyncAPI CLI or with a custom build script. Which one to choose all depends on your scenario, look below:

**Custom build script**
- DO work with other inputs then AsyncAPI
- DO work when needing extensive build options and configurations

Checkout the Maven example here: [Integrate Modelina into Maven](../examples/integrate-modelina-into-maven/maven-project/)

**AsyncAPI CLI**

- DO NOT work if you have other inputs then AsyncAPI
- DO NOT need extensive build options and configuration

We don't have a full example for this, but you can use similar concept as the custom build script. However, instead you just install and call the AsyncAPI CLI directly in the plugin execution when utilizing the [frontend-maven-plugin](https://github.com/eirslett/frontend-maven-plugin) and [more specifically the NPX execution](https://github.com/eirslett/frontend-maven-plugin#npx).
3 changes: 3 additions & 0 deletions docs/languages/Java.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,6 @@ Currently not supported, [let everyone know you need it](https://github.com/asyn

### To and from binary
Currently not supported, [let everyone know you need it](https://github.com/asyncapi/modelina/issues/new?assignees=&labels=enhancement&template=enhancement.md)!

## Integrate Modelina into Maven
We have created an example Maven project to show you how to generate AsyncAPI payload models from your AsyncAPI file and integrate it into the build process. [You can find the integration example here](../integration.md#integrate-modelina-into-maven).
4 changes: 3 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ These are all the basic generator examples that shows a bare minimal example of

## Integrations
These are examples of how you can integrate Modelina into a specific scenario:
- [integrate with React](./integrate-with-react) - A basic example that shows how you can integrate Modelina with React.
- [integrate-with-react](./integrate-with-react) - A basic example that shows how you can integrate Modelina with React.
- [integrate-with-next](./integrate-with-next) - A basic example that shows how you can integrate Modelina with Next.
- [integrate-modelina-into-maven/](./integrate-modelina-into-maven/) - A basic example that shows how you can integrate Modelina into the Java Maven build process.

## Python
These are all specific examples only relevant to the Python generator:
Expand All @@ -94,6 +95,7 @@ These are all specific examples only relevant to the Java generator:
- [java-generate-equals](./java-generate-equals) - A basic example that shows how to generate models that overwrite the `equal` method
- [java-generate-javax-constraint-annotation](./java-generate-javax-constraint-annotation) - A basic example that shows how Java data models having `javax.validation.constraints` annotations can be generated.
- [java-generate-javadoc](./java-generate-javadoc) - A basic example of how to generate Java models including JavaDocs.
- [integrate-modelina-into-maven](./integrate-modelina-into-maven/) - A basic example that shows how you can integrate Modelina into the Java Maven build process.

## C#
These are all specific examples only relevant to the C# generator:
Expand Down
3 changes: 3 additions & 0 deletions examples/integrate-modelina-into-maven/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Integrate Modelina into Maven

This example show how you can integrate Modelina into Maven with a custom build script. [Please have a look at the Maven project README file for further information](./maven-project/).
25 changes: 25 additions & 0 deletions examples/integrate-modelina-into-maven/maven-project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar

# Eclipse m2e generated files
# Eclipse Core
.project
# JDT-specific (Eclipse Java Development Tools)
.classpath

bin
.settings

# Ignore Modelina generation node modules
scripts/modelina/java_runtime_node
scripts/modelina/node_modules
10 changes: 10 additions & 0 deletions examples/integrate-modelina-into-maven/maven-project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Integrate Modelina into Maven

This Java Maven project shows an example how to integrate Modelina and AsyncAPI into your build process.

Here is how it works:
- The script `./scripts/modelina/generate.ts` is what generates all the models, and what Maven uses to generate the models. This can also be executed manually through `npm run generate`.
- The input, in this case, is an AsyncAPI document located in the root of the project `./asyncapi.json`. The input can be anything, just alter the generator script.
- The Maven project file `./pom.xml` then utilizes the [frontend-maven-plugin](https://github.com/eirslett/frontend-maven-plugin) to execute the generate script on build so you will always have the up to date models from your AsyncAPI document.

> NOTICE: The only thing you manually have to change for this to work in your project is the dependency entry for `"@asyncapi/modelina": "file:../../../../",` in the `./scripts/modelina/package.json` file to use the latest Modelina version (we only use a local one for testing purposes).
26 changes: 26 additions & 0 deletions examples/integrate-modelina-into-maven/maven-project/asyncapi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"asyncapi": "2.2.0",
"info": {
"title": "example",
"version": "0.1.0"
},
"channels": {
"/test": {
"subscribe": {
"message": {
"payload": {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"additionalProperties": false,
"properties": {
"email": {
"type": "string",
"format": "email"
}
}
}
}
}
}
}
}
113 changes: 113 additions & 0 deletions examples/integrate-modelina-into-maven/maven-project/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany.app</groupId>
<artifactId>maven-project</artifactId>
<version>1.0-SNAPSHOT</version>

<name>maven-project</name>
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.15.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.12.1</version>
<configuration>
<workingDirectory>./scripts/modelina</workingDirectory>
<installDirectory>java_runtime_node</installDirectory>
</configuration>
<executions>
<!-- It will install nodejs and npm -->
<execution>
<id>install node and npm</id>
<phase>generate-sources</phase>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v18.15.0</nodeVersion>
</configuration>
</execution>
<execution>
<id>Generate sources</id>
<phase>generate-sources</phase>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>run generate</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { JAVA_JACKSON_PRESET, JavaFileGenerator } from '@asyncapi/modelina';
import path from 'path';

// Where should the models be placed relative to root maven project?
const PACKAGE_NAME = 'java/com/mycompany/app';
const MODEL_DIR = `src/main/${PACKAGE_NAME}`;

const FINAL_OUTPUT_PATH = path.resolve(__dirname, '../../', MODEL_DIR);
// Setup the generator and all it's configurations
const generator = new JavaFileGenerator({
presets: [JAVA_JACKSON_PRESET]
});

// Load the input from file, memory, or remotely.
// Here we just use a local AsyncAPI file
import INPUT from '../../asyncapi.json';
const input = INPUT;

// Generate all the files
generator.generateToFiles(
input,
FINAL_OUTPUT_PATH,
{
packageName: 'java/com/mycompany/app'
}
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"dependencies": {
"@asyncapi/modelina": "file:../../../../",
"ts-node": "^10.3.0"
},
"scripts": {
"generate": "./node_modules/.bin/ts-node ./generate.ts",
"generate:windows": "..\\node_modules\\.bin\\ts-node .%\\generate.ts"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.mycompany.app.generic;
import com.mycompany.app.generic.NestedObject;
import java.util.Map;
import com.fasterxml.jackson.annotation.*;
public class Address {
@JsonProperty("street_name")
private String streetName;
@JsonProperty("house_number")
private Double houseNumber;
@JsonProperty("marriage")
private boolean marriage;
@JsonProperty("members")
private Object members;
@JsonProperty("array_type")
private Object[] arrayType;
@JsonProperty("nestedObject")
private NestedObject nestedObject;
private Map<String, Object> additionalProperties;

public String getStreetName() { return this.streetName; }
public void setStreetName(String streetName) { this.streetName = streetName; }

public Double getHouseNumber() { return this.houseNumber; }
public void setHouseNumber(Double houseNumber) { this.houseNumber = houseNumber; }

public boolean getMarriage() { return this.marriage; }
public void setMarriage(boolean marriage) { this.marriage = marriage; }

public Object getMembers() { return this.members; }
public void setMembers(Object members) { this.members = members; }

public Object[] getArrayType() { return this.arrayType; }
public void setArrayType(Object[] arrayType) { this.arrayType = arrayType; }

public NestedObject getNestedObject() { return this.nestedObject; }
public void setNestedObject(NestedObject nestedObject) { this.nestedObject = nestedObject; }

public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mycompany.app.generic;

import java.util.Map;
import com.fasterxml.jackson.annotation.*;
public class NestedObject {
@JsonProperty("test")
private String test;
private Map<String, Object> additionalProperties;

public String getTest() { return this.test; }
public void setTest(String test) { this.test = test; }

public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
}
12 changes: 12 additions & 0 deletions examples/integrate-modelina-into-maven/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"config": {
"example_name": "integrate-modelina-into-maven"
},
"scripts": {
"install": "(cd ../.. && npm i) && cd maven-project/scripts/modelina && npm i",
"start": "cd ./examples/$npm_package_config_example_name/maven-project/scripts/modelina && npm run generate",
"start:windows": "cd ./examples/%npm_package_config_example_name%/maven-project/scripts/modelina && npm run generate:windows",
"test": "npm run start",
"test:windows": "npm run start:windows"
}
}
19 changes: 17 additions & 2 deletions modelina-website/scripts/build-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ const nameMapping = [
'php'
];

const examplesToIgnore = [
'integrate-with-react',
'integrate-with-next',
'integrate-modelina-into-maven',
'TEMPLATE'
]


/**
* Find the proper language type for the output code.
*
Expand All @@ -53,7 +61,7 @@ function getOutput(exampleDirPath){
const consoleLogOutputPath = path.resolve(exampleDirPath, './__snapshots__/index.spec.ts.snap');
let output = require(consoleLogOutputPath);
output = Object.values(output).map((exportValue) => {
const searchValue = 'Array [\n \"\"';
const searchValue = 'Array [\n ""';
exportValue = exportValue.slice(searchValue.length, exportValue.length);
exportValue = exportValue.slice(0, exportValue.length-5);
exportValue = exportValue.replace(/\\/g, "");
Expand Down Expand Up @@ -83,7 +91,7 @@ async function start() {
//Filter out any examples that either:
// 1. are impossible to show (react and next examples)
// 2. should not be shown
exampleDirs = exampleDirs.filter((dir) => dir !== 'TEMPLATE' && dir !== 'integrate-with-react' && dir !== 'integrate-with-next');
exampleDirs = exampleDirs.filter((dir) => !examplesToIgnore.includes(dir));
const templateConfig = {};

for (const example of exampleDirs) {
Expand Down Expand Up @@ -111,9 +119,16 @@ async function start() {
mainReadme = mainReadme.replace('<!-- toc is generated with GitHub Actions do not remove toc markers -->', '');
mainReadme = mainReadme.replace('<!-- toc -->', '');
mainReadme = mainReadme.replace('<!-- tocstop -->', '');
<<<<<<< Updated upstream
mainReadme = mainReadme.replace('- [integrate with React](?selectedExample=integrate-with-react/)', '- [integrate with React](https://github.com/asyncapi/modelina/tree/master/examples/integrate-with-react)');
mainReadme = mainReadme.replace('- [integrate with Next](?selectedExample=integrate-with-next/)', '- [integrate with Next](https://github.com/asyncapi/modelina/tree/master/examples/integrate-with-next)');
mainReadme = mainReadme.replace('- [TEMPLATE](?selectedExample=TEMPLATE)', '- [TEMPLATE](https://github.com/asyncapi/modelina/tree/master/examples/TEMPLATE)');
=======
mainReadme = mainReadme.replace('../docs/contributing.md', 'https://github.com/asyncapi/modelina/tree/master/examples/../docs/contributing.md');
for (const exampleToIgnore of exampleToIgnore) {
mainReadme = mainReadme.replace(`- [${exampleToIgnore}](?selectedExample=${exampleToIgnore}/)`, `- [${exampleToIgnore}](https://github.com/asyncapi/modelina/tree/master/examples/${exampleToIgnore})`);
}
>>>>>>> Stashed changes
const readmePath = path.resolve(__dirname, '../config/examples_readme.json');
await writeFile(readmePath, JSON.stringify(mainReadme))
}
Expand Down

0 comments on commit 2cd605a

Please sign in to comment.