in samtranslator/plugins/globals/globals.py [0:0]
def fix_openapi_definitions(cls, template: Dict[str, Any]) -> None:
"""
Helper method to postprocess the resources to make sure the swagger doc version matches
the one specified on the resource with flag OpenApiVersion.
This is done postprocess in globals because, the implicit api plugin runs before globals, \
and at that point the global flags aren't applied on each resource, so we do not know \
whether OpenApiVersion flag is specified. Running the globals plugin before implicit api \
was a risky change, so we decided to postprocess the openapi version here.
To make sure we don't modify customer defined swagger, we also check for __MANAGE_SWAGGER flag.
:param dict template: SAM template
"""
resources = template.get("Resources", {})
for _, resource in resources.items():
if ("Type" in resource) and (resource["Type"] == cls._API_TYPE):
properties = resource["Properties"]
if (
(cls._OPENAPIVERSION in properties)
and (cls._MANAGE_SWAGGER in properties)
and SwaggerEditor.safe_compare_regex_with_string(
SwaggerEditor._OPENAPI_VERSION_3_REGEX, properties[cls._OPENAPIVERSION]
)
):
if not isinstance(properties[cls._OPENAPIVERSION], str):
properties[cls._OPENAPIVERSION] = str(properties[cls._OPENAPIVERSION])
resource["Properties"] = properties
if "DefinitionBody" in properties:
definition_body = properties["DefinitionBody"]
definition_body["openapi"] = properties[cls._OPENAPIVERSION]
if definition_body.get("swagger"):
del definition_body["swagger"]