OpenAPI Commands

Azu CLI provides comprehensive OpenAPI 3.1 support for both generating code from OpenAPI specifications and exporting OpenAPI specifications from your Crystal code.

Overview

The OpenAPI commands enable bidirectional integration between OpenAPI specifications and Azu applications:

  • Generate code from OpenAPI specifications

  • Export specifications from existing Azu code

  • Support OpenAPI 3.1 standard

  • Type-safe integration with Crystal

Available Commands

Command
Description

azu openapi:generate

Generate Crystal code from OpenAPI specification

azu openapi:export

Export OpenAPI specification from Crystal code

azu openapi:generate

Generates Crystal code from an OpenAPI 3.1 specification file, creating models, endpoints, requests, and responses.

Basic Usage

# Generate all code from OpenAPI spec
azu openapi:generate openapi.yaml

# Generate only models
azu openapi:generate openapi.yaml --models-only

# Generate only endpoints
azu openapi:generate openapi.yaml --endpoints-only

# Force overwrite existing files
azu openapi:generate openapi.yaml --force

Options

Option
Description
Default

--spec PATH

Path to OpenAPI specification file

Required

--force

Overwrite existing files without prompting

false

--models-only

Generate only models from schemas

false

--endpoints-only

Generate only endpoints from paths

false

Examples

Generate Complete API

Generate Specific Components

Force Overwrite

Generated Code Structure

The generator creates the following files based on your OpenAPI specification:

Generated Model Example

Generated Endpoint Example

Generated Request Example

Generated Response Example

Supported OpenAPI Features

Schemas (Component Models)

  • Basic types: string, integer, number, boolean

  • Complex types: object, array

  • Validation: required fields, format patterns, length constraints

  • Nested objects: automatically handled

  • Enums: converted to Crystal enums

Paths (Endpoints)

  • HTTP methods: GET, POST, PUT, PATCH, DELETE

  • Path parameters: automatically extracted

  • Query parameters: converted to request validation

  • Request bodies: generated as request classes

  • Responses: generated as page classes

Request/Response Bodies

  • JSON schemas: converted to Crystal structs

  • Validation rules: mapped to Azu validation

  • Nested objects: properly handled

  • Arrays: converted to Crystal arrays

OpenAPI Specification Example

azu openapi:export

Exports an OpenAPI 3.1 specification from your existing Azu application code.

Basic Usage

Options

Option
Description
Default

--output PATH

Output file path

openapi.yaml

--format FORMAT

Output format (yaml, json)

yaml

--project NAME

Project name

Auto-detected from shard.yml

--version VERSION

API version

1.0.0

Examples

Export to YAML

Export to JSON

Custom Project Information

Code Analysis

The export command analyzes your Azu application and extracts:

Endpoints Analysis

  • HTTP methods from endpoint classes

  • Path patterns from route definitions

  • Request parameters from method signatures

  • Response types from return values

Models Analysis

  • Schema definitions from CQL models

  • Field types and constraints

  • Validation rules and formats

  • Relationships between models

Request/Response Analysis

  • Request classes and their fields

  • Response classes and their structure

  • Validation rules and constraints

  • Content types and formats

Generated OpenAPI Specification

The exported specification includes:

Workflow Integration

API-First Development

  1. Design API using OpenAPI specification

  2. Generate code from specification

  3. Implement business logic in generated files

  4. Export updated spec as you develop

Code-First Development

  1. Develop application with Azu endpoints and models

  2. Export specification from existing code

  3. Share specification with frontend/API consumers

  4. Generate client code from specification

Continuous Integration

Best Practices

1. Specification Management

2. Code Generation

3. Validation

4. Documentation

Troubleshooting

Common Issues

Invalid OpenAPI Specification

Generation Failures

Export Issues

File Permission Issues

Memory Issues

Integration Examples

Frontend Integration

API Documentation

Testing Integration


The OpenAPI commands provide powerful integration between OpenAPI specifications and Azu applications, enabling both API-first and code-first development workflows.

Next Steps:

Last updated