From 632d2037d0c19c13021371be39585e8ad3efc46f Mon Sep 17 00:00:00 2001 From: Alex Zywicki Date: Mon, 17 Jan 2022 10:39:12 -0600 Subject: [PATCH] Convert from AsyncApiSpec to JsonMapping --- asynction/mock_server.py | 4 ++-- asynction/server.py | 11 ++++++----- tests/unit/test_mock_server.py | 5 ++++- tests/unit/test_server.py | 5 ++++- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/asynction/mock_server.py b/asynction/mock_server.py index cf6847c..2a65d7e 100644 --- a/asynction/mock_server.py +++ b/asynction/mock_server.py @@ -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, @@ -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``. diff --git a/asynction/server.py b/asynction/server.py index d222331..d7197f6 100644 --- a/asynction/server.py +++ b/asynction/server.py @@ -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, @@ -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``. @@ -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 ( diff --git a/tests/unit/test_mock_server.py b/tests/unit/test_mock_server.py index 603c78f..cf98922 100644 --- a/tests/unit/test_mock_server.py +++ b/tests/unit/test_mock_server.py @@ -1,4 +1,5 @@ import threading +from dataclasses import asdict from ipaddress import IPv4Address from typing import Any from typing import Callable @@ -12,6 +13,7 @@ import jsonschema import pytest +import yaml from faker import Faker from flask.app import Flask from flask_socketio import SocketIO @@ -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) diff --git a/tests/unit/test_server.py b/tests/unit/test_server.py index a7b9c12..990e7fb 100644 --- a/tests/unit/test_server.py +++ b/tests/unit/test_server.py @@ -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 @@ -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)