Skip to content

Commit

Permalink
Make schema validation optional
Browse files Browse the repository at this point in the history
There are a lot of issues, so we're only partially validating the spec
now. We still validate with prance, but skip checking x-api-model
because there are so many legacy issues.

I looked at adding the x-api-model ... but wow, we haven't been adding
that for a long time. And there are open issues about it:
#3575
#3788

So, we just make it possible, but optional, to run the schema validation.
  • Loading branch information
cognifloyd committed Aug 12, 2022
1 parent 94ddd21 commit 77af9d0
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions st2common/st2common/cmd/validate_api_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@
)
)

# When disabled, only load the spec in prance to validate. Otherwise check for x-api-model as well.
# validate-defs is disabled by default until these are resolved:
# https://github.com/StackStorm/st2/issues/3575
# https://github.com/StackStorm/st2/issues/3788
cfg.CONF.register_cli_opt(
cfg.BoolOpt("validate-defs", short="-d", required=False, default=False)
)

cfg.CONF.register_cli_opt(
cfg.BoolOpt("generate", short="-c", required=False, default=False)
)
Expand All @@ -71,6 +79,7 @@ def _validate_definitions(spec):

if verbose:
LOG.info("Supplied definition for model %s: \n\n%s.", model, definition)
msg += "\n"

error = True
LOG.error(msg)
Expand All @@ -81,6 +90,7 @@ def _validate_definitions(spec):
def validate_spec():
spec_file = cfg.CONF.spec_file
generate_spec = cfg.CONF.generate
validate_defs = cfg.CONF.validate_defs

if not os.path.exists(spec_file) and not generate_spec:
msg = (
Expand All @@ -103,10 +113,13 @@ def validate_spec():
parser = prance.ResolvingParser(spec_file)
spec = parser.specification

if not validate_defs:
return True

return _validate_definitions(spec)


def teartown():
def teardown():
common_teardown()


Expand All @@ -119,11 +132,13 @@ def main():
spec_loader.load_spec("st2common", "openapi.yaml.j2")

# run the schema through prance to validate openapi spec.
ret = validate_spec()
passed = validate_spec()

ret = 0 if passed else 1
except Exception:
LOG.error("Failed to validate openapi.yaml file", exc_info=True)
ret = 1
finally:
teartown()
teardown()

return ret

0 comments on commit 77af9d0

Please sign in to comment.