-
-
Notifications
You must be signed in to change notification settings - Fork 228
feat(parser): Detect OpenAPI documents of incorrect versions (closes #281) #302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 contains hidden or 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 contains hidden or 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 |
---|---|---|
@@ -1,69 +1,50 @@ | ||
""" | ||
OpenAPI v3.0.3 schema types, created according to the specification: | ||
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md | ||
|
||
The type orders are according to the contents of the specification: | ||
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#table-of-contents | ||
""" | ||
|
||
__all__ = [ | ||
"Components", | ||
"Contact", | ||
"Discriminator", | ||
"Encoding", | ||
"Example", | ||
"ExternalDocumentation", | ||
"Header", | ||
"Info", | ||
"License", | ||
"Link", | ||
"MediaType", | ||
"OAuthFlow", | ||
"OAuthFlows", | ||
"OpenAPI", | ||
"Operation", | ||
"Parameter", | ||
"PathItem", | ||
"Paths", | ||
"Reference", | ||
"RequestBody", | ||
"Response", | ||
"Responses", | ||
"Schema", | ||
"SecurityRequirement", | ||
"SecurityScheme", | ||
"Server", | ||
"ServerVariable", | ||
"Tag", | ||
"XML", | ||
] | ||
|
||
from .components import Components | ||
from .contact import Contact | ||
from .discriminator import Discriminator | ||
from .encoding import Encoding | ||
from .example import Example | ||
from .external_documentation import ExternalDocumentation | ||
from .header import Header | ||
from .info import Info | ||
from .license import License | ||
from .link import Link | ||
from .media_type import MediaType | ||
from .oauth_flow import OAuthFlow | ||
from .oauth_flows import OAuthFlows | ||
from .open_api import OpenAPI | ||
from .operation import Operation | ||
from .parameter import Parameter | ||
from .path_item import PathItem | ||
from .paths import Paths | ||
from .reference import Reference | ||
from .request_body import RequestBody | ||
from .response import Response | ||
from .responses import Responses | ||
from .schema import Schema | ||
from .security_requirement import SecurityRequirement | ||
from .security_scheme import SecurityScheme | ||
from .server import Server | ||
from .server_variable import ServerVariable | ||
from .tag import Tag | ||
from .xml import XML | ||
|
||
import re | ||
from typing import Callable, Iterator | ||
|
||
from .openapi_schema_pydantic import MediaType | ||
from .openapi_schema_pydantic import OpenAPI as _OpenAPI | ||
from .openapi_schema_pydantic import Operation, Parameter, PathItem, Reference, RequestBody, Response, Responses, Schema | ||
|
||
regex = re.compile(r"(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)") | ||
|
||
|
||
class SemVer: | ||
def __init__(self, str_value: str) -> None: | ||
self.str_value = str_value | ||
if not isinstance(str_value, str): | ||
raise TypeError("string required") | ||
m = regex.fullmatch(str_value) | ||
if not m: | ||
raise ValueError("invalid semantic versioning format") | ||
self.major = int(m.group(1)) | ||
self.minor = int(m.group(2)) | ||
self.patch = int(m.group(3)) | ||
|
||
@classmethod | ||
def __get_validators__(cls) -> Iterator[Callable[[str], "SemVer"]]: | ||
yield cls.validate | ||
|
||
@classmethod | ||
def validate(cls, v: str) -> "SemVer": | ||
return cls(v) | ||
|
||
def __str__(self) -> str: | ||
return self.str_value | ||
|
||
|
||
class OpenAPI(_OpenAPI): | ||
openapi: SemVer |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
openapi_python_client/schema/README.md → .../schema/openapi_schema_pydantic/README.md
This file contains hidden or 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
69 changes: 69 additions & 0 deletions
69
openapi_python_client/schema/openapi_schema_pydantic/__init__.py
This file contains hidden or 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,69 @@ | ||
""" | ||
OpenAPI v3.0.3 schema types, created according to the specification: | ||
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md | ||
|
||
The type orders are according to the contents of the specification: | ||
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#table-of-contents | ||
""" | ||
|
||
__all__ = [ | ||
"Components", | ||
"Contact", | ||
"Discriminator", | ||
"Encoding", | ||
"Example", | ||
"ExternalDocumentation", | ||
"Header", | ||
"Info", | ||
"License", | ||
"Link", | ||
"MediaType", | ||
"OAuthFlow", | ||
"OAuthFlows", | ||
"OpenAPI", | ||
"Operation", | ||
"Parameter", | ||
"PathItem", | ||
"Paths", | ||
"Reference", | ||
"RequestBody", | ||
"Response", | ||
"Responses", | ||
"Schema", | ||
"SecurityRequirement", | ||
"SecurityScheme", | ||
"Server", | ||
"ServerVariable", | ||
"Tag", | ||
"XML", | ||
] | ||
|
||
from .components import Components | ||
from .contact import Contact | ||
from .discriminator import Discriminator | ||
from .encoding import Encoding | ||
from .example import Example | ||
from .external_documentation import ExternalDocumentation | ||
from .header import Header | ||
from .info import Info | ||
from .license import License | ||
from .link import Link | ||
from .media_type import MediaType | ||
from .oauth_flow import OAuthFlow | ||
from .oauth_flows import OAuthFlows | ||
from .open_api import OpenAPI | ||
from .operation import Operation | ||
from .parameter import Parameter | ||
from .path_item import PathItem | ||
from .paths import Paths | ||
from .reference import Reference | ||
from .request_body import RequestBody | ||
from .response import Response | ||
from .responses import Responses | ||
from .schema import Schema | ||
from .security_requirement import SecurityRequirement | ||
from .security_scheme import SecurityScheme | ||
from .server import Server | ||
from .server_variable import ServerVariable | ||
from .tag import Tag | ||
from .xml import XML |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or 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 contains hidden or 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,16 @@ | ||
import pytest | ||
from pydantic import ValidationError | ||
|
||
from openapi_python_client.schema import OpenAPI | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"version, valid", [("abc", False), ("1", False), ("2.0", False), ("3.0.0", True), ("3.1.0-b.3", False), (1, False)] | ||
) | ||
def test_validate_version(version, valid): | ||
data = {"openapi": version, "info": {"title": "test", "version": ""}, "paths": {}} | ||
if valid: | ||
OpenAPI.parse_obj(data) | ||
else: | ||
with pytest.raises(ValidationError): | ||
OpenAPI.parse_obj(data) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just check for this or the
openapi
field before attempting to parse the dict?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
swagger
) so I don't want to disallow that if the spec is otherwise validopenapi
seems like a task for Pydantic since the semver requirement is part of the OpenAPI spec.