Skip to content

Commit 5df6816

Browse files
nezharp1c2u
authored andcommitted
python-openapi/openapi-core#296: Extend alises and adapt tests for OpenAPI 3.1
1 parent f13a935 commit 5df6816

File tree

8 files changed

+206
-69
lines changed

8 files changed

+206
-69
lines changed

openapi_spec_validator/__init__.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@
1717
__all__ = [
1818
'openapi_v2_spec_validator',
1919
'openapi_v3_spec_validator',
20+
'openapi_v30_spec_validator',
2021
'openapi_v31_spec_validator',
2122
'validate_v2_spec',
2223
'validate_v3_spec',
24+
'validate_v30_spec',
25+
'validate_v31_spec',
2326
'validate_spec',
2427
'validate_v2_spec_url',
2528
'validate_v3_spec_url',
29+
'validate_v30_spec_url',
30+
'validate_v31_spec_url',
2631
'validate_spec_url',
2732
]
2833

@@ -47,13 +52,13 @@
4752
)
4853

4954
# v3.0 spec
50-
schema_v3, schema_v3_url = get_openapi_schema('3.0')
51-
openapi_v3_validator_factory = Draft4JSONSpecValidatorFactory(
52-
schema_v3, schema_v3_url,
55+
schema_v30, schema_v30_url = get_openapi_schema('3.0')
56+
openapi_v30_validator_factory = Draft4JSONSpecValidatorFactory(
57+
schema_v30, schema_v30_url,
5358
resolver_handlers=default_handlers,
5459
)
55-
openapi_v3_spec_validator = SpecValidator(
56-
openapi_v3_validator_factory,
60+
openapi_v30_spec_validator = SpecValidator(
61+
openapi_v30_validator_factory,
5762
resolver_handlers=default_handlers,
5863
)
5964

@@ -68,21 +73,27 @@
6873
resolver_handlers=default_handlers,
6974
)
7075

71-
7276
# shortcuts
7377
validate_v2_spec = validate_spec_factory(openapi_v2_spec_validator.validate)
7478
validate_v2_spec_url = validate_spec_url_factory(
7579
openapi_v2_spec_validator.validate, default_handlers)
7680

77-
validate_v3_spec = validate_spec_factory(openapi_v3_spec_validator.validate)
78-
validate_v3_spec_url = validate_spec_url_factory(
79-
openapi_v3_spec_validator.validate, default_handlers)
81+
validate_v30_spec = validate_spec_factory(openapi_v30_spec_validator.validate)
82+
validate_v30_spec_url = validate_spec_url_factory(
83+
openapi_v30_spec_validator.validate, default_handlers)
8084

8185

8286
validate_v31_spec = validate_spec_factory(openapi_v31_spec_validator.validate)
8387
validate_v31_spec_url = validate_spec_url_factory(
8488
openapi_v31_spec_validator.validate, default_handlers)
8589

90+
# aliases to the latest v3 version
91+
schema_v3 = schema_v31
92+
schema_v3_url = schema_v31_url
93+
openapi_v3_validator_factory = openapi_v31_validator_factory
94+
openapi_v3_spec_validator = openapi_v31_spec_validator
95+
validate_v3_spec = validate_v31_spec
96+
validate_v3_spec_url = validate_v31_spec_url
8697

8798
# aliases to the latest version
8899
validate_spec = validate_v3_spec

openapi_spec_validator/__main__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
from jsonschema.exceptions import best_match
66

77
from openapi_spec_validator import (
8-
openapi_v2_spec_validator, openapi_v3_spec_validator,
8+
openapi_v2_spec_validator,
9+
openapi_v30_spec_validator,
10+
openapi_v31_spec_validator,
911
)
1012
from openapi_spec_validator.exceptions import ValidationError
1113
from openapi_spec_validator.readers import read_from_stdin, read_from_filename
@@ -50,10 +52,10 @@ def main(args=None):
5052
)
5153
parser.add_argument(
5254
'--schema',
53-
help="OpenAPI schema (default: 3.0.0)",
55+
help="OpenAPI schema (default: 3.1.0)",
5456
type=str,
55-
choices=['2.0', '3.0.0'],
56-
default='3.0.0'
57+
choices=['2.0', '3.0.0', '3.1.0'],
58+
default='3.1.0'
5759
)
5860
args = parser.parse_args(args)
5961

@@ -72,7 +74,8 @@ def main(args=None):
7274
# choose the validator
7375
validators = {
7476
'2.0': openapi_v2_spec_validator,
75-
'3.0.0': openapi_v3_spec_validator,
77+
'3.0.0': openapi_v30_spec_validator,
78+
'3.1.0': openapi_v31_spec_validator,
7679
}
7780
validator = validators[args.schema]
7881

tests/integration/conftest.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from urllib.parse import urlunparse
66
from yaml import safe_load
77

8-
from openapi_spec_validator import (openapi_v3_spec_validator,
9-
openapi_v2_spec_validator,
8+
from openapi_spec_validator import (openapi_v2_spec_validator,
9+
openapi_v30_spec_validator,
1010
openapi_v31_spec_validator)
1111
from openapi_spec_validator.schemas import read_yaml_file
1212

@@ -43,15 +43,15 @@ def factory():
4343

4444

4545
@pytest.fixture
46-
def validator():
47-
return openapi_v3_spec_validator
46+
def validator_v2():
47+
return openapi_v2_spec_validator
4848

4949

5050
@pytest.fixture
51-
def validator_v31():
52-
return openapi_v31_spec_validator
51+
def validator_v30():
52+
return openapi_v30_spec_validator
5353

5454

5555
@pytest.fixture
56-
def swagger_validator():
57-
return openapi_v2_spec_validator
56+
def validator_v31():
57+
return openapi_v31_spec_validator
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
openapi: "3.1.0"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT License
7+
identifier: MIT
8+
servers:
9+
- url: http://petstore.swagger.io/v1
10+
paths:
11+
/pets:
12+
get:
13+
summary: List all pets
14+
operationId: listPets
15+
tags:
16+
- pets
17+
parameters:
18+
- name: limit
19+
in: query
20+
description: How many items to return at one time (max 100)
21+
required: false
22+
schema:
23+
type: integer
24+
format: int32
25+
responses:
26+
200:
27+
description: An paged array of pets
28+
headers:
29+
x-next:
30+
description: A link to the next page of responses
31+
schema:
32+
type: string
33+
content:
34+
application/json:
35+
schema:
36+
$ref: "#/components/schemas/Pets"
37+
default:
38+
description: unexpected error
39+
content:
40+
application/json:
41+
schema:
42+
$ref: "#/components/schemas/Error"
43+
post:
44+
summary: Create a pet
45+
operationId: createPets
46+
tags:
47+
- pets
48+
responses:
49+
'201':
50+
description: Null response
51+
default:
52+
description: unexpected error
53+
content:
54+
application/json:
55+
schema:
56+
$ref: "#/components/schemas/Error"
57+
/pets/{petId}:
58+
get:
59+
summary: Info for a specific pet
60+
operationId: showPetById
61+
tags:
62+
- pets
63+
parameters:
64+
- name: petId
65+
in: path
66+
required: true
67+
description: The id of the pet to retrieve
68+
schema:
69+
type: string
70+
responses:
71+
'200':
72+
description: Expected response to a valid request
73+
content:
74+
application/json:
75+
schema:
76+
$ref: "#/components/schemas/Pets"
77+
default:
78+
description: unexpected error
79+
content:
80+
application/json:
81+
schema:
82+
$ref: "#/components/schemas/Error"
83+
components:
84+
schemas:
85+
Pet:
86+
required:
87+
- id
88+
- name
89+
properties:
90+
id:
91+
type: integer
92+
format: int64
93+
name:
94+
type: string
95+
tag:
96+
type: string
97+
$ref:
98+
type: string
99+
Pets:
100+
type: array
101+
items:
102+
$ref: "#/components/schemas/Pet"
103+
Error:
104+
required:
105+
- code
106+
- message
107+
properties:
108+
code:
109+
type: integer
110+
format: int32
111+
message:
112+
type: string

tests/integration/test_main.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@
77

88

99
def test_schema_default():
10-
"""Test default schema is 3.0.0"""
11-
testargs = ['./tests/integration/data/v3.0/petstore.yaml']
10+
"""Test default schema is 3.1.0"""
11+
testargs = ['./tests/integration/data/v3.1/petstore.yaml']
1212
main(testargs)
1313

1414

15-
def test_schema_v3():
16-
"""No errors when calling proper v3 file."""
15+
def test_schema_v31():
16+
"""No errors when calling proper v3.1 file."""
17+
testargs = ['--schema', '3.1.0',
18+
'./tests/integration/data/v3.1/petstore.yaml']
19+
main(testargs)
20+
21+
22+
def test_schema_v30():
23+
"""No errors when calling proper v3.0 file."""
1724
testargs = ['--schema', '3.0.0',
1825
'./tests/integration/data/v3.0/petstore.yaml']
1926
main(testargs)
@@ -28,7 +35,10 @@ def test_schema_v2():
2835

2936
def test_errors_on_missing_description_best(capsys):
3037
"""An error is obviously printed given an empty schema."""
31-
testargs = ['./tests/integration/data/v3.0/missing-description.yaml']
38+
testargs = [
39+
'./tests/integration/data/v3.0/missing-description.yaml',
40+
'--schema=3.0.0'
41+
]
3242
with pytest.raises(SystemExit):
3343
main(testargs)
3444
out, err = capsys.readouterr()
@@ -42,7 +52,8 @@ def test_errors_on_missing_description_full(capsys):
4252
"""An error is obviously printed given an empty schema."""
4353
testargs = [
4454
"./tests/integration/data/v3.0/missing-description.yaml",
45-
"--errors=all"
55+
"--errors=all",
56+
"--schema=3.0.0",
4657
]
4758
with pytest.raises(SystemExit):
4859
main(testargs)
@@ -70,7 +81,7 @@ def test_validation_error():
7081

7182

7283
@mock.patch(
73-
'openapi_spec_validator.__main__.openapi_v3_spec_validator.validate',
84+
'openapi_spec_validator.__main__.openapi_v30_spec_validator.validate',
7485
side_effect=Exception,
7586
)
7687
def test_unknown_error(m_validate):
@@ -95,6 +106,6 @@ def test_schema_stdin():
95106
spec_lines = spec_file.readlines()
96107
spec_io = StringIO("".join(spec_lines))
97108

98-
testargs = ['-']
109+
testargs = ['--schema', '3.0.0', '-']
99110
with mock.patch('openapi_spec_validator.__main__.sys.stdin', spec_io):
100111
main(testargs)

tests/integration/test_shortcuts.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import pytest
22

33
from openapi_spec_validator import (
4-
validate_spec, validate_spec_url,
54
validate_v2_spec, validate_v2_spec_url,
65
validate_spec_url_factory,
7-
openapi_v2_spec_validator, openapi_v3_spec_validator,
6+
openapi_v2_spec_validator, openapi_v30_spec_validator,
7+
validate_v30_spec_url, validate_v30_spec,
88
)
99
from openapi_spec_validator.exceptions import OpenAPIValidationError
1010
from openapi_spec_validator.handlers.urllib import UrllibHandler
@@ -26,14 +26,14 @@ def test_failed(self, spec):
2626
class BaseTestValidValidteSpec:
2727

2828
def test_valid(self, spec):
29-
validate_spec(spec)
29+
validate_v30_spec(spec)
3030

3131

3232
class BaseTestFaliedValidateSpec:
3333

3434
def test_failed(self, spec):
3535
with pytest.raises(OpenAPIValidationError):
36-
validate_spec(spec)
36+
validate_v30_spec(spec)
3737

3838

3939
class BaseTestValidValidateSpecUrl:
@@ -70,15 +70,15 @@ def test_failed(self, spec_url):
7070
validate_v2_spec_url(spec_url)
7171

7272

73-
class BaseTestValidValidateV3SpecUrl(BaseTestValidValidateSpecUrl):
73+
class BaseTestValidValidateV30SpecUrl(BaseTestValidValidateSpecUrl):
7474

7575
@pytest.fixture
7676
def validate_spec_url_callable(self, urllib_handlers):
7777
return validate_spec_url_factory(
78-
openapi_v3_spec_validator.validate, urllib_handlers)
78+
openapi_v30_spec_validator.validate, urllib_handlers)
7979

8080
def test_default_valid(self, spec_url):
81-
validate_spec_url(spec_url)
81+
validate_v30_spec_url(spec_url)
8282

8383
def test_urllib_valid(self, validate_spec_url_callable, spec_url):
8484
validate_spec_url_callable(spec_url)
@@ -88,7 +88,7 @@ class BaseTestFaliedValidateSpecUrl:
8888

8989
def test_failed(self, spec_url):
9090
with pytest.raises(OpenAPIValidationError):
91-
validate_spec_url(spec_url)
91+
validate_v30_spec_url(spec_url)
9292

9393

9494
class TestLocalEmptyExample(BaseTestFaliedValidateSpec):
@@ -145,7 +145,7 @@ def spec_url(self):
145145
)
146146

147147

148-
class TestPetstoreExample(BaseTestValidValidateV3SpecUrl):
148+
class TestPetstoreExample(BaseTestValidValidateV30SpecUrl):
149149

150150
@pytest.fixture
151151
def spec_url(self):
@@ -156,7 +156,7 @@ def spec_url(self):
156156
)
157157

158158

159-
class TestApiWithExample(BaseTestValidValidateV3SpecUrl):
159+
class TestApiWithExample(BaseTestValidValidateV30SpecUrl):
160160

161161
@pytest.fixture
162162
def spec_url(self):
@@ -167,7 +167,7 @@ def spec_url(self):
167167
)
168168

169169

170-
class TestPetstoreExpandedExample(BaseTestValidValidateV3SpecUrl):
170+
class TestPetstoreExpandedExample(BaseTestValidValidateV30SpecUrl):
171171

172172
@pytest.fixture
173173
def spec_url(self):

0 commit comments

Comments
 (0)