Skip to content

Commit

Permalink
Use forked openapi generator repo (#4343)
Browse files Browse the repository at this point in the history
  • Loading branch information
bisgaard-itis authored Jun 13, 2023
1 parent b7f2df6 commit 63b5ef7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1,183 deletions.
87 changes: 51 additions & 36 deletions scripts/openapi-generator.bash
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/bin/bash
# Script for calling OpenAPI Generato (generate clients, servers, and documentation from OpenAPI 2.0/3.x documents) or
# fetch the associated templates for Python client generation. Which function is called is determined by the 'RUN_FCN' environment variable.
# Setting 'RUN_FCN'="OPENAPI_GENERATOR_CLI" calls the generator and setting 'RUN_FCN'="FETCH_TEMPLATES" fetches the python client templates
# Script for calling OpenAPI Generator (generate clients, servers, and documentation from OpenAPI 2.0/3.x documents).

# OpenAPI Generator: generate clients, servers, and documentation from OpenAPI 2.0/3.x documents
#
Expand All @@ -24,52 +22,69 @@
# https://hub.docker.com/r/openapitools/openapi-generator-cli
#

# fetch_openapi_generator_templates
# usage: fetch_openapi_generator_templates <dir to place the templates in>

OPENAPI_GENERATOR_VERSION=v4.2.3

openapi_generator_cli(){

fetch_openapi_generator_templates(){
CURDIR=$(pwd)
TMPDIR=$1
cd ${TMPDIR}

echo "Cloning openapi-generator into ${TMPDIR} to get templates..."
git clone [email protected]:ITISFoundation/openapi-generator.git
cd openapi-generator
git checkout openapi-generator-${OPENAPI_GENERATOR_VERSION}
git status
git pull
echo "Done fetching templates..."

cd ${CURDIR}
}


openapi_generator_cli_generate(){
TMPDIR=$(mktemp -d)
USERID=$(stat --format=%u "$PWD")
GROUPID=$(stat --format=%g "$PWD")
HOST_TEMPL_DIR=${TMPDIR}/openapi-generator/modules/openapi-generator/src/main/resources/python
CONTAINER_TEMPL_DIR=/tmp/openapi_templates

# to mount a directory (e.g. to find custom templates, define the DOCKER_MOUNT environment variable)
docker_mount=""
if [ -v DOCKER_MOUNT ]; then
docker_mount="-v ${DOCKER_MOUNT}"
fetch_openapi_generator_templates "${TMPDIR}"
if [ ! -d "${HOST_TEMPL_DIR}" ]; then
echo "Templates could not be correctly fetched from Github"
exit 1
fi

(
exec docker run --rm \
-v ${HOST_TEMPL_DIR}:${CONTAINER_TEMPL_DIR} \
--user "$USERID:$GROUPID" \
--volume "$PWD:/local" \
openapitools/openapi-generator-cli:${OPENAPI_GENERATOR_VERSION} "$@" \
--template-dir=${CONTAINER_TEMPL_DIR}
)

rm -fr "${TMPDIR}"
}

openapi_generator_cli(){
USERID=$(stat --format=%u "$PWD")
GROUPID=$(stat --format=%g "$PWD")

exec docker run --rm \
${docker_mount} \
--user "$USERID:$GROUPID" \
--volume "$PWD:/local" \
openapitools/openapi-generator-cli:${OPENAPI_GENERATOR_VERSION} "$@"
}

fetch_openapi_generator_templates(){
TEMPLATE_DIR=$1
TMPDIR=$(mktemp -d)

echo "Fetching openapi generator templates from github..."
wget -O ${TMPDIR}/openapi_zip.zip "https://github.com/OpenAPITools/openapi-generator/archive/refs/tags/${OPENAPI_GENERATOR_VERSION}.zip"
unzip -q ${TMPDIR}/openapi_zip.zip -d ${TMPDIR}

echo "Overwriting templates in this repo..."
rm -r ${TEMPLATE_DIR}
mkdir ${TEMPLATE_DIR}
cd ${TMPDIR}
openapi_dir=$(ls . | grep openapi-generator)
cd ${openapi_dir}
cp -r ./modules/openapi-generator/src/main/resources/python/* ${TEMPLATE_DIR}

rm -r ${TMPDIR}
echo "Done!"
}
generate=false
if [[ $1 == "generate" ]]; then
echo "Found generate input. Will fetch templates from [email protected]:ITISFoundation/openapi-generator.git"
generate=true
fi

if [[ "${RUN_FCN}" == "OPENAPI_GENERATOR_CLI" ]]; then
openapi_generator_cli "$@"
elif [[ "${RUN_FCN}" == "FETCH_TEMPLATES" ]]; then
fetch_openapi_generator_templates "$@"
if ${generate}; then
openapi_generator_cli_generate "$@"
else
echo "The environment variable 'RUN_FCN' must be defined when calling this bash script"
openapi_generator_cli "$@"
fi
16 changes: 1 addition & 15 deletions services/api-server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ run-fake-devel: # starts a fake server in a dev-container


# BUILD ###########################################################################
OPENAPI_GENERATOR_VERSION := v4.2.3

define create_and_validate_openapi
# generating openapi specs file under $< (NOTE: Skips DEV FEATURES since this OAS is the 'offically released'!)
@source .env; \
Expand All @@ -95,7 +93,6 @@ define create_and_validate_openapi
@set -o allexport; \
source .env; \
cd $(CURDIR); \
export RUN_FCN=OPENAPI_GENERATOR_CLI; \
$(SCRIPTS_DIR)/openapi-generator.bash validate --input-spec /local/$@
endef

Expand Down Expand Up @@ -154,8 +151,6 @@ python-client: client openapi.json ## runs python client generator
# generates
@source .env; \
cd $(CURDIR); \
export RUN_FCN=OPENAPI_GENERATOR_CLI; \
export DOCKER_MOUNT=$(REPO_BASE_DIR)/services/api-server/openapi/python_templates:$(CONTAINER_TEMPL_DIR); \
$(SCRIPTS_DIR)/openapi-generator.bash generate \
--generator-name=$(GENERATOR_NAME) \
--git-user-id=$(GIT_USER_ID) \
Expand All @@ -165,19 +160,10 @@ python-client: client openapi.json ## runs python client generator
--output=/local/client \
--additional-properties=$(subst $(space),$(comma),$(strip $(ADDITIONAL_PROPS))) \
--package-name=osparc \
--release-note="Updated to $(APP_VERSION)" \
--template-dir=$(CONTAINER_TEMPL_DIR)

--release-note="Updated to $(APP_VERSION)"

generator-help: ## help on client-api generator
# generate help
@$(SCRIPTS_DIR)/openapi-generator-cli.bash help generate
# generator config help
@$(SCRIPTS_DIR)/openapi-generator-cli.bash config-help -g $(GENERATOR_NAME)


fetch-openapi-templates: ## fetches open api generator templates and overwrites the ones in $(CURDIR)/openapi/python-templates
@cd $(CURDIR); \
export RUN_FCN=FETCH_TEMPLATES; \
export DOCKER_MOUNT=$(REPO_BASE_DIR)/services/api-server/openapi/python_templates:$(CONTAINER_TEMPL_DIR); \
$(SCRIPTS_DIR)/openapi-generator.bash $(CURDIR)/openapi/python_templates
Loading

0 comments on commit 63b5ef7

Please sign in to comment.