lunchable icon indicating copy to clipboard operation
lunchable copied to clipboard

Developer API v2 - Dynamically Generate Client

Open juftin opened this issue 1 year ago • 3 comments

Summary

LunchMoney Developer API v2 is incoming with an OpenAPI Spec

juftin avatar Dec 12 '24 02:12 juftin

Script

#!/usr/bin/env bash

set -e

OPENAPI_URL="https://lm-v2-api-mock-data-xxxxxxxxxxx.herokuapp.com/v2/openapi/"
#OPENAPI_URL="https://lm-v2-api-v1-proxy-xxxxxxxxxxx.herokuapp.com/v2/openapi/"

GENERATOR_VERSION="7.10.0"

CONFIGURATION_JSON="$(cat <<EOF
[
  {
    "packageName": "lunchable",
    "language": "python",
    "installCommand": "poetry install",
    "testCommand": "poetry run pytest"
  }
]
EOF
)"

DEPENDENCIES=(
  "jq"
  "docker"
  "poetry"
)
for DEPENDENCY in ${DEPENDENCIES[@]}; do
  if ! command -v ${DEPENDENCY} &> /dev/null; then
    echo "${DEPENDENCY} is required but it's not installed. Aborting."
    exit 1
  fi
done

function bold() {
  echo -e "\033[1m${1}\033[0m"
}

THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
echo "${CONFIGURATION_JSON}" | jq -c '.[]' | while read -r CONFIG; do
  # PARSED CONFIGURATION
  PACKAGE_NAME="$(echo "${CONFIG}" | jq -r '.packageName')"
  LANGUAGE="$(echo "${CONFIG}" | jq -r '.language')"
  TEST_COMMAND="$(echo "${CONFIG}" | jq -r '.testCommand')"
  # GENERATE CLIENT
  bold "Generating client for ${LANGUAGE}..."
  bold "Package name: ${PACKAGE_NAME}"
  CLIENT_DIR="${THIS_DIR}/clients/${LANGUAGE}"
  rm -rf "${CLIENT_DIR}"
  mkdir -p "${CLIENT_DIR}"
  docker run --rm \
    -v "${CLIENT_DIR}:/local/${LANGUAGE}" \
    openapitools/openapi-generator-cli:v${GENERATOR_VERSION} \
    generate \
      -i "${OPENAPI_URL}" \
      -g ${LANGUAGE} \
      -o /local/${LANGUAGE} \
      --package-name ${PACKAGE_NAME} \
      --skip-validate-spec
  bold "Client generated successfully!"
  pushd "${CLIENT_DIR}"
  # INSTALL CLIENT
  bold "Installing client for ${LANGUAGE}..."
  eval "${INSTALL_COMMAND}"
  # TEST CLIENT
  bold "Testing client for ${LANGUAGE}..."
  eval "${TEST_COMMAND}"
  popd
done

juftin avatar Dec 12 '24 02:12 juftin

Exception in thread "main" org.openapitools.codegen.SpecValidationException: There were issues with the specification. The option can be disabled via validateSpec (Maven/Gradle) or --skip-validate-spec (CLI).
 | Error count: 1, Warning count: 0
Errors:
	-attribute components.schemas.transactionObject.items is missing
test/test_account_type_enum.py:18: in <module>
    from lunchable.models.account_type_enum import AccountTypeEnum
lunchable/__init__.py:21: in <module>
    from lunchable.api.categories_api import CategoriesApi
lunchable/api/__init__.py:4: in <module>
    from lunchable.api.categories_api import CategoriesApi
lunchable/api/categories_api.py:24: in <module>
    from lunchable.models.create_category_request_object import CreateCategoryRequestObject
E   ModuleNotFoundError: No module named 'lunchable.models.create_category_request_object'

juftin avatar Dec 12 '24 02:12 juftin

Latest Issues:

❯ npm run generate

> [email protected] generate
> openapi-generator-cli generate

[[python] openapi.yaml] [main] ERROR o.o.codegen.utils.ModelUtils - Undefined array inner type for `null`. Default to String.
[[python] openapi.yaml] [main] ERROR o.o.codegen.utils.ModelUtils - Undefined array inner type for `null`. Default to String.
[[python] openapi.yaml] [main] ERROR o.o.codegen.utils.ModelUtils - Undefined array inner type for `null`. Default to String.
[[python] openapi.yaml] [main] ERROR o.o.codegen.utils.ModelUtils - Undefined array inner type for `null`. Default to String.
Exception in thread "main" org.openapitools.codegen.SpecValidationException: There were issues with the specification. The option can be disabled via validateSpec (Maven/Gradle) or --skip-validate-spec (CLI).
[[python] openapi.yaml]  | Error count: 2, Warning count: 0
[[python] openapi.yaml] Errors:
[[python] openapi.yaml]         -attribute components.schemas.transactionObject.items is missing
[[python] openapi.yaml]         -attribute components.schemas.updateTransactionObject.items is missing
[[python] openapi.yaml]
[[python] openapi.yaml]         at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:717)
[[python] openapi.yaml]         at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:744)
[[python] openapi.yaml]         at org.openapitools.codegen.cmd.Generate.execute(Generate.java:527)
[[python] openapi.yaml]         at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
[[python] openapi.yaml]         at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
[[python] openapi.yaml] java -jar "/Users/juftin/git/lunchable/node_modules/@openapitools/openapi-generator-cli/versions/7.10.0.jar" generate --input-spec="openapi.yaml" --generator-name="python" --output="clients/python" --package-name="lunchable" exited with code 1
[python] openapi.yaml
  java -jar "/Users/juftin/git/lunchable/node_modules/@openapitools/openapi-generator-cli/versions/7.10.0.jar" generate --input-spec="openapi.yaml" --generator-name="python" --output="clients/python" --package-name="lunchable"

Code generation failed

juftin avatar Mar 01 '25 04:03 juftin