Skip to content

Commit 3bc98d1

Browse files
authored
Merge pull request #10238 from swagger-api/standalone-gen-dev
docs: standalone generator development
2 parents 46f199a + 7053b1d commit 3bc98d1

File tree

6 files changed

+191
-0
lines changed

6 files changed

+191
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ Once built, `run-in-docker.sh` will act as an executable for swagger-codegen-cli
231231
-l go -o /gen/out/go-petstore -DpackageName=petstore # generates go client, outputs locally to ./out/go-petstore
232232
```
233233

234+
#### Standalone generator Development in docker
235+
236+
See [standalone generator development](https://github.com/swagger-api/swagger-codegen/blob/3.0.0/standalone-gen-dev/standalone-generator-development.md)
237+
234238
#### Run Docker in Vagrant
235239
Prerequisite: install [Vagrant](https://www.vagrantup.com/downloads.html) and [VirtualBox](https://www.virtualbox.org/wiki/Downloads).
236240
```sh
@@ -468,6 +472,9 @@ java -cp output/myLibrary/target/myClientCodegen-swagger-codegen-1.0.0.jar:modul
468472
-o myClient
469473
```
470474

475+
See also [standalone generator development](https://github.com/swagger-api/swagger-codegen/blob/3.0.0/standalone-gen-dev/standalone-generator-development.md)
476+
477+
471478
### Generating a client from local files
472479
If you don't want to call your server, you can save the OpenAPI Spec files into a directory and pass an argument
473480
to the code generator like this:
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
# GEN_DIR allows to share the entrypoint between Dockerfile and run-in-docker.sh (backward compatible)
6+
GEN_DIR=${GEN_DIR:-/opt/swagger-codegen}
7+
JAVA_OPTS=${JAVA_OPTS:-"-Xmx1024M -Dlogback.configurationFile=conf/logback.xml"}
8+
9+
cli="${GEN_DIR}"
10+
codegen3="${cli}/swagger-codegen-cli.jar"
11+
12+
(cd "${GEN_DIR}" && exec mvn -Duser.home=$(dirname MAVEN_CONFIG) package)
13+
command=$1
14+
shift
15+
exec java ${JAVA_OPTS} -cp "${codegen3}:${GEN_DIR}/target/*" "io.swagger.codegen.v3.cli.SwaggerCodegen" "${command}" "$@"
16+

standalone-gen-dev/docker-stub.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
set -euo pipefail
3+
# GEN_DIR allows to share the entrypoint between Dockerfile and run-in-docker.sh (backward compatible)
4+
GEN_DIR=${GEN_DIR:-/opt/swagger-codegen}
5+
JAVA_OPTS=${JAVA_OPTS:-"-Xmx1024M -Dlogback.configurationFile=conf/logback.xml"}
6+
7+
codegen3="${GEN_DIR}/swagger-codegen-cli.jar"
8+
9+
command=$1
10+
exec java ${JAVA_OPTS} -jar "${codegen3}" "meta" "$@"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
set -exo pipefail
3+
4+
cd "$(dirname ${BASH_SOURCE})"
5+
6+
docker run --rm -it \
7+
-w /gen \
8+
-e GEN_DIR=/gen \
9+
-v "${PWD}:/gen" \
10+
--entrypoint /gen/docker-stub.sh \
11+
openjdk:8-jre-alpine "$@"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
set -exo pipefail
3+
4+
cd "$(dirname ${BASH_SOURCE})"
5+
6+
maven_cache_repo="${HOME}/.m2/repository"
7+
8+
mkdir -p "${maven_cache_repo}"
9+
10+
docker run --rm -it \
11+
-w /gen \
12+
-e GEN_DIR=/gen \
13+
-e MAVEN_CONFIG=/var/maven/.m2 \
14+
-u "$(id -u):$(id -g)" \
15+
-v "${PWD}:/gen" \
16+
-v "${maven_cache_repo}:/var/maven/.m2/repository" \
17+
--entrypoint /gen/docker-entrypoint.sh \
18+
maven:3-jdk-8 "$@"
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
### Swagger Codegen 3.0.0 Standalone generator development (separate project/repo)
2+
3+
As described in [Readme](https://github.com/swagger-api/swagger-codegen/tree/3.0.0#making-your-own-codegen-modules),
4+
a new generator can be implemented by starting with a project stub generated by the `meta` command of `swagger-codegen-cli`.
5+
6+
This can be achieved without needing to clone the `swagger-codegen` repo, by downloading and running the jar, e.g.:
7+
8+
```
9+
wget https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.19/swagger-codegen-cli-3.0.19.jar -O swagger-codegen-cli.jar
10+
java -jar swagger-codegen-cli.jar meta -o output/myLibrary -n myClientCodegen -p com.my.company.codegen
11+
```
12+
13+
Some additional details about codegen APIs of interest to the generator Java class(es) are [available here](https://github.com/swagger-api/swagger-codegen-generators/wiki/Adding-a-new-generator-for-a-language-or-framework.
14+
).
15+
16+
Such generator can be then made available to the CLI by adding it to the classpath, allowing to run/test it via the command line CLI,
17+
add it to the build pipeline and so on, as mentioned in [Readme](https://github.com/swagger-api/swagger-codegen/tree/3.0.0#making-your-own-codegen-modules).
18+
19+
#### Adding tests
20+
21+
One easy way to test the generator outcome without going through the CLI is adding a test like:
22+
23+
24+
```java
25+
26+
@Test
27+
public void testGenerator() throws Exception {
28+
29+
String path = getOutFolder(false).getAbsolutePath();
30+
GenerationRequest request = new GenerationRequest();
31+
request
32+
.codegenVersion(GenerationRequest.CodegenVersion.V3) // use V2 to target Swagger/OpenAPI 2.x Codegen version
33+
.type(GenerationRequest.Type.CLIENT)
34+
.lang("theNameOfMyCodegen")
35+
.spec(loadSpecAsNode( "theSpecIWantToTest.yaml",
36+
true, // YAML file, use false for JSON
37+
false)) // OpenAPI 3.x - use true for Swagger/OpenAPI 2.x definitions
38+
.options(
39+
new Options()
40+
.outputDir(path)
41+
);
42+
43+
List<File> files = new GeneratorService().generationRequest(request).generate();
44+
Assert.assertFalse(files.isEmpty());
45+
for (File f: files) {
46+
// test stuff
47+
}
48+
}
49+
50+
protected static File getOutFolder(boolean delete) {
51+
try {
52+
File outputFolder = new File("customCodegenOut");
53+
System.out.println(outputFolder.getAbsolutePath());
54+
if (delete) {
55+
// delete..
56+
}
57+
return outputFolder;
58+
} catch (Exception e) {
59+
e.printStackTrace();
60+
return null;
61+
}
62+
}
63+
64+
protected JsonNode loadSpecAsNode(final String file, boolean yaml, boolean v2) {
65+
InputStream in = null;
66+
String s = "";
67+
try {
68+
in = getClass().getClassLoader().getResourceAsStream(file);
69+
if (yaml) {
70+
if (v2) {
71+
return Yaml.mapper().readTree(in);
72+
} else {
73+
return io.swagger.v3.core.util.Yaml.mapper().readTree(in);
74+
}
75+
}
76+
if (v2) {
77+
return Json.mapper().readTree(in);
78+
}
79+
return io.swagger.v3.core.util.Json.mapper().readTree(in);
80+
} catch (Exception e) {
81+
throw new RuntimeException("could not load file " + file);
82+
} finally {
83+
IOUtils.closeQuietly(in);
84+
}
85+
}
86+
```
87+
88+
#### Development in docker
89+
90+
Similar to what mentioned in Readme [development in docker section](https://github.com/swagger-api/swagger-codegen/tree/3.0.0#development-in-docker), a standalone generator can be built and run in docker, without need of a java/maven environment on the local machine.
91+
92+
Generate the initial project:
93+
94+
```bash
95+
96+
# project dir
97+
TARGET_DIR=/tmp/codegen/mygenerator
98+
mkdir -p $TARGET_DIR
99+
cd $TARGET_DIR
100+
# generated code location
101+
GENERATED_CODE_DIR=generated
102+
mkdir -p $GENERATED_CODE_DIR
103+
# download desired version
104+
wget https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.19/swagger-codegen-cli-3.0.19.jar -O swagger-codegen-cli.jar
105+
wget https://raw.githubusercontent.com/swagger-api/swagger-codegen/3.0.0/standalone-gen-dev/docker-stub.sh -O docker-stub.sh
106+
wget https://raw.githubusercontent.com/swagger-api/swagger-codegen/3.0.0/standalone-gen-dev/generator-stub-docker.sh -O generator-stub-docker.sh
107+
chmod +x *.sh
108+
# generated initial stub: -p <root package> -n <generator name>
109+
./generator-stub-docker.sh -p io.swagger.codegen.custom -n custom
110+
111+
```
112+
113+
A test definition if we don't have one:
114+
115+
```bash
116+
wget https://raw.githubusercontent.com/swagger-api/swagger-codegen/3.0.0/modules/swagger-codegen/src/test/resources/3_0_0/petstore-simple.yaml -O petstore-simple.yaml
117+
```
118+
119+
120+
Build the generator and run it against a definition (first time will be slower as it needs to download deps)
121+
122+
```bash
123+
wget https://raw.githubusercontent.com/swagger-api/swagger-codegen/3.0.0/standalone-gen-dev/run-in-docker.sh -O run-in-docker.sh
124+
wget https://raw.githubusercontent.com/swagger-api/swagger-codegen/3.0.0/standalone-gen-dev/docker-entrypoint.sh -O docker-entrypoint.sh
125+
chmod +x *.sh
126+
./run-in-docker.sh generate -i petstore-simple.yaml -l custom -o /gen/$GENERATED_CODE_DIR
127+
```
128+
129+

0 commit comments

Comments
 (0)