Skip to content

Commit 8f0e01a

Browse files
4655 p3 polyapi python schema errors lets fix (#63)
* Changed encoding to utf-8 and added unit test * changed version * Updated version
1 parent 7dc8423 commit 8f0e01a

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

polyapi/poly_schemas.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,20 @@ def add_schema_file(
121121
# Read current __init__.py content if it exists
122122
init_content = ""
123123
if os.path.exists(init_path):
124-
with open(init_path, "r") as f:
124+
with open(init_path, "r", encoding='utf-8') as f:
125125
init_content = f.read()
126126

127127
# Prepare new content to append to __init__.py
128128
new_init_content = init_content + f"\n\nfrom ._{schema_namespace} import {schema_name}\n__all__.append('{schema_name}')\n"
129129

130130
# Use temporary files for atomic writes
131131
# Write to __init__.py atomically
132-
with tempfile.NamedTemporaryFile(mode="w", delete=False, dir=full_path, suffix=".tmp") as temp_init:
132+
with tempfile.NamedTemporaryFile(mode="w", delete=False, dir=full_path, suffix=".tmp", encoding='utf-8') as temp_init:
133133
temp_init.write(new_init_content)
134134
temp_init_path = temp_init.name
135135

136136
# Write to schema file atomically
137-
with tempfile.NamedTemporaryFile(mode="w", delete=False, dir=full_path, suffix=".tmp") as temp_schema:
137+
with tempfile.NamedTemporaryFile(mode="w", delete=False, dir=full_path, suffix=".tmp", encoding='utf-8') as temp_schema:
138138
temp_schema.write(schema_defs)
139139
temp_schema_path = temp_schema.name
140140

@@ -205,7 +205,7 @@ def create_schema(
205205
def add_schema_to_init(full_path: str, spec: SchemaSpecDto):
206206
init_the_init(full_path, code_imports="")
207207
init_path = os.path.join(full_path, "__init__.py")
208-
with open(init_path, "a") as f:
208+
with open(init_path, "a", encoding='utf-8') as f:
209209
f.write(render_poly_schema(spec) + "\n\n")
210210

211211

polyapi/schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def generate_schema_types(input_data: Dict, root=None):
9393
with contextlib.redirect_stdout(None):
9494
process_config(config, [tmp_input])
9595

96-
with open(tmp_output) as f:
96+
with open(tmp_output, encoding='utf-8') as f:
9797
output = f.read()
9898

9999
output = clean_malformed_examples(output)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ requires = ["setuptools>=61.2", "wheel"]
33

44
[project]
55
name = "polyapi-python"
6-
version = "0.3.8"
6+
version = "0.3.9.dev1"
77
description = "The Python Client for PolyAPI, the IPaaS by Developers for Developers"
88
authors = [{ name = "Dan Fellin", email = "dan@polyapi.io" }]
99
dependencies = [

tests/test_schema.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import unittest
2-
from polyapi.schema import clean_malformed_examples, wrapped_generate_schema_types
2+
from polyapi.schema import clean_malformed_examples, wrapped_generate_schema_types, generate_schema_types
33

44
SCHEMA = {
55
"$schema": "http://json-schema.org/draft-06/schema#",
@@ -10,6 +10,14 @@
1010
"definitions": {},
1111
}
1212

13+
CHARACTER_SCHEMA = {
14+
"$schema": "http://json-schema.org/draft-06/schema#",
15+
"type": "object",
16+
"properties": {"CHARACTER_SCHEMA_NAME": {"description": "This is — “bad”, right?", "type": "string"}},
17+
"additionalProperties": False,
18+
"definitions": {},
19+
}
20+
1321
APALEO_MALFORMED_EXAMPLE = 'from typing import List, TypedDict, Union\nfrom typing_extensions import Required\n\n\n# Body.\n# \n# example: {\n "from": "2024-04-21",\n "to": "2024-04-24",\n "grossDailyRate": {\n "amount": 160.0,\n "currency": "EUR"\n },\n "timeSlices": [\n {\n "blockedUnits": 3\n },\n {\n "blockedUnits": 0\n },\n {\n "blockedUnits": 7\n }\n ]\n}\n# x-readme-ref-name: ReplaceBlockModel\nBody = TypedDict(\'Body\', {\n # Start date and time from which the inventory will be blockedSpecify either a pure date or a date and time (without fractional second part) in UTC or with UTC offset as defined in <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO8601:2004</a>\n # \n # Required property\n \'from\': Required[str],\n # End date and time until which the inventory will be blocked. Cannot be more than 5 years after the start date.Specify either a pure date or a date and time (without fractional second part) in UTC or with UTC offset as defined in <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO8601:2004</a>\n # \n # Required property\n \'to\': Required[str],\n # x-readme-ref-name: MonetaryValueModel\n # \n # Required property\n \'grossDailyRate\': Required["_BodygrossDailyRate"],\n # The list of time slices\n # \n # Required property\n \'timeSlices\': Required[List["_BodytimeSlicesitem"]],\n}, total=False)\n\n\nclass _BodygrossDailyRate(TypedDict, total=False):\n """ x-readme-ref-name: MonetaryValueModel """\n\n amount: Required[Union[int, float]]\n """\n format: double\n\n Required property\n """\n\n currency: Required[str]\n """ Required property """\n\n\n\nclass _BodytimeSlicesitem(TypedDict, total=False):\n """ x-readme-ref-name: CreateBlockTimeSliceModel """\n\n blockedUnits: Required[Union[int, float]]\n """\n Number of units blocked for the time slice\n\n format: int32\n\n Required property\n """\n\n'
1422

1523

@@ -23,4 +31,10 @@ def test_fix_titles(self):
2331

2432
def test_clean_malformed_examples(self):
2533
output = clean_malformed_examples(APALEO_MALFORMED_EXAMPLE)
26-
self.assertNotIn("# example: {", output)
34+
self.assertNotIn("# example: {", output)
35+
36+
def test_character_encoding(self):
37+
output = generate_schema_types(CHARACTER_SCHEMA, "Dict")
38+
expected = 'from typing import TypedDict\n\n\nclass Dict(TypedDict, total=False):\n CHARACTER_SCHEMA_NAME: str\n """ This is — “bad”, right? """\n\n'
39+
self.assertEqual(output, expected)
40+

0 commit comments

Comments
 (0)