Skip to content

Commit

Permalink
Convert from AsyncApiSpec to JsonMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-zywicki committed Jan 17, 2022
1 parent e012e03 commit 632d203
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
4 changes: 2 additions & 2 deletions asynction/mock_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def __init__(
@classmethod
def from_spec(
cls,
spec_path: Union[Path, AsyncApiSpec],
spec_path: Union[Path, JSONMapping],
validation: bool = True,
server_name: Optional[str] = None,
docs: bool = True,
Expand All @@ -162,7 +162,7 @@ def from_spec(
* ``custom_formats_sample_size``
:param spec_path: The path where the AsyncAPI YAML specification is located,
or a pre loaded AsyncApiSpec object.
or a pre loaded JSONMapping object.
:param validation: When set to ``False``, message payloads, channel
bindings and ack callbacks are NOT validated.
Defaults to ``True``.
Expand Down
11 changes: 6 additions & 5 deletions asynction/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def init_app(self, app: Optional[Flask], **kwargs) -> None:
@classmethod
def from_spec(
cls,
spec_path: Union[Path, AsyncApiSpec],
spec_path: Union[Path, JSONMapping],
validation: bool = True,
server_name: Optional[str] = None,
docs: bool = True,
Expand All @@ -126,7 +126,7 @@ def from_spec(
This is the single entrypoint to the Asynction server API.
:param spec_path: The path where the AsyncAPI YAML specification is located,
or a pre loaded AsyncApiSpec object.
or a pre loaded JSONMapping object.
:param validation: When set to ``False``, message payloads, channel
bindings and ack callbacks are NOT validated.
Defaults to ``True``.
Expand Down Expand Up @@ -157,10 +157,11 @@ def from_spec(
)
"""
if isinstance(spec_path, AsyncApiSpec):
spec = spec_path
else:
if isinstance(spec_path, Path):
spec = load_spec(spec_path=spec_path)
else:
raw_resolved = resolve_references(raw_spec=spec_path)
spec = AsyncApiSpec.from_dict(raw_resolved)

server_security: Sequence[SecurityRequirement] = []
if (
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/test_mock_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import threading
from dataclasses import asdict
from ipaddress import IPv4Address
from typing import Any
from typing import Callable
Expand All @@ -12,6 +13,7 @@

import jsonschema
import pytest
import yaml
from faker import Faker
from flask.app import Flask
from flask_socketio import SocketIO
Expand Down Expand Up @@ -142,7 +144,8 @@ def test_mock_asynction_socketio_from_spec(fixture_paths: FixturePaths):


def test_mock_asynction_socketio_from_spec_object(fixture_paths: FixturePaths):
spec = load_spec(fixture_paths.simple)
with open(fixture_paths.simple, "r") as simple:
spec = yaml.safe_load(simple)
mock_asio = MockAsynctionSocketIO.from_spec(spec_path=spec)
assert isinstance(mock_asio, MockAsynctionSocketIO)
assert isinstance(mock_asio.faker, Faker)
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/test_server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from dataclasses import asdict
from typing import Optional
from unittest import mock

import pytest
import yaml
from faker import Faker
from flask import Flask

Expand Down Expand Up @@ -51,7 +53,8 @@ def test_asynction_socketio_from_spec(fixture_paths: FixturePaths):


def test_asynction_socketio_from_spec_object(fixture_paths: FixturePaths):
spec = load_spec(fixture_paths.simple)
with open(fixture_paths.simple, "r") as simple:
spec = yaml.safe_load(simple)
asio = AsynctionSocketIO.from_spec(spec_path=spec)
assert isinstance(asio, AsynctionSocketIO)

Expand Down

0 comments on commit 632d203

Please sign in to comment.