Skip to content

Commit

Permalink
Check deprecation_schema for valid JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
yashvardhannanavati committed Aug 5, 2024
1 parent ea3998a commit 84dcfad
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 17 deletions.
18 changes: 3 additions & 15 deletions iib/web/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from flask import current_app, url_for
from flask_login import UserMixin, current_user
from flask_sqlalchemy.model import DefaultMeta
import ruamel.yaml
import sqlalchemy
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.orm import joinedload, load_only, Mapped, validates
Expand Down Expand Up @@ -46,17 +45,6 @@
FbcOperationRequestResponse,
)

yaml = ruamel.yaml.YAML()
# IMPORTANT: ruamel will introduce a line break if the yaml line is longer than yaml.width.
# Unfortunately, this causes issues for JSON values nested within a YAML file, e.g.
# metadata.annotations."alm-examples" in a CSV file.
# The default value is 80. Set it to a more forgiving higher number to avoid issues
yaml.width = 200
# ruamel will also cause issues when normalizing a YAML object that contains
# a nested JSON object when it does not preserve quotes. Thus, it produces
# invalid YAML. Let's prevent this from happening at all.
yaml.preserve_quotes = True


class BaseEnum(Enum):
"""A base class for IIB enums."""
Expand Down Expand Up @@ -2336,9 +2324,9 @@ def from_json( # type: ignore[override] # noqa: F821
if not _deprecation_schema or not isinstance(_deprecation_schema, str):
raise ValidationError('"deprecation_schema" should be a non-empty string')
try:
yaml.load(_deprecation_schema)
except ruamel.yaml.YAMLError:
raise ValidationError('"deprecation_schema" string should be valid YAML')
json.loads(_deprecation_schema)
except ValueError:
raise ValidationError('"deprecation_schema" string should be valid JSON')

# cast to more wider type, see _from_json method
cls._from_json(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_web/test_api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2875,9 +2875,9 @@ def test_fbc_operations(
'from_index': 'pull:spec',
'binary_image': 'binary:image',
'operator_package': 'my-package',
'deprecation_schema': '{invalid_yaml',
'deprecation_schema': '{{{invalid_json',
},
'"deprecation_schema" string should be valid YAML',
'"deprecation_schema" string should be valid JSON',
),
),
)
Expand Down

0 comments on commit 84dcfad

Please sign in to comment.