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 api_deploy/schema.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
from typing import Dict
import yaml
import re

yaml.Dumper.ignore_aliases = lambda *args: True


# pyyaml interprets strings which are octals as int so we're overriding this behavior
def represent_str(self, data):
"""Custom string representer that forces quoting for strings with leading zeros"""
# Only quote strings that look like numbers with leading zeros (but not just "0")
if re.match(r'^0[0-9]+$', data):
return self.represent_scalar('tag:yaml.org,2002:str', data, style="'")

# Use the default behavior for all other strings
return yaml.Dumper.represent_str(self, data)


# Attach custom representer to the default YAML dumper
yaml.add_representer(str, represent_str)


class YamlDict(Dict):
def __init__(self, schema) -> None:
content = yaml.load(schema, yaml.Loader) or {}
Expand Down
12 changes: 12 additions & 0 deletions tests/functional/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def simple_target_file():
return Schema.from_file(filename)


@fixture
def simple_target_file_raw():
with open(os.path.join(dirname, '../openapi/simple_target.yml'), 'r') as target_file:
return target_file.read()


@fixture
def complex_source_file():
filename = os.path.join(dirname, '../openapi/complex_source.yml')
Expand Down Expand Up @@ -63,6 +69,12 @@ def test_compile_simple(simple_source_file, simple_target_file):
assert processed.dump(True) == simple_target_file.dump(True)


def test_compile_simple_raw(simple_source_file, simple_target_file_raw):
manager = ProcessManager.default(config)
processed = manager.process(simple_source_file)
assert processed.dump(True) == simple_target_file_raw


def test_compile_complex(complex_source_file, complex_target_file):
manager = ProcessManager.default(config)
processed = manager.process(complex_source_file)
Expand Down
2 changes: 2 additions & 0 deletions tests/openapi/simple_source.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ components:
- east
- south
- west
- "1190"
- "0119"

PingPayload:
type: object
Expand Down
Loading