Skip to content
Closed
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
33 changes: 28 additions & 5 deletions api_deploy/schema.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
from typing import Dict
import yaml
import io
import ruyaml

yaml.Dumper.ignore_aliases = lambda *args: True
_yaml = ruyaml.YAML(typ='safe')
# avoid YAML aliases/anchors in output
_yaml.representer.ignore_aliases = lambda *args: True


def _dump_to_string(data, sort_keys=False):
# Optional key sorting to mimic previous behavior when requested
def _sort(obj):
if isinstance(obj, dict):
# sort by key, recursively
return {k: _sort(obj[k]) for k in sorted(obj.keys())}
if isinstance(obj, list):
return [
_sort(i) for i in obj
]
return obj

if sort_keys:
data = _sort(data)
stream = io.StringIO()
_yaml.dump(data, stream)
return stream.getvalue()


class YamlDict(Dict):
def __init__(self, schema) -> None:
content = yaml.load(schema, yaml.Loader) or {}
# ruyaml.YAML.load accepts str, bytes, and file-like objects
content = _yaml.load(schema) or {}
super().__init__(content)

@classmethod
Expand All @@ -19,7 +42,7 @@ def to_file(self, file_path):
target.write(self.dump())

def dump(self, sort_keys=False):
return yaml.dump(self, sort_keys=sort_keys)
return _dump_to_string(self, sort_keys=sort_keys)


class Schema(YamlDict):
Expand All @@ -34,4 +57,4 @@ def dump(self, sort_keys=False):
'x-amazon-apigateway-request-validators': self.get('x-amazon-apigateway-request-validators', {}),
'x-amazon-apigateway-minimum-compression-size': 0 ,
}
return yaml.dump(data, sort_keys=sort_keys)
return _dump_to_string(data, sort_keys=sort_keys)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def readme():
'click>=8.1.3, <9',
'mergedeep>=1.3.4',
'boto3>=1.26.118',
'PyYAML>=6.0',
'ruyaml==0.91',
'requests>=2.31.0',
'Jinja2==3.1.2',
'urllib3<2',
Expand Down
7 changes: 7 additions & 0 deletions tests/openapi/complex_source.yml
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,13 @@ components:
enum:
- draft
- submitted
offerCode:
type: string
readOnly: true
enum:
- "0116"
- "0118"
- "0119"
stateTransitions:
$ref: "#/components/schemas/StateTransitionList"
_meta:
Expand Down
16 changes: 16 additions & 0 deletions tests/openapi/complex_target.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,7 @@ components:
- attachments
- comment
- state
- offerCode
- stateTransitions
- _meta
- _links
Expand Down Expand Up @@ -1307,6 +1308,13 @@ components:
enum:
- draft
- submitted
offerCode:
type: string
readOnly: true
enum:
- "0116"
- "0118"
- "0119"
stateTransitions:
type: array
readOnly: true
Expand Down Expand Up @@ -1704,6 +1712,7 @@ components:
- attachments
- comment
- state
- offerCode
- stateTransitions
- _meta
- _links
Expand Down Expand Up @@ -1759,6 +1768,13 @@ components:
enum:
- draft
- submitted
offerCode:
type: string
readOnly: true
enum:
- "0116"
- "0118"
- "0119"
stateTransitions:
type: array
readOnly: true
Expand Down