From 697993f11f58ee7abd52edbf9e96c3d30853a597 Mon Sep 17 00:00:00 2001 From: Matthias Dellweg Date: Tue, 4 Jun 2024 17:06:01 +0200 Subject: [PATCH] Rework fixing api spec This also allows to consume the *api.json from anywhere, as it's processed into patched-api.json prior to be used in the container. [noissue] --- .github/workflows/ci.yml | 8 +----- .gitignore | 8 +++--- gen-client.sh | 19 ++++++++++--- remove-cookie-auth.py | 58 ---------------------------------------- 4 files changed, 20 insertions(+), 73 deletions(-) delete mode 100644 remove-cookie-auth.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 18270ce..0c9cc2b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,9 +1,3 @@ -# WARNING: DO NOT EDIT! -# -# This file was generated by plugin_template, and is managed by it. Please use -# './plugin-template --github pulp_file' to update this file. -# -# For more info visit https://github.com/pulp/plugin_template --- name: pulp-openapi-generator PR CI on: pull_request @@ -66,7 +60,7 @@ jobs: run: | echo ::group::HTTPIE sudo apt-get update -yq - sudo -E apt-get -yq --no-install-suggests --no-install-recommends install httpie + sudo -E apt-get -yq --no-install-suggests --no-install-recommends install httpie jq echo ::endgroup:: echo "HTTPIE_CONFIG_DIR=$GITHUB_WORKSPACE/.ci/assets/httpie/" >> $GITHUB_ENV diff --git a/.gitignore b/.gitignore index 5be0c53..2ad937d 100644 --- a/.gitignore +++ b/.gitignore @@ -63,8 +63,8 @@ target/ #Ipython Notebook .ipynb_checkpoints -# written in generate.sh -.openapi-generator-ignore - -# generated client packages +# generated stuff +/api.json +/patched-api.json /*-client/ +.openapi-generator-ignore diff --git a/gen-client.sh b/gen-client.sh index 6e4cd72..97e63d8 100755 --- a/gen-client.sh +++ b/gen-client.sh @@ -61,15 +61,23 @@ else VOLUME_DIR="${PWD}" fi +REMOVE_COOKIE_AUTH_FILTER='del(.paths[][].security|select(.)[]|select(.cookieAuth))|del(.components.securitySchemes.cookieAuth)' + +# These two may be needed when upgrading the generator image +FIX_TASK_CREATED_RESOURCES_FILTER='(.components.schemas.TaskResponse|select(.)|.properties.created_resources.items) |= {"$oneOf":[{type:"null"},.]}' +FIX_TASK_ERROR_FILTER='(.components.schemas.TaskResponse|select(.)|.properties.error) |= (del(.readOnly) | .additionalProperties.type = "string")' + if [ "$LANGUAGE" = "python" ] then + cat "${API_SPEC}" | jq "." > patched-api.json + $CONTAINER_EXEC run \ "${ULIMIT_COMMAND[@]}" \ "${USER_COMMAND[@]}" \ --rm \ "${VOLUME_OPTION[@]}" \ "$OPENAPI_PYTHON_IMAGE" generate \ - -i "${VOLUME_DIR}/${API_SPEC}" \ + -i "${VOLUME_DIR}/patched-api.json" \ -g python \ -o "${VOLUME_DIR}/${PACKAGE}-client" \ "--additional-properties=packageName=pulpcore.client.${PACKAGE},projectName=${PACKAGE}-client,packageVersion=${VERSION},domainEnabled=${DOMAIN_ENABLED}" \ @@ -86,14 +94,15 @@ then mkdir -p "${PACKAGE}-client" echo git_push.sh > "${PACKAGE}-client/.openapi-generator-ignore" - python3 remove-cookie-auth.py + cat "${API_SPEC}" | jq "${REMOVE_COOKIE_AUTH_FILTER}" > patched-api.json + $CONTAINER_EXEC run \ "${ULIMIT_COMMAND[@]}" \ "${USER_COMMAND[@]}" \ --rm \ "${VOLUME_OPTION[@]}" \ "$OPENAPI_RUBY_IMAGE" generate \ - -i "${VOLUME_DIR}/${API_SPEC}" \ + -i "${VOLUME_DIR}/patched-api.json" \ -g ruby \ -o "${VOLUME_DIR}/${PACKAGE}-client" \ "--additional-properties=gemName=${PACKAGE}_client,gemLicense="GPLv2+",gemVersion=${VERSION},gemHomepage=https://github.com/pulp/${PACKAGE}" \ @@ -105,13 +114,15 @@ fi if [ "$LANGUAGE" = "typescript" ] then + cat "${API_SPEC}" | jq "." > patched-api.json + $CONTAINER_EXEC run \ "${ULIMIT_COMMAND[@]}" \ "${USER_COMMAND[@]}" \ --rm \ "${VOLUME_OPTION[@]}" \ "$OPENAPI_TYPESCRIPT_IMAGE" generate \ - -i "${VOLUME_DIR}/${API_SPEC}" \ + -i "${VOLUME_DIR}/patched-api.json" \ -g typescript-axios \ -o "${VOLUME_DIR}/${PACKAGE}-client" \ -t "${VOLUME_DIR}/templates/typescript-axios" \ diff --git a/remove-cookie-auth.py b/remove-cookie-auth.py deleted file mode 100644 index a6dd112..0000000 --- a/remove-cookie-auth.py +++ /dev/null @@ -1,58 +0,0 @@ -import json - - -def recursive_remove(json_dict): - """ - Recursively search through all of the dictionary and removes any key that is 'cookieAuth' - """ - for key in list(json_dict): - if key == 'cookieAuth': - json_dict.pop(key) - else: - value = json_dict[key] - if isinstance(value, dict): - # Search this dictionary as well - recursive_remove(value) - elif isinstance(value, list): - recursive_list_search(value) - - -def recursive_list_search(json_list): - """ - Recursively search through a passed in list till you hit a dictionary to pass into recursive_remove - Any other objects are don't cares - """ - for i in reversed(range(len(json_list))): - value = json_list[i] - if isinstance(value, dict): - recursive_remove(value) - # Delete the dictionary if it is empty - if not value: - del json_list[i] - elif isinstance(value, list): - recursive_list_search(value) - - -# Open the JSON file -api_file = open('api.json', 'r') - -# Load the api JSON object into dictionary -api_json = json.load(api_file) - -# Close the file -api_file.close() - -# Remove 'cookieAuth' from the schema -recursive_remove(api_json) - -# Serialize the json -new_api_json_object = json.dumps(api_json) - -# Reopen the file in write mode -api_file = open('api.json', 'w') - -# Re-write the api.json file -api_file.write(new_api_json_object) - -# Close the file -api_file.close()