From 7a362f9fa883b10063cfd35bbded7748be306e34 Mon Sep 17 00:00:00 2001 From: Matt Storey Date: Thu, 19 Sep 2024 14:58:17 +0100 Subject: [PATCH] CTOOLS-373: Fixed circular reference issue in Access --- generate/fix-imports-for-access.sh | 36 ++++++++++++++++++++++++++++ generate/templates/api_test.mustache | 17 +++++++++++-- justfile | 16 ++++++++++++- 3 files changed, 66 insertions(+), 3 deletions(-) create mode 100755 generate/fix-imports-for-access.sh diff --git a/generate/fix-imports-for-access.sh b/generate/fix-imports-for-access.sh new file mode 100755 index 0000000..0061275 --- /dev/null +++ b/generate/fix-imports-for-access.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +set -eETuo pipefail + +failure() { + local lineno="$1" + local msg="$2" + echo "Failed at $lineno: $msg" +} +trap 'failure ${LINENO} "$BASH_COMMAND"' ERR + +file_path="$1" +import_statement="$2" +update_forward_refs="$3" + +echo "$file_path" +echo "Fixing imports for Access" + + +# Check if the import statement is already present +if ! grep -Fxq "$import_statement" "$file_path"; then + # Append the import statement to the bottom of the file + echo "$import_statement" >> "$file_path" + echo "Import statement added to $file_path" +else + echo "Import statement is already present in $file_path" +fi + +# Append the additional line if it's not already present +if ! grep -Fxq "$update_forward_refs" "$file_path"; then + # Ensure the file ends with a new line, then append the additional line at the bottom + echo -e "\n$update_forward_refs" >> "$file_path" + echo "Import statement added to $file_path" +else + echo "Import statement is already present in $file_path" +fi \ No newline at end of file diff --git a/generate/templates/api_test.mustache b/generate/templates/api_test.mustache index 43d9a76..f2dceaf 100644 --- a/generate/templates/api_test.mustache +++ b/generate/templates/api_test.mustache @@ -2,7 +2,7 @@ {{>partial_header}} -from typing import List +from typing import List, Dict import unittest import json from xeger import Xeger @@ -55,7 +55,19 @@ class {{#operations}}Test{{classname}}(unittest.IsolatedAsyncioTestCase): count = 0 while True: count += 1 + regex = r"{{{vendorExtensions.x-regex}}}" + # The regex map is required due to a limitation of Xeger library where it fails + # correctly generate the string from the regex + regex_map = { + r'^(?=.*[a-zA-Z])[\w][\w +-]{2,100}$': "official-portfolios-read-only", + r'^[a-zA-Z0-9\\+/]*={0,3}$': "L==" + } + {{paramName}}: {{{dataType}}} = xegerObj.xeger(r"{{{vendorExtensions.x-regex}}}") + + if regex in regex_map: + {{paramName}} = regex_map[regex] + if len({{paramName}}) >= minLength: break if count > 100: @@ -93,7 +105,8 @@ class {{#operations}}Test{{classname}}(unittest.IsolatedAsyncioTestCase): {{paramName}}: {{dataType}} = example["value"] numExamples = len(examples) - {{paramName}} = {{dataType}}.from_dict({{paramName}}) + if hasattr({{dataType}}, 'from_dict') and callable(getattr({{dataType}}, 'from_dict')): + {{paramName}} = {{dataType}}.from_dict({{paramName}}) {{/content}} {{^content}} diff --git a/justfile b/justfile index fde9d35..d1b82e9 100644 --- a/justfile +++ b/justfile @@ -56,7 +56,6 @@ generate-local FLAG="": finbourne/lusid-sdk-gen-python:latest -- ./generate/generate.sh ./generate ./generate/.output /tmp/swagger.json .config.json rm -f generate/.output/.openapi-generator-ignore rm generate/templates/description.mustache - # split the README into two, and move one up a level bash generate/split-readme.sh @@ -64,6 +63,10 @@ generate-local FLAG="": # caused by a bug in the python generator if [ "{{APPLICATION_NAME}}" = "notifications" ] || [ "{{APPLICATION_NAME}}" = "workflow" ]; then just make-fix-for-one-of; fi + echo "Application name: $APPLICATION_NAME" + + if [ "{{APPLICATION_NAME}}" = "access" ]; then just make-import-fix; fi + add-tests: mkdir -p {{justfile_directory()}}/generate/.output/sdk/test/ rm -rf {{justfile_directory()}}/generate/.output/sdk/test/* @@ -150,6 +153,8 @@ generate-cicd TARGET_DIR FLAG="": # caused by a bug in the python generator if [ "{{APPLICATION_NAME}}" = "notifications" ] || [ "{{APPLICATION_NAME}}" = "workflow" ]; then just make-fix-for-one-of; fi + if [ "{{APPLICATION_NAME}}" = "access" ]; then just make-import-fix; fi + # need to remove the created content before copying over the top of it. # this prevents deleted content from hanging around indefinitely. rm -rf {{TARGET_DIR}}/sdk/${PACKAGE_NAME} @@ -203,3 +208,12 @@ generate-and-publish-cicd OUT_DIR FLAG="": make-fix-for-one-of: bash {{justfile_directory()}}/generate/fix-files-for-one-of.sh {{justfile_directory()}} ${PACKAGE_NAME} ${APPLICATION_NAME} +make-import-fix: + bash {{justfile_directory()}}/generate/fix-imports-for-access.sh \ + {{justfile_directory()}}/generate/.output/sdk/finbourne_access/models/policy_selector_definition.py \ + "from finbourne_access.models.selector_definition import SelectorDefinition" \ + "PolicySelectorDefinition.update_forward_refs()" + bash {{justfile_directory()}}/generate/fix-imports-for-access.sh \ + {{justfile_directory()}}/generate/.output/sdk/finbourne_access/models/selector_definition.py \ + "from finbourne_access.models.policy_selector_definition import PolicySelectorDefinition" \ + "SelectorDefinition.update_forward_refs()" \ No newline at end of file