Skip to content

Commit

Permalink
refactor: do not write out modified specs (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rahuljagwani committed Dec 17, 2023
1 parent a5ae046 commit da23709
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 38 deletions.
18 changes: 1 addition & 17 deletions foca/api/register_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import Dict, List

from connexion import App
import yaml

from foca.models.config import SpecConfig
from foca.config.config_parser import ConfigParser
Expand Down Expand Up @@ -86,26 +85,11 @@ def register_openapi(
operation_object.pop('security', None)
logger.debug("Removed security fields")

# Write modified specs
try:
with open(spec.path_out, 'w') as out_file: # type: ignore
yaml.safe_dump(spec_parsed, out_file)
except OSError as e:
raise OSError(
"Modified specification could not be written to file "
f"'{spec.path_out}'"
) from e
except yaml.YAMLError as e:
raise yaml.YAMLError(
"Could not encode modified specification"
) from e
logger.debug(f"Wrote specs to file: {spec.path_out}")

# Attach specs to connexion App
logger.debug(f"Modified specs: {spec_parsed}")
spec.connexion = {} if spec.connexion is None else spec.connexion
app.add_api(
specification=spec.path_out,
specification=spec_parsed,
**spec.dict().get('connexion', {}),
)
logger.info(f"API endpoints added from spec: {spec.path_out}")
Expand Down
21 changes: 0 additions & 21 deletions tests/api/test_register_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,27 +150,6 @@ def test_openapi_2_fragments(self):
res = register_openapi(app=app, specs=spec_configs)
assert isinstance(res, App)

def test_register_openapi_spec_cannot_serialize(self):
"""Registration failing because modified specs cannot be serialized."""
app = App(__name__)
spec_configs = [SpecConfig(
path=PATH_SPECS_2_YAML_ORIGINAL,
path_out=PATH_SPECS_2_YAML_MODIFIED,
add_operation_fields=OPERATION_FIELDS_2_NO_RESOLVE,
)]
with pytest.raises(YAMLError):
register_openapi(app=app, specs=spec_configs)

def test_register_openapi_spec_cannot_write(self):
"""Registration failing because modified specs cannot be written."""
app = App(__name__)
spec_configs = [SpecConfig(
path=PATH_SPECS_2_YAML_ORIGINAL,
path_out=PATH_NOT_FOUND,
)]
with pytest.raises(OSError):
register_openapi(app=app, specs=spec_configs)

def test_openapi_2_yaml_no_auth(self):
"""Successfully register OpenAPI 2 YAML specs with Connexion app;
no security definitions/fields.
Expand Down

0 comments on commit da23709

Please sign in to comment.