Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions polyapi/schema.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import contextlib
from typing import Dict
from jsonschema_gentypes.cli import process_config
Expand Down Expand Up @@ -31,6 +32,21 @@ def _temp_store_input_data(input_data: Dict) -> str:
return temp_file.name


def wrapped_generate_schema_types(type_spec: dict, root, fallback_type):
if not root:
root = "MyList" if fallback_type == "List" else "MyDict"

try:
return clean_title(root), generate_schema_types(type_spec, root=root)
except RecursionError:
# some schemas are so huge, our library cant handle it
# TODO identify critical recursion penalty and maybe switch underlying logic to iterative?
return fallback_type, ""
except:
logging.exception(f"Error when generating schema type: {type_spec}")
return fallback_type, ""


def generate_schema_types(input_data: Dict, root=None):
"""takes in a Dict representing a schema as input then appends the resulting python code to the output file"""
_cleanup_input_for_gentypes(input_data)
Expand Down
24 changes: 5 additions & 19 deletions polyapi/utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import re
import os
import logging
from typing import Tuple, List
from colorama import Fore, Style
from polyapi.constants import BASIC_PYTHON_TYPES
from polyapi.typedefs import PropertySpecification, PropertyType
from polyapi.schema import generate_schema_types, clean_title, map_primitive_types
from polyapi.schema import wrapped_generate_schema_types, clean_title, map_primitive_types


# this string should be in every __init__ file.
# it contains all the imports needed for the function or variable code to run
CODE_IMPORTS = "from typing import List, Dict, Any, TypedDict, Optional, Callable\nimport logging\nimport requests\nimport socketio # type: ignore\nfrom polyapi.config import get_api_key_and_url\nfrom polyapi.execute import execute, execute_post, variable_get, variable_update\n\n"
FALLBACK_TYPES = {"Dict", "List"}


def init_the_init(full_path: str) -> None:
Expand Down Expand Up @@ -96,11 +96,7 @@ def get_type_and_def(type_spec: PropertyType) -> Tuple[str, str]:
if type_spec.get("items"):
items = type_spec["items"]
if items.get("$ref"):
try:
return "ResponseType", generate_schema_types(type_spec, root="ResponseType") # type: ignore
except:
logging.exception(f"Error when generating schema type: {type_spec}")
return "Dict", ""
return wrapped_generate_schema_types(type_spec, "ResponseType", "Dict") # type: ignore
else:
item_type, _ = get_type_and_def(items)
title = f"List[{item_type}]"
Expand All @@ -116,12 +112,7 @@ def get_type_and_def(type_spec: PropertyType) -> Tuple[str, str]:
title = schema.get("title", "")
if title:
assert isinstance(title, str)
title = clean_title(title)
try:
return title, generate_schema_types(schema, root=title) # type: ignore
except:
logging.exception(f"Error when generating schema type: {schema}")
return "Dict", ""
return wrapped_generate_schema_types(schema, title, "Dict") # type: ignore

elif schema.get("items"):
# fallback to schema $ref name if no explicit title
Expand All @@ -132,16 +123,11 @@ def get_type_and_def(type_spec: PropertyType) -> Tuple[str, str]:
title = items.get("$ref", "") # type: ignore

title = title.rsplit("/", 1)[-1]
title = clean_title(title)
if not title:
return "List", ""

title = f"List[{title}]"
try:
return title, generate_schema_types(schema, root=title)
except:
logging.exception(f"Error when generating schema type: {schema}")
return "List", ""
return wrapped_generate_schema_types(schema, title, "List")
else:
return "Any", ""
else:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["setuptools>=61.2", "wheel"]

[project]
name = "polyapi-python"
version = "0.2.5.dev8"
version = "0.2.5.dev9"
description = "The Python Client for PolyAPI, the IPaaS by Developers for Developers"
authors = [{ name = "Dan Fellin", email = "dan@polyapi.io" }]
dependencies = [
Expand Down
24 changes: 15 additions & 9 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import unittest
from polyapi.schema import generate_schema_types
from polyapi.schema import wrapped_generate_schema_types

SCHEMA = {
"$schema": "http://json-schema.org/draft-06/schema#",
"type": "object",
"properties": {"name": {"type": "string"}},
"required": ["name"],
"additionalProperties": False,
"definitions": {},
}


class T(unittest.TestCase):
def test_fix_titles(self):
# schema = json.loads(SCHEMA)
schema = {"$schema": "http://json-schema.org/draft-06/schema#"}
try:
a, b = generate_schema_types(schema)
except AssertionError:
pass

# should not throw with unknown dialect error
output = wrapped_generate_schema_types(SCHEMA, "", "Dict")
self.assertEqual("MyDict", output[0])
self.assertIn("class MyDict", output[1])

# should not throw with unknown dialect error