-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #291 from python-openapi/feature/versions-submodule
Versions submodule
- Loading branch information
Showing
9 changed files
with
152 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from openapi_spec_validator.versions.consts import OPENAPIV2 | ||
from openapi_spec_validator.versions.consts import OPENAPIV30 | ||
from openapi_spec_validator.versions.consts import OPENAPIV31 | ||
from openapi_spec_validator.versions.datatypes import SpecVersion | ||
from openapi_spec_validator.versions.shortcuts import get_spec_version | ||
|
||
__all__ = [ | ||
"OPENAPIV2", | ||
"OPENAPIV30", | ||
"OPENAPIV31", | ||
"SpecVersion", | ||
"get_spec_version", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from typing import List | ||
|
||
from openapi_spec_validator.versions.datatypes import SpecVersion | ||
|
||
OPENAPIV2 = SpecVersion( | ||
keyword="swagger", | ||
major="2", | ||
minor="0", | ||
) | ||
|
||
OPENAPIV30 = SpecVersion( | ||
keyword="openapi", | ||
major="3", | ||
minor="0", | ||
) | ||
|
||
OPENAPIV31 = SpecVersion( | ||
keyword="openapi", | ||
major="3", | ||
minor="1", | ||
) | ||
|
||
VERSIONS: List[SpecVersion] = [OPENAPIV2, OPENAPIV30, OPENAPIV31] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass(frozen=True) | ||
class SpecVersion: | ||
""" | ||
Spec version designates the OAS feature set. | ||
""" | ||
|
||
keyword: str | ||
major: str | ||
minor: str | ||
|
||
def __str__(self) -> str: | ||
return f"OpenAPIV{self.major}.{self.minor}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from openapi_spec_validator.exceptions import OpenAPIError | ||
|
||
|
||
class OpenAPIVersionNotFound(OpenAPIError): | ||
def __str__(self) -> str: | ||
return "Specification version not found" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from re import compile | ||
from typing import List | ||
|
||
from jsonschema_spec.typing import Schema | ||
|
||
from openapi_spec_validator.versions.datatypes import SpecVersion | ||
from openapi_spec_validator.versions.exceptions import OpenAPIVersionNotFound | ||
|
||
|
||
class SpecVersionFinder: | ||
pattern = compile(r"(?P<major>\d+)\.(?P<minor>\d+)(\..*)?") | ||
|
||
def __init__(self, versions: List[SpecVersion]) -> None: | ||
self.versions = versions | ||
|
||
def find(self, spec: Schema) -> SpecVersion: | ||
for v in self.versions: | ||
if v.keyword in spec: | ||
version_str = spec[v.keyword] | ||
m = self.pattern.match(version_str) | ||
if m: | ||
version = SpecVersion(**m.groupdict(), keyword=v.keyword) | ||
if v == version: | ||
return v | ||
|
||
raise OpenAPIVersionNotFound |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from jsonschema_spec.typing import Schema | ||
|
||
from openapi_spec_validator.versions.consts import VERSIONS | ||
from openapi_spec_validator.versions.datatypes import SpecVersion | ||
from openapi_spec_validator.versions.finders import SpecVersionFinder | ||
|
||
|
||
def get_spec_version(spec: Schema) -> SpecVersion: | ||
finder = SpecVersionFinder(VERSIONS) | ||
return finder.find(spec) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import pytest | ||
|
||
from openapi_spec_validator.versions import consts as versions | ||
from openapi_spec_validator.versions.exceptions import OpenAPIVersionNotFound | ||
from openapi_spec_validator.versions.shortcuts import get_spec_version | ||
|
||
|
||
class TestGetSpecVersion: | ||
def test_no_keyword(self): | ||
spec = {} | ||
|
||
with pytest.raises(OpenAPIVersionNotFound): | ||
get_spec_version(spec) | ||
|
||
@pytest.mark.parametrize("keyword", ["swagger", "openapi"]) | ||
@pytest.mark.parametrize("version", ["x.y.z", "xyz2.0.0", "2.xyz0.0"]) | ||
def test_invalid(self, keyword, version): | ||
spec = { | ||
keyword: version, | ||
} | ||
|
||
with pytest.raises(OpenAPIVersionNotFound): | ||
get_spec_version(spec) | ||
|
||
@pytest.mark.parametrize( | ||
"keyword,version,expected", | ||
[ | ||
("swagger", "2.0", versions.OPENAPIV2), | ||
("openapi", "3.0.0", versions.OPENAPIV30), | ||
("openapi", "3.0.1", versions.OPENAPIV30), | ||
("openapi", "3.0.2", versions.OPENAPIV30), | ||
("openapi", "3.0.3", versions.OPENAPIV30), | ||
("openapi", "3.1.0", versions.OPENAPIV31), | ||
], | ||
) | ||
def test_valid(self, keyword, version, expected): | ||
spec = { | ||
keyword: version, | ||
} | ||
|
||
result = get_spec_version(spec) | ||
|
||
assert result == expected |