Prance

25.4.8.0 · active · verified Thu Apr 09

Prance is a Python library that provides parsers for Swagger/OpenAPI 2.0 and 3.0 API specifications. It validates specifications using backends like `openapi_spec_validator`, `swagger_spec_validator`, or `flex`, and additionally resolves JSON references, including non-URI file paths. The current version is 25.4.8.0, and it follows a Calendar Versioning (CalVer) release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `ResolvingParser` to load an OpenAPI/Swagger specification from a file (or URL) and access its fully resolved dictionary representation.

from prance import ResolvingParser
import os

# Example OpenAPI/Swagger spec content (YAML format for demonstration)
# In a real scenario, this would be loaded from a file or URL
spec_content = """
openapi: 3.0.0
info:
  title: Sample API
  version: 1.0.0
paths:
  /items:
    get:
      summary: Get all items
      responses:
        '200':
          description: A list of items
"""

# Create a dummy spec file for the example
with open('sample_spec.yaml', 'w') as f:
    f.write(spec_content)

try:
    # Initialize the parser with the path to your spec file
    parser = ResolvingParser('sample_spec.yaml')

    # The fully resolved specification is available as a dictionary
    resolved_spec = parser.specification
    print(f"API Title: {resolved_spec['info']['title']}")
    print(f"API Version: {resolved_spec['info']['version']}")
    print("Specification parsed successfully.")

    # Example with a URL (requires internet access)
    # parser = ResolvingParser('http://petstore.swagger.io/v2/swagger.json')
    # resolved_spec = parser.specification
    # print(f"Petstore API Title: {resolved_spec['info']['title']}")

finally:
    # Clean up the dummy spec file
    if os.path.exists('sample_spec.yaml'):
        os.remove('sample_spec.yaml')

view raw JSON →