OpenapiParser

CIHex.pmDocumentation

Note: This library was primarily created with AI assistance. As with any external library, please use it with caution and verify its behavior in your specific use case before deploying to production.

A comprehensive OpenAPI specification parser for Elixir that supports:

Both JSON and YAML formats are supported with full validation.

Features

Installation

Add openapi_parser to your list of dependencies in mix.exs:

def deps do
  [
    {:openapi_parser, "~> 0.2.0"}
  ]
end

Then run:

mix deps.get

Quick Start

# Parse from a string
{:ok, spec} = OpenapiParser.parse(json_string, format: :json)
{:ok, spec} = OpenapiParser.parse(yaml_string, format: :yaml)

# Parse from a file (auto-detects format)
{:ok, spec} = OpenapiParser.parse_file("openapi.json")
{:ok, spec} = OpenapiParser.parse_file("swagger.yaml")

# Access parsed data
IO.puts(spec.document.info.title)
IO.puts(spec.document.info.version)

Parsing Options

# Parse without validation (faster)
{:ok, spec} = OpenapiParser.parse(content, validate: false)

# Parse with reference resolution
{:ok, spec} = OpenapiParser.parse_file("spec.yaml", resolve_refs: true)

Supported Specifications

OpenAPI 3.1

OpenAPI 3.0

Swagger 2.0

Error Handling

case OpenapiParser.parse(invalid_spec, validate: true) do
  {:ok, spec} ->
    IO.puts("Valid spec!")

  {:error, message} ->
    IO.puts("Validation error: #{message}")
end

Resources

License

This project is licensed under the MIT License - see the LICENSE file for details.