diff --git a/api_deploy/schema.py b/api_deploy/schema.py index 1f73623..9f980b2 100644 --- a/api_deploy/schema.py +++ b/api_deploy/schema.py @@ -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 {} @@ -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) diff --git a/tests/openapi/complex_source.yml b/tests/openapi/complex_source.yml index e8edff7..6eb831d 100644 --- a/tests/openapi/complex_source.yml +++ b/tests/openapi/complex_source.yml @@ -403,6 +403,13 @@ components: enum: - draft - submitted + offerCode: + type: string + readOnly: true + enum: + - "0116" + - "0118" + - "0119" stateTransitions: $ref: "#/components/schemas/StateTransitionList" _meta: diff --git a/tests/openapi/complex_target.yml b/tests/openapi/complex_target.yml index 439c1f5..d70e421 100644 --- a/tests/openapi/complex_target.yml +++ b/tests/openapi/complex_target.yml @@ -1252,6 +1252,7 @@ components: - attachments - comment - state + - offerCode - stateTransitions - _meta - _links @@ -1307,6 +1308,13 @@ components: enum: - draft - submitted + offerCode: + type: string + readOnly: true + enum: + - "0116" + - "0118" + - "0119" stateTransitions: type: array readOnly: true @@ -1704,6 +1712,7 @@ components: - attachments - comment - state + - offerCode - stateTransitions - _meta - _links @@ -1759,6 +1768,13 @@ components: enum: - draft - submitted + offerCode: + type: string + readOnly: true + enum: + - "0116" + - "0118" + - "0119" stateTransitions: type: array readOnly: true