1
- # pylint:disable=wildcard-import
2
- # pylint:disable=unused-import
3
1
# pylint:disable=unused-variable
4
2
# pylint:disable=unused-argument
5
3
# pylint:disable=redefined-outer-name
13
11
import yaml
14
12
from openapi_spec_validator import validate_spec
15
13
from openapi_spec_validator .exceptions import OpenAPIValidationError
16
-
17
- from utils import (dump_specs , is_json_schema , is_openapi_schema ,
18
- list_files_in_api_specs , load_specs , specs_folder )
14
+ from utils import (
15
+ dump_specs ,
16
+ is_json_schema ,
17
+ is_openapi_schema ,
18
+ list_files_in_api_specs ,
19
+ load_specs ,
20
+ specs_folder ,
21
+ )
19
22
20
23
# Conventions
21
24
_REQUIRED_FIELDS = ["error" , "data" ]
22
25
CONVERTED_SUFFIX = "-converted.yaml"
23
26
_FAKE_SCHEMA_NAME = "FakeSchema"
24
27
25
28
_FAKE_OPEN_API_HEADERS = {
26
- "openapi" : "3.0.0" ,
27
- "info" :{
28
- "title" : "An include file to define sortable attributes" ,
29
- "version" : "1.0.0"
30
- },
31
- "paths" : {},
32
- "components" : {
33
- "parameters" :{},
34
- "schemas" :{}
35
- }
36
- }
29
+ "openapi" : "3.0.0" ,
30
+ "info" : {
31
+ "title" : "An include file to define sortable attributes" ,
32
+ "version" : "1.0.0" ,
33
+ },
34
+ "paths" : {},
35
+ "components" : {"parameters" : {}, "schemas" : {}},
36
+ }
37
+
37
38
38
39
def add_namespace_for_converted_schemas (schema_specs : dict ):
39
40
# schemas converted from jsonschema do not have an overarching namespace.
40
41
# the openapi validator does not like this
41
42
# we use the jsonschema title to create a fake namespace
42
- fake_schema_specs = {
43
- _FAKE_SCHEMA_NAME : schema_specs
44
- }
43
+ fake_schema_specs = {_FAKE_SCHEMA_NAME : schema_specs }
45
44
return fake_schema_specs
46
45
46
+
47
47
def change_references_to_schemas (filepath : Path , specs : dict ):
48
- from os .path import relpath , isabs , join , abspath , exists
48
+ from os .path import relpath , isabs , join , abspath , exists
49
49
50
50
filedir = filepath .parent
51
51
@@ -54,36 +54,44 @@ def change_references_to_schemas(filepath: Path, specs: dict):
54
54
# navigate specs
55
55
change_references_to_schemas (filepath , value )
56
56
57
- elif key in ("allOf" , "oneOf" , "anyOf" ): # navigates allOf, oneOf, anyOf
57
+ elif key in ("allOf" , "oneOf" , "anyOf" ): # navigates allOf, oneOf, anyOf
58
58
for item in value :
59
59
change_references_to_schemas (filepath , item )
60
60
61
- elif key == "$ref" :
61
+ elif key == "$ref" :
62
62
# Ensures value = "file_ref#section_ref"
63
63
value = str (value )
64
- if value .startswith ('#' ):
64
+ if value .startswith ("#" ):
65
65
value = str (filepath ) + value
66
- elif '#' not in value :
66
+ elif "#" not in value :
67
67
value = value + "# "
68
68
69
-
70
69
file_ref , section_ref = value .split ("#" )
71
70
72
71
if not isabs (file_ref ):
73
- file_ref = str (filedir / file_ref )
72
+ file_ref = str (filedir / file_ref )
74
73
75
- file_ref = abspath (file_ref ) # resolves
74
+ file_ref = abspath (file_ref ) # resolves
76
75
assert exists (file_ref ), file_ref
77
76
78
- if 'schemas' in file_ref : # reference to a schema file (i.e. inside a schemas folder)
79
- if not section_ref .startswith ('/components/schemas/' ): # not updated!
80
- section_ref = "/components/schemas/" + section_ref .lstrip ('/' ).strip ()
81
- if file_ref .endswith (CONVERTED_SUFFIX ): # fake name used in converted schemas
77
+ if (
78
+ "schemas" in file_ref
79
+ ): # reference to a schema file (i.e. inside a schemas folder)
80
+ if not section_ref .startswith ("/components/schemas/" ): # not updated!
81
+ section_ref = (
82
+ "/components/schemas/" + section_ref .lstrip ("/" ).strip ()
83
+ )
84
+ if file_ref .endswith (
85
+ CONVERTED_SUFFIX
86
+ ): # fake name used in converted schemas
82
87
section_ref += _FAKE_SCHEMA_NAME
83
88
84
- file_ref = "./" + relpath (file_ref , filedir ) if not filepath .samefile (file_ref ) else ""
85
- specs [key ] = file_ref + "#" + section_ref
86
-
89
+ file_ref = (
90
+ "./" + relpath (file_ref , filedir )
91
+ if not filepath .samefile (file_ref )
92
+ else ""
93
+ )
94
+ specs [key ] = file_ref + "#" + section_ref
87
95
88
96
89
97
@pytest .fixture ("session" )
@@ -98,20 +106,22 @@ def converted_specs_testdir(api_specs_dir, all_api_specs_tails, tmpdir_factory):
98
106
99
107
"""
100
108
basedir = api_specs_dir
101
- testdir = Path ( tmpdir_factory .mktemp ("converted-specs" ) )
109
+ testdir = Path (tmpdir_factory .mktemp ("converted-specs" ))
102
110
103
111
print (testdir )
104
112
105
113
for tail in all_api_specs_tails :
106
114
107
115
# directory with converted specs
108
- os .makedirs (testdir / tail .parent , exist_ok = True )
116
+ os .makedirs (testdir / tail .parent , exist_ok = True )
109
117
110
- specs = load_specs (basedir / tail )
118
+ specs = load_specs (basedir / tail )
111
119
112
- if "schemas" in str (tail ) and \
113
- not is_openapi_schema (specs ) and \
114
- not is_json_schema (specs ):
120
+ if (
121
+ "schemas" in str (tail )
122
+ and not is_openapi_schema (specs )
123
+ and not is_json_schema (specs )
124
+ ):
115
125
116
126
# convert to valid openapi
117
127
if tail .name .endswith (CONVERTED_SUFFIX ):
@@ -121,16 +131,16 @@ def converted_specs_testdir(api_specs_dir, all_api_specs_tails, tmpdir_factory):
121
131
new_specs ["components" ]["schemas" ] = specs
122
132
123
133
# change references
124
- change_references_to_schemas (basedir / tail , new_specs )
125
- dump_specs (new_specs , testdir / tail )
134
+ change_references_to_schemas (basedir / tail , new_specs )
135
+ dump_specs (new_specs , testdir / tail )
126
136
127
137
elif is_openapi_schema (specs ):
128
138
new_specs = specs
129
139
# change references
130
- change_references_to_schemas (basedir / tail , new_specs )
131
- dump_specs (new_specs , testdir / tail )
140
+ change_references_to_schemas (basedir / tail , new_specs )
141
+ dump_specs (new_specs , testdir / tail )
132
142
else :
133
- shutil .copy2 (basedir / tail , testdir / tail )
143
+ shutil .copy2 (basedir / tail , testdir / tail )
134
144
135
145
return testdir
136
146
0 commit comments