Skip to content

Commit c084e1c

Browse files
authored
Merge pull request #803 from marshmallow-code/make_validate_spec_private_api
Remove validate_spec from public API
2 parents 549a05e + aa39184 commit c084e1c

File tree

3 files changed

+40
-43
lines changed

3 files changed

+40
-43
lines changed

src/apispec/utils.py

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@
55
from __future__ import annotations
66

77
import re
8-
import json
9-
import typing
10-
11-
from apispec import exceptions
12-
13-
if typing.TYPE_CHECKING:
14-
from apispec.core import APISpec
158

169

1710
COMPONENT_SUBSECTIONS = {
@@ -50,37 +43,6 @@ def build_reference(
5043
}
5144

5245

53-
def validate_spec(spec: APISpec) -> bool:
54-
"""Validate the output of an :class:`APISpec` object against the
55-
OpenAPI specification.
56-
57-
Note: Requires installing apispec with the ``[validation]`` extras.
58-
::
59-
60-
pip install 'apispec[validation]'
61-
62-
:raise: apispec.exceptions.OpenAPIError if validation fails.
63-
"""
64-
try:
65-
import prance
66-
except ImportError as error: # re-raise with a more verbose message
67-
exc_class = type(error)
68-
raise exc_class(
69-
"validate_spec requires prance to be installed. "
70-
"You can install all validation requirements using:\n"
71-
" pip install 'apispec[validation]'"
72-
) from error
73-
parser_kwargs = {}
74-
if spec.openapi_version.major == 3:
75-
parser_kwargs["backend"] = "openapi-spec-validator"
76-
try:
77-
prance.BaseParser(spec_string=json.dumps(spec.to_dict()), **parser_kwargs)
78-
except prance.ValidationError as err:
79-
raise exceptions.OpenAPIError(*err.args) from err
80-
else:
81-
return True
82-
83-
8446
# from django.contrib.admindocs.utils
8547
def trim_docstring(docstring: str) -> str:
8648
"""Uniformly trims leading/trailing whitespace from docstrings.

tests/test_ext_marshmallow_openapi.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import pytest
21
from datetime import datetime
32

3+
import pytest
4+
45
from marshmallow import EXCLUDE, fields, INCLUDE, RAISE, Schema, validate
56

67
from apispec.ext.marshmallow import MarshmallowPlugin
7-
from apispec import exceptions, utils, APISpec
8+
from apispec import exceptions, APISpec
89

910
from .schemas import CustomList, CustomStringField
10-
from .utils import get_schemas, build_ref
11+
from .utils import get_schemas, build_ref, validate_spec
1112

1213

1314
class TestMarshmallowFieldToOpenAPI:
@@ -533,7 +534,7 @@ def test_openapi_tools_validate_v2():
533534
},
534535
)
535536
try:
536-
utils.validate_spec(spec)
537+
validate_spec(spec)
537538
except exceptions.OpenAPIError as error:
538539
pytest.fail(str(error))
539540

@@ -602,7 +603,7 @@ def test_openapi_tools_validate_v3():
602603
},
603604
)
604605
try:
605-
utils.validate_spec(spec)
606+
validate_spec(spec)
606607
except exceptions.OpenAPIError as error:
607608
pytest.fail(str(error))
608609

tests/utils.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"""Utilities to get elements of generated spec"""
2+
import json
23

4+
from apispec.core import APISpec
5+
from apispec import exceptions
36
from apispec.utils import build_reference
47

58

@@ -43,3 +46,34 @@ def get_paths(spec):
4346

4447
def build_ref(spec, component_type, obj):
4548
return build_reference(component_type, spec.openapi_version.major, obj)
49+
50+
51+
def validate_spec(spec: APISpec) -> bool:
52+
"""Validate the output of an :class:`APISpec` object against the
53+
OpenAPI specification.
54+
55+
Note: Requires installing apispec with the ``[validation]`` extras.
56+
::
57+
58+
pip install 'apispec[validation]'
59+
60+
:raise: apispec.exceptions.OpenAPIError if validation fails.
61+
"""
62+
try:
63+
import prance
64+
except ImportError as error: # re-raise with a more verbose message
65+
exc_class = type(error)
66+
raise exc_class(
67+
"validate_spec requires prance to be installed. "
68+
"You can install all validation requirements using:\n"
69+
" pip install 'apispec[validation]'"
70+
) from error
71+
parser_kwargs = {}
72+
if spec.openapi_version.major == 3:
73+
parser_kwargs["backend"] = "openapi-spec-validator"
74+
try:
75+
prance.BaseParser(spec_string=json.dumps(spec.to_dict()), **parser_kwargs)
76+
except prance.ValidationError as err:
77+
raise exceptions.OpenAPIError(*err.args) from err
78+
else:
79+
return True

0 commit comments

Comments
 (0)