$ show apple/swift-openapi-generator
This is a Swift code generator that produces type-safe client and server code from OpenAPI documents (versions 3.0, 3.1, and preliminary 3.2). It is distributed as a Swift Package Manager build plugin, a command plugin, and a standalone CLI tool (`swift-openapi-generator`). The core architecture follows a pipeline: parse the OpenAPI document using YamsParser, translate the parsed representation into a structured Swift intermediate representation (IR) via a multiplex translator, and render the IR into Swift source files using a text-based renderer. The generated code is decoupled from specific HTTP transport libraries via `ClientTransport` and `ServerTransport` protocols, and supports middleware for cross-cutting concerns like logging, authentication, retrying, metrics, and tracing. The repository includes extensive examples covering a wide range of use cases: basic clients and servers, event streaming, multipart content, database integrations, and middleware stacks. The test suite is comprehensive, including unit tests, golden-file reference tests, and integration tests that compile and run generated code against mock transports. The project is actively maintained with a proposal process (SOAR) for feature additions and has a well-documented ecosystem of separate runtime and transport libraries.
The LLM identified 5 files worth highlighting for reuse:
Defines the main generator pipeline (`GeneratorPipeline`) with a chain of stages: parsing, translation, and rendering. Each stage conforms to `GeneratorPipelineStage` and supports pre/post hooks. This is a clean, reusable pipeline pattern that can be extracted to orchestrate any multi-step transformation process. The `runGenerator` function is the public entry point, taking input document data, configuration, and a diagnostics collector.
Implements `TypeAssigner` which maps OpenAPI schema components to unique Swift type names, handling collision avoidance, recursive type boxing, and type usage tracking. It works with `TypeMatcher` and `SafeNameGenerator` (defensive and idiomatic naming strategies). This component is isolated and could be reused to generate type assignments for any code generation project that needs to produce unique, safe identifiers from a schema.
Provides a `ContentType` enum that categorizes MIME types (JSON, binary, urlEncodedForm, multipart, text, other) and extracts structured information like content type substrate. It includes parsing logic for OpenAPI content type strings and a `bestContentType` function to select the best match. This is a self-contained utility that could be extracted for any project needing to parse and compare HTTP content types.
Contains shared utility functions for Swift Package Manager plugins, including reading configuration files, locating the generated sources directory, and error handling. It demonstrates a clean, reusable pattern for building SPM plugins that need to interact with the file system and package target structure. Could be adapted for other code-generation plugins.
Implements a generic `RecursionDetector` that uses graph traversal to detect cycles in a tree of `TypeNode` instances. It is used to identify recursive types that need boxing. The component is generic over a `TypeNode` protocol and could be reused for any scenario requiring cycle detection in a directed graph, such as dependency resolution or schema validation.