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
15 changes: 15 additions & 0 deletions api_deploy/schema.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
from typing import Dict
import yaml
import re

yaml.Dumper.ignore_aliases = lambda *args: True


# pyyaml interprents strings which are octals as int so we're overriding this behaviour
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 default behavior for all other strings
return yaml.Dumper.represent_str(self, data)


# Attach custom representer to tye 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 Expand Up @@ -34,4 +48,5 @@ 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)
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