Skip to content

Commit 0db0430

Browse files
authored
feat: add java generator (#176)
* feat: add java generator * update README.md
1 parent 73e9713 commit 0db0430

File tree

8 files changed

+410
-5
lines changed

8 files changed

+410
-5
lines changed

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,11 @@ download-oas:
1717
generate-sdk:
1818
@$(SCRIPTS_BASE)/generate-sdk/generate-sdk.sh "$(GIT_HOST)" "$(GIT_USER_ID)" "$(GIT_REPO_ID)" "$(SDK_REPO_URL)" "$(LANGUAGE)" "$(SDK_BRANCH)"
1919

20+
generate-go-sdk:
21+
@$(SCRIPTS_BASE)/generate-sdk/generate-sdk.sh "$(GIT_HOST)" "$(GIT_USER_ID)" "$(GIT_REPO_ID)" "$(SDK_REPO_URL)" "go" "$(SDK_BRANCH)"
22+
23+
generate-python-sdk:
24+
@$(SCRIPTS_BASE)/generate-sdk/generate-sdk.sh "$(GIT_HOST)" "$(GIT_USER_ID)" "$(GIT_REPO_ID)" "$(SDK_REPO_URL)" "python" "$(SDK_BRANCH)"
25+
26+
generate-java-sdk:
27+
@$(SCRIPTS_BASE)/generate-sdk/generate-sdk.sh "$(GIT_HOST)" "$(GIT_USER_ID)" "$(GIT_REPO_ID)" "$(SDK_REPO_URL)" "java" "$(SDK_BRANCH)"

README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,29 @@ If you want to modify script or templates and you can run code locally.
1313
Requires `Go 1.21` or higher.
1414

1515
1. Set up the project and tools by running
16-
```
16+
```bash
1717
make project-tools
1818
```
1919
2. Download Open Api Specifications (OAS), that are the input for the SDK generation, by running
20-
```
20+
```bash
2121
make download-oas
2222
```
2323
This step needs to be done only at the first start and when OAS updates are present.
24-
3. Run the SDK generation for testing by
24+
3. Run the Go SDK generation for testing by
25+
```bash
26+
make generate-go-sdk
27+
```
28+
The output goes to the `./sdk-repo-updated` folder.
29+
1. Run the Python SDK generation for testing by
30+
```bash
31+
make generate-python-sdk
2532
```
26-
make generate-sdk
33+
The output goes to the `./sdk-repo-updated` folder.
34+
2. Run the Java SDK generation for testing by
35+
```bash
36+
make generate-java-sdk
2737
```
28-
The output goes to the `./sdk` folder.
38+
The output goes to the `./sdk-repo-updated` folder.
2939

3040
## Reporting issues
3141

openapi-generator-config-java.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
templateDir: templates/java
2+
additionalProperties:
3+
artifactUrl: https://github.com/stackitcloud/stackit-sdk-java
4+
developerName: STACKIT Developer Tools
5+
developerEmail: [email protected]
6+
developerOrganization: STACKIT
7+
developerOrganizationUrl: https://www.stackit.de
8+
groupId: cloud.stackit
9+
hideGenerationTimestamp: true
10+
licenseName: Apache License 2.0
11+
licenseUrl: http://www.apache.org/licenses/
12+
scmConnection: scm:[email protected]:stackitcloud/stackit-sdk-java.git
13+
scmDeveloperConnection: scm:[email protected]:stackitcloud/stackit-sdk-java.git
14+
scmUrl: https://github.com/stackitcloud/stackit-sdk-java
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
git_push.sh
2+
.travis.yml
3+
.gitignore
4+
api/openapi.yaml
5+
tox.ini
6+
**/.github/**
7+
**/AndroidManifest.xml
8+
pom.xml
9+
build.sbt

scripts/generate-sdk/generate-sdk.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ go)
5757
GENERATOR_VERSION="v6.6.0" # There are issues with GO SDK generation in version v7
5858
;;
5959
python)
60+
# Renovate: datasource=github-tags depName=OpenAPITools/openapi-generator versioning=semver
61+
GENERATOR_VERSION="v7.14.0"
62+
;;
63+
java)
6064
# Renovate: datasource=github-tags depName=OpenAPITools/openapi-generator versioning=semver
6165
GENERATOR_VERSION="v7.14.0"
6266
;;
@@ -94,6 +98,13 @@ python)
9498
# Usage: generate_python_sdk GENERATOR_PATH GIT_HOST GIT_USER_ID [GIT_REPO_ID] [SDK_REPO_URL] [SDK_BRANCH]
9599
generate_python_sdk "${jar_path}" "${GIT_HOST}" "${GIT_USER_ID}" "${GIT_REPO_ID}" "${SDK_REPO_URL}" "${SDK_BRANCH}"
96100
;;
101+
java)
102+
echo -e "\n>> Generating the Java SDK..."
103+
104+
source ${LANGUAGE_GENERATORS_FOLDER_PATH}/${LANGUAGE}.sh
105+
# Usage: generate_java_sdk GENERATOR_PATH GIT_HOST GIT_USER_ID [GIT_REPO_ID] [SDK_REPO_URL] [SDK_BRANCH]
106+
generate_java_sdk "${jar_path}" "${GIT_HOST}" "${GIT_USER_ID}" "${GIT_REPO_ID}" "${SDK_REPO_URL}" "${SDK_BRANCH}"
107+
;;
97108
*)
98109
echo "! SDK language not supported."
99110
exit 1
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#!/bin/bash
2+
# This script clones the SDK repo and updates it with the generated API modules
3+
# Pre-requisites: Java
4+
set -eo pipefail
5+
6+
ROOT_DIR=$(git rev-parse --show-toplevel)
7+
SDK_REPO_LOCAL_PATH="${ROOT_DIR}/sdk-repo-updated"
8+
9+
SERVICES_FOLDER="${SDK_REPO_LOCAL_PATH}/services"
10+
11+
GENERATOR_LOG_LEVEL="error" # Must be a Java log level (error, warn, info...)
12+
13+
INCLUDE_SERVICES=("resourcemanager")
14+
15+
generate_java_sdk() {
16+
# Required parameters
17+
local GENERATOR_JAR_PATH=$1
18+
local GIT_HOST=$2
19+
local GIT_USER_ID=$3
20+
21+
# Optional parameters
22+
local GIT_REPO_ID=$4
23+
local SDK_REPO_URL=$5
24+
local SDK_BRANCH=$6
25+
26+
# Check required parameters
27+
if [[ -z ${GIT_HOST} ]]; then
28+
echo "! GIT_HOST not specified."
29+
exit 1
30+
fi
31+
32+
if [[ -z ${GIT_USER_ID} ]]; then
33+
echo "! GIT_USER_ID id not specified."
34+
exit 1
35+
fi
36+
37+
# Check optional parameters and set defaults if not provided
38+
if [[ -z ${GIT_REPO_ID} ]]; then
39+
echo "GIT_REPO_ID not specified, default will be used."
40+
GIT_REPO_ID="stackit-sdk-java"
41+
fi
42+
43+
if [[ -z ${SDK_REPO_URL} ]]; then
44+
echo "SDK_REPO_URL not specified, default will be used."
45+
SDK_REPO_URL="https://github.com/stackitcloud/stackit-sdk-java.git"
46+
fi
47+
48+
# Prepare folders
49+
if [[ ! -d $SERVICES_FOLDER ]]; then
50+
mkdir -p "$SERVICES_FOLDER"
51+
fi
52+
53+
# Clone SDK repo
54+
if [ -d ${SDK_REPO_LOCAL_PATH} ]; then
55+
echo "Old SDK repo clone was found, it will be removed."
56+
rm -rf ${SDK_REPO_LOCAL_PATH}
57+
fi
58+
git clone --quiet -b ${SDK_BRANCH} ${SDK_REPO_URL} ${SDK_REPO_LOCAL_PATH}
59+
60+
# Backup of the current state of the SDK services dir (services/)
61+
sdk_services_backup_dir=$(mktemp -d)
62+
if [[ ! ${sdk_services_backup_dir} || -d {sdk_services_backup_dir} ]]; then
63+
echo "! Unable to create temporary directory"
64+
exit 1
65+
fi
66+
cleanup() {
67+
rm -rf ${sdk_services_backup_dir}
68+
}
69+
cp -a "${SERVICES_FOLDER}/." ${sdk_services_backup_dir}
70+
71+
# Cleanup after we are done
72+
trap cleanup EXIT
73+
74+
# Remove old contents of services dir (services/)
75+
rm -rf ${SERVICES_FOLDER}
76+
77+
# Generate SDK for each service
78+
for service_json in ${ROOT_DIR}/oas/*.json; do
79+
service="${service_json##*/}"
80+
service="${service%.json}"
81+
82+
# Remove invalid characters to ensure a valid Java pkg name
83+
service="${service//-/}" # remove dashes
84+
service="${service// /}" # remove spaces
85+
service=$(echo "${service}" | tr '[:upper:]' '[:lower:]') # convert upper case letters to lower case
86+
service=$(echo "${service}" | tr -d -c '[:alnum:]') # remove non-alphanumeric characters
87+
88+
# Ensure the package name doesn't start with a number
89+
if [[ "${service}" =~ ^[0-9] ]]; then
90+
service="_${service}" # Prepend a valid prefix if it starts with a number
91+
fi
92+
93+
if ! [[ ${INCLUDE_SERVICES[*]} =~ ${service} ]]; then
94+
echo "Skipping not included service ${service}"
95+
continue
96+
fi
97+
98+
if grep -E "^$service$" ${ROOT_DIR}/blacklist.txt; then
99+
echo "Skipping blacklisted service ${service}"
100+
continue
101+
fi
102+
103+
echo ">> Generating \"${service}\" service..."
104+
cd ${ROOT_DIR}
105+
106+
mkdir -p "${SERVICES_FOLDER}/${service}/"
107+
cp "${ROOT_DIR}/scripts/generate-sdk/.openapi-generator-ignore-java" "${SERVICES_FOLDER}/${service}/.openapi-generator-ignore"
108+
109+
SERVICE_DESCRIPTION=$(cat ${service_json} | jq .info.title --raw-output)
110+
111+
# Run the generator
112+
java -Dlog.level=${GENERATOR_LOG_LEVEL} -jar ${jar_path} generate \
113+
--generator-name java \
114+
--input-spec "${service_json}" \
115+
--output "${SERVICES_FOLDER}/${service}" \
116+
--git-host ${GIT_HOST} \
117+
--git-user-id ${GIT_USER_ID} \
118+
--git-repo-id ${GIT_REPO_ID} \
119+
--global-property apis,models,modelTests=false,modelDocs=false,apiDocs=false,apiTests=false,supportingFiles \
120+
--additional-properties=artifactId="stackit-sdk-${service}",artifactDescription="${SERVICE_DESCRIPTION}",invokerPackage="cloud.stackit.sdk.${service}",modelPackage="cloud.stackit.sdk.${service}.model",apiPackage="cloud.stackit.sdk.${service}.api" >/dev/null \
121+
--http-user-agent stackit-sdk-java/"${service}" \
122+
--config openapi-generator-config-java.yml
123+
124+
# Remove unnecessary files
125+
rm "${SERVICES_FOLDER}/${service}/.openapi-generator-ignore"
126+
rm -r "${SERVICES_FOLDER}/${service}/.openapi-generator/"
127+
rm "${SERVICES_FOLDER}/${service}/.github/workflows/maven.yml"
128+
129+
# If the service has a README.md file, move them inside the service folder
130+
if [ -f ${sdk_services_backup_dir}/${service}/README.md ]; then
131+
echo "Found ${service} \"README.md\" file"
132+
cp -r ${sdk_services_backup_dir}/${service}/README.md ${SERVICES_FOLDER}/${service}/README.md
133+
fi
134+
135+
# If the service has a CHANGELOG file, move it inside the service folder
136+
if [ -f ${sdk_services_backup_dir}/${service}/CHANGELOG.md ]; then
137+
echo "Found ${service} \"CHANGELOG\" file"
138+
cp -r ${sdk_services_backup_dir}/${service}/CHANGELOG.md ${SERVICES_FOLDER}/${service}/CHANGELOG.md
139+
fi
140+
141+
# If the service has a LICENSE file, move it inside the service folder
142+
if [ -f ${sdk_services_backup_dir}/${service}/LICENSE.md ]; then
143+
echo "Found ${service} \"LICENSE\" file"
144+
cp -r ${sdk_services_backup_dir}/${service}/LICENSE.md ${SERVICES_FOLDER}/${service}/LICENSE.md
145+
fi
146+
147+
# If the service has a NOTICE file, move it inside the service folder
148+
if [ -f ${sdk_services_backup_dir}/${service}/NOTICE.txt ]; then
149+
echo "Found ${service} \"NOTICE\" file"
150+
cp -r ${sdk_services_backup_dir}/${service}/NOTICE.txt ${SERVICES_FOLDER}/${service}/NOTICE.txt
151+
fi
152+
153+
# If the service has a VERSION file, move it inside the service folder
154+
if [ -f ${sdk_services_backup_dir}/${service}/VERSION ]; then
155+
echo "Found ${service} \"VERSION\" file"
156+
cp -r ${sdk_services_backup_dir}/${service}/VERSION ${SERVICES_FOLDER}/${service}/VERSION
157+
fi
158+
159+
done
160+
}

templates/java/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Java templates
2+
3+
This folder contains only our customized Java templates. Beside these customized templates,
4+
the original templates of openapi-generator for Java are used. These can be found in the
5+
official GitHub repo of the [openapi-generator](https://github.com/OpenAPITools/openapi-generator/tree/v7.14.0/modules/openapi-generator/src/main/resources/Java).
6+
7+
If you need to change something in the Java Generator, try always first to add
8+
[user-defined templates](https://openapi-generator.tech/docs/customization#user-defined-templates),
9+
instead of overwriting existing templates. These ensure an easier upgrade process, to newer
10+
versions of the openapi-generator.
11+
12+
If it's required to customize the original templates, you can copy them into this directory.
13+
Try to minimize the customization as much as possible, to ensure, that we can easily upgrade
14+
to newer versions in the future.

0 commit comments

Comments
 (0)