1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
Metadata-Version: 2.4
Name: prance
Version: 25.4.8.0
Summary: Resolving Swagger/OpenAPI 2.0 and 3.0.0 Parser
Home-page: https://github.com/RonnyPfannschmidt/prance
Author: Jens Finkhaeuser
Author-email: jens@finkhaeuser.de
Maintainer: Ronny Pfannschmidt
Maintainer-email: opensource@ronnypfannschmidt.de
License: MITNFA
Keywords: swagger openapi parsing
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Plugins
Classifier: Intended Audience :: Developers
Classifier: Natural Language :: English
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.10
Description-Content-Type: text/x-rst
License-File: LICENSE.txt
Requires-Dist: chardet>=5.2
Requires-Dist: ruamel.yaml>=0.18.10
Requires-Dist: requests>=2.32.3
Requires-Dist: packaging>=24.2
Provides-Extra: dev
Requires-Dist: tox>=4.23.2; extra == "dev"
Requires-Dist: bumpversion>=0.6.0; extra == "dev"
Requires-Dist: pytest>=8.3.5; extra == "dev"
Requires-Dist: pytest-cov>=6.0; extra == "dev"
Requires-Dist: sphinx>=8.1.3; extra == "dev"
Requires-Dist: towncrier>=24.8; extra == "dev"
Provides-Extra: icu
Requires-Dist: PyICU~=2.14; extra == "icu"
Provides-Extra: ssv
Requires-Dist: swagger-spec-validator~=3.0.4; extra == "ssv"
Provides-Extra: osv
Requires-Dist: openapi-spec-validator~=0.7.1; extra == "osv"
Provides-Extra: flex
Requires-Dist: flex~=6.14.1; extra == "flex"
Provides-Extra: cli
Requires-Dist: click>=8.1.8; extra == "cli"
Dynamic: license-file
|License| |PyPI| |Python Versions| |Package Format| |Package Status|
|Logo|
Prance provides parsers for `Swagger/OpenAPI
2.0 and 3.0 <http://swagger.io/specification/>`__ API specifications in Python.
It uses `openapi\_spec\_validator <https://github.com/p1c2u/openapi-spec-validator>`__,
`swagger\_spec\_validator <https://github.com/Yelp/swagger_spec_validator>`__ or
`flex <https://github.com/pipermerriam/flex>`__
to validate specifications, but additionally resolves `JSON
references <https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03>`__
in accordance with the OpenAPI spec.
Mostly the latter involves handling non-URI references; OpenAPI is fine
with providing relative file paths, whereas JSON references require URIs
at this point in time.
Usage
=====
Installation
------------
Prance is available from PyPI, and can be installed via pip:
.. code:: bash
$ pip install prance
Note that this will install the code, but additional subpackages must be specified
to unlock various pieces of functionality. At minimum, a parsing backend must be
installed. For the CLI functionality, you need further dependencies.
The recommended installation installs the CLI, uses ICU and installs one validation
backend:
.. code:: bash
$ pip install prance[osv,icu,cli]
Make sure you have `ICU Unicode Library <http://site.icu-project.org/home>`__ installed,
as well as Python dev library before running the commands above. If not, use the
following commands:
.. code:: bash
$ sudo apt-get install libicu-dev python3-dev # Ubuntu/Debian
$ sudo dnf install libicu-devel python3-devel # Fedora
Command Line Interface
----------------------
After installing prance, a CLI is available for validating (and resolving
external references in) specs:
.. code:: bash
# Validates with resolving
$ prance validate path/to/swagger.yml
# Validates without resolving
$ prance validate --no-resolve path/to/swagger.yml
# Fetch URL, validate and resolve.
$ prance validate http://petstore.swagger.io/v2/swagger.json
Processing "http://petstore.swagger.io/v2/swagger.json"...
-> Resolving external references.
Validates OK as Swagger/OpenAPI 2.0!
Validation is not the only feature of prance. One of the side effects of
resolving is that from a spec with references, one can create a fully resolved
output spec. In the past, this was done via options to the ``validate`` command,
but now there's a specific command just for this purpose:
.. code:: bash
# Compile spec
$ prance compile path/to/input.yml path/to/output.yml
Lastly, with the arrival of OpenAPI 3.0.0, it becomes useful for tooling to
convert older specs to the new standard. Instead of re-inventing the wheel,
prance just provides a CLI command for passing specs to the web API of
`swagger2openapi <https://github.com/Mermade/swagger2openapi>`__ - a working
internet connection is therefore required for this command:
.. code:: bash
# Convert spec
$ prance convert path/to/swagger.yml path/to/openapi.yml
Code
----
Most likely you have spec file and want to parse it:
.. code:: python
from prance import ResolvingParser
parser = ResolvingParser('path/to/my/swagger.yaml')
parser.specification # contains fully resolved specs as a dict
Prance also includes a non-resolving parser that does not follow JSON
references, in case you prefer that.
.. code:: python
from prance import BaseParser
parser = BaseParser('path/to/my/swagger.yaml')
parser.specification # contains specs as a dict still containing JSON references
On Windows, the code reacts correctly if you pass posix-like paths
(``/c:/swagger``) or if the path is relative. If you pass absolute
windows path (like ``c:\swagger.yaml``), you can use
``prance.util.fs.abspath`` to convert them.
URLs can also be parsed:
.. code:: python
parser = ResolvingParser('http://petstore.swagger.io/v2/swagger.json')
Largely, that's it. There is a whole slew of utility code that you may
or may not find useful, too. Look at the `full documentation
<https://prance.readthedocs.io/en/latest/#api-modules>`__ for details.
Compatibility
-------------
*Python Versions*
Version 0.16.2 is the last version supporting Python 2. It was released on
Nov 12th, 2019. Python 2 reaches end of life at the end of 2019. If you wish
for updates to the Python 2 supported packages, please contact the maintainer
directly.
Until fairly recently, we also tested with `PyPy <https://www.pypy.org/>`__.
Unfortunately, Travis isn't very good at supporting this. So in the absence
of spare time, they're disabled. `Issue 50 <https://github.com/jfinkhaeuser/prance/issues/50>`__
tracks progress on that.
Similarly, but less critically, Python 3.4 is no longer receiving a lot of
love from CI vendors, so automated builds on that version are no longer
supported.
*Backends*
Different validation backends support different features.
+------------------------+----------------+-----------------+-------------+-------------------------------------------------------+----------------+-----------------------------------------------------------------------------------+
| Backend | Python Version | OpenAPI Version | Strict Mode | Notes | Available From | Link |
+========================+================+=================+=============+=======================================================+================+===================================================================================+
| swagger-spec-validator | 2 and 3 | 2.0 only | yes | Slow; does not accept integer keys (see strict mode). | prance 0.1 | `swagger\_spec\_validator <https://github.com/Yelp/swagger_spec_validator>`__ |
+------------------------+----------------+-----------------+-------------+-------------------------------------------------------+----------------+-----------------------------------------------------------------------------------+
| flex | 2 and 3 | 2.0 only | n/a | Fastest; unfortunately deprecated. | prance 0.8 | `flex <https://github.com/pipermerriam/flex>`__ |
+------------------------+----------------+-----------------+-------------+-------------------------------------------------------+----------------+-----------------------------------------------------------------------------------+
| openapi-spec-validator | 2 and 3 | 2.0 and 3.0 | yes | Slow; does not accept integer keys (see strict mode). | prance 0.11 | `openapi\_spec\_validator <https://github.com/p1c2u/openapi-spec-validator>`__ |
+------------------------+----------------+-----------------+-------------+-------------------------------------------------------+----------------+-----------------------------------------------------------------------------------+
You can select the backend in the constructor of the parser(s):
.. code:: python
parser = ResolvingParser('http://petstore.swagger.io/v2/swagger.json', backend = 'openapi-spec-validator')
No backend is included in the dependencies; they are detected at run-time. If you install them,
they can be used:
.. code:: bash
$ pip install openapi-spec-validator
$ pip install prance
$ prance validate --backend=openapi-spec-validator path/to/spec.yml
*A note on flex usage:* While flex is the fastest validation backend, unfortunately it is no longer
maintained and there are issues with its dependencies. For one thing, it depends on a version of `PyYAML`
that contains security flaws. For another, it depends explicitly on older versions of `click`.
If you use the flex subpackage, therefore, you do so at your own risk.
*Compatibility*
See `COMPATIBILITY.rst <https://github.com/jfinkhaeuser/prance/blob/master/COMPATIBILITY.rst>`__
for a list of known issues.
Partial Reference Resolution
----------------------------
It's possible to instruct the parser to only resolve some kinds of references.
This allows e.g. resolving references from external URLs, whilst keeping local
references (i.e. to local files, or file internal) intact.
.. code:: python
from prance import ResolvingParser
from prance.util.resolver import RESOLVE_HTTP
parser = ResolvingParser('/path/to/spec', resolve_types = RESOLVE_HTTP)
Multiple types can be specified by OR-ing constants together:
.. code:: python
from prance import ResolvingParser
from prance.util.resolver import RESOLVE_HTTP, RESOLVE_FILES
parser = ResolvingParser('/path/to/spec', resolve_types = RESOLVE_HTTP | RESOLVE_FILES)
Extensions
----------
Prance includes the ability to reference outside swagger definitions
in outside Python packages. Such a package must already be importable
(i.e. installed), and be accessible via the
`ResourceManager API <https://setuptools.readthedocs.io/en/latest/pkg_resources.html#resourcemanager-api>`__
(some more info `here <https://setuptools.readthedocs.io/en/latest/setuptools.html#including-data-files>`__).
For example, you might create a package ``common_swag`` with the file
``base.yaml`` containing the definition
.. code:: yaml
definitions:
Severity:
type: string
enum:
- INFO
- WARN
- ERROR
- FATAL
In the ``setup.py`` for ``common_swag`` you would add lines such as
.. code:: python
packages=find_packages('src'),
package_dir={'': 'src'},
package_data={
'': '*.yaml'
}
Then, having installed ``common_swag`` into some application, you could
now write
.. code:: yaml
definitions:
Message:
type: object
properties:
severity:
$ref: 'python://common_swag/base.yaml#/definitions/Severity'
code:
type: string
summary:
type: string
description:
type: string
required:
- severity
- summary
Contributing
============
See `CONTRIBUTING.md <https://github.com/jfinkhaeuser/prance/blob/master/CONTRIBUTING.md>`__ for details.
Professional support is available through `finkhaeuser consulting <https://finkhaeuser.de>`__.
License
=======
Licensed under MIT. See the `LICENSE.txt <https://github.com/RonnyPfannschmidt/prance/blob/master/LICENSE.txt>`__ file for details.
"Prancing unicorn" logo image Copyright (c) Jens Finkhaeuser.
Made by `Moreven B <http://morevenb.com/>`__. Use of the logo is permitted under
the `Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International license <https://creativecommons.org/licenses/by-nc-sa/4.0/>`__.
.. |License| image:: https://img.shields.io/pypi/l/prance.svg
:target: https://pypi.python.org/pypi/prance/
.. |PyPI| image:: https://img.shields.io/pypi/v/prance.svg
:target: https://pypi.python.org/pypi/prance/
.. |Package Format| image:: https://img.shields.io/pypi/format/prance.svg
:target: https://pypi.python.org/pypi/prance/
.. |Python Versions| image:: https://img.shields.io/pypi/pyversions/prance.svg
:target: https://pypi.python.org/pypi/prance/
.. |Package Status| image:: https://img.shields.io/pypi/status/prance.svg
:target: https://pypi.python.org/pypi/prance/
.. |Logo| image:: https://raw.githubusercontent.com/RonnyPfannschmidt/prance/master/docs/images/prance_logo_256.png
|