22
22
from __future__ import absolute_import
23
23
import os
24
24
25
+ import prance
25
26
from oslo_config import cfg
26
- from prance import ResolvingParser
27
27
28
28
from st2common import config
29
29
from st2common import log as logging
45
45
)
46
46
)
47
47
48
+ # When disabled, only load the spec in prance to validate. Otherwise check for x-api-model as well.
49
+ # validate-defs is disabled by default until these are resolved:
50
+ # https://github.com/StackStorm/st2/issues/3575
51
+ # https://github.com/StackStorm/st2/issues/3788
52
+ cfg .CONF .register_cli_opt (
53
+ cfg .BoolOpt ("validate-defs" , short = "-d" , required = False , default = False )
54
+ )
55
+
48
56
cfg .CONF .register_cli_opt (
49
57
cfg .BoolOpt ("generate" , short = "-c" , required = False , default = False )
50
58
)
@@ -71,6 +79,7 @@ def _validate_definitions(spec):
71
79
72
80
if verbose :
73
81
LOG .info ("Supplied definition for model %s: \n \n %s." , model , definition )
82
+ msg += "\n "
74
83
75
84
error = True
76
85
LOG .error (msg )
@@ -81,6 +90,7 @@ def _validate_definitions(spec):
81
90
def validate_spec ():
82
91
spec_file = cfg .CONF .spec_file
83
92
generate_spec = cfg .CONF .generate
93
+ validate_defs = cfg .CONF .validate_defs
84
94
85
95
if not os .path .exists (spec_file ) and not generate_spec :
86
96
msg = (
@@ -100,13 +110,16 @@ def validate_spec():
100
110
f .write (spec_string )
101
111
f .flush ()
102
112
103
- parser = ResolvingParser (spec_file )
113
+ parser = prance . ResolvingParser (spec_file )
104
114
spec = parser .specification
105
115
116
+ if not validate_defs :
117
+ return True
118
+
106
119
return _validate_definitions (spec )
107
120
108
121
109
- def teartown ():
122
+ def teardown ():
110
123
common_teardown ()
111
124
112
125
@@ -118,11 +131,14 @@ def main():
118
131
# The spec loader do not allow duplicate keys.
119
132
spec_loader .load_spec ("st2common" , "openapi.yaml.j2" )
120
133
121
- ret = 0
134
+ # run the schema through prance to validate openapi spec.
135
+ passed = validate_spec ()
136
+
137
+ ret = 0 if passed else 1
122
138
except Exception :
123
139
LOG .error ("Failed to validate openapi.yaml file" , exc_info = True )
124
140
ret = 1
125
141
finally :
126
- teartown ()
142
+ teardown ()
127
143
128
144
return ret
0 commit comments