Add multi-file code generation support (--multiple-files) - #73
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: ⛔ Files ignored due to path filters (14)
📒 Files selected for processing (5)
🚧 Files skipped from review as they are similar to previous changes (4)
📝 WalkthroughWalkthroughThe CLI adds ChangesMulti-file generation
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant CLI
participant Generator
participant RuntimeGenerator
participant UnifiedApiGenerator
participant OutputDirectory
CLI->>Generator: select --multiple-files
Generator->>RuntimeGenerator: generate runtime source
Generator->>UnifiedApiGenerator: generate client-only source
Generator->>OutputDirectory: write models.zig, runtime.zig, client.zig
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
🧹 Nitpick comments (3)
src/cli.zig (1)
193-206: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winConsider adding a parser test for
--multiple-files.Existing tests cover
--models-only/defaults/upgradebut not the new flag.test "parse generate supports multiple-files flag" { const argv = [_][:0]const u8{ "openapi2zig", "generate", "-i", "openapi.json", "--multiple-files", }; const parsed = try parse(&argv); try std.testing.expect(parsed.args.multiple_files); }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/cli.zig` around lines 193 - 206, Add a parser test alongside the existing generate argument tests that invokes parse with the --multiple-files flag and asserts parsed.args.multiple_files is true, using the same input-path setup and test style as the models-only case.build.zig (1)
241-249: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick winNo compile verification for multi-file generated output.
generated_testsonly compilesgenerated/compile_generated.zig(single-file mode). Consider adding a similar test target that importsgenerated/multi/models.zig,runtime.zig, andclient.zigso cross-file breakage (like thepubvisibility issue flagged inruntime_generator.zig) is caught in CI.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@build.zig` around lines 241 - 249, Add a second generated-output test target alongside generated_tests in the build configuration, using a root module that imports generated/multi/models.zig, runtime.zig, and client.zig so multi-file generated code is compiled and cross-file visibility errors are caught. Register its run artifact with test_step, while preserving the existing single-file generated_tests coverage.src/generators/unified/runtime_generator.zig (1)
33-97: 📐 Maintainability & Code Quality | 🔵 Trivial | 🏗️ Heavy liftRuntime template strings duplicated with
api_generator.zig.
generateRuntimeTypes,generateHttpObserverType,generateSseFunctions, andgenerateSseBufferConstantshere are near-verbatim copies ofgenerateRuntimePreamble/generateHttpObserverType/generateSsePreamble/generateSseBufferConstantsinapi_generator.zig(used for single-file mode). Any future fix/change to the runtime code (e.g. the SSE parser) must be applied in both places, and they've already drifted slightly (e.g. leading blank-line differences). Consider extracting the shared template text into a common helper both generators call.Also applies to: 99-109, 111-230
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/generators/unified/runtime_generator.zig` around lines 33 - 97, Remove the duplicated runtime template emission from generateRuntimeTypes, generateHttpObserverType, generateSseFunctions, and generateSseBufferConstants, and extract the shared template text into a common helper reusable by both RuntimeGenerator and api_generator’s corresponding generation methods. Preserve the generated runtime contents and eliminate formatting drift between single-file and runtime generation paths.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/generators/unified/runtime_generator.zig`:
- Around line 129-215: Make checkCancellation and TypedSseCallback public in
runtime_generator.zig by changing both declarations to pub fn, allowing
generated consumers to access them across file boundaries. In api_generator.zig
lines 253-269, make no direct changes; its existing reexports should resolve
once the runtime symbols are public.
- Around line 129-133: In the generated runtime declarations, change both
checkCancellation and TypedSseCallback from non-public functions to pub
functions so client.zig can reexport and use them in --multiple-files mode;
leave their signatures and behavior unchanged.
In `@src/lib.zig`:
- Around line 251-288: Replace the success-inactive errdefer cleanup for the
intermediate buffers in generateCodeMultiple—models_code, header, runtime_code,
and api_code—with defer cleanup. Keep the concatenated result buffers owned by
the returned GeneratedFiles value, while ensuring each source buffer is freed
after successful generation and on errors.
- Around line 241-246: Update the GeneratedFiles struct to add a
deinit(allocator) method that frees each owned buffer: models, runtime when
present, and client when present. Use the provided allocator and preserve the
optional fields’ null-safe cleanup behavior.
---
Nitpick comments:
In `@build.zig`:
- Around line 241-249: Add a second generated-output test target alongside
generated_tests in the build configuration, using a root module that imports
generated/multi/models.zig, runtime.zig, and client.zig so multi-file generated
code is compiled and cross-file visibility errors are caught. Register its run
artifact with test_step, while preserving the existing single-file
generated_tests coverage.
In `@src/cli.zig`:
- Around line 193-206: Add a parser test alongside the existing generate
argument tests that invokes parse with the --multiple-files flag and asserts
parsed.args.multiple_files is true, using the same input-path setup and test
style as the models-only case.
In `@src/generators/unified/runtime_generator.zig`:
- Around line 33-97: Remove the duplicated runtime template emission from
generateRuntimeTypes, generateHttpObserverType, generateSseFunctions, and
generateSseBufferConstants, and extract the shared template text into a common
helper reusable by both RuntimeGenerator and api_generator’s corresponding
generation methods. Preserve the generated runtime contents and eliminate
formatting drift between single-file and runtime generation paths.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 0834187f-0ace-4570-8eb9-91620ff648e4
⛔ Files ignored due to path filters (14)
generated/anthropic.zigis excluded by!**/generated/**generated/generated_v2.zigis excluded by!**/generated/**generated/generated_v2_yaml.zigis excluded by!**/generated/**generated/generated_v3.zigis excluded by!**/generated/**generated/generated_v31.zigis excluded by!**/generated/**generated/generated_v31_yaml.zigis excluded by!**/generated/**generated/generated_v32.zigis excluded by!**/generated/**generated/generated_v3_yaml.zigis excluded by!**/generated/**generated/lmstudio.zigis excluded by!**/generated/**generated/main.zigis excluded by!**/generated/**generated/multi/client.zigis excluded by!**/generated/**generated/multi/models.zigis excluded by!**/generated/**generated/multi/runtime.zigis excluded by!**/generated/**generated/openai.zigis excluded by!**/generated/**
📒 Files selected for processing (6)
build.zigsrc/cli.zigsrc/generator.zigsrc/generators/unified/api_generator.zigsrc/generators/unified/runtime_generator.zigsrc/lib.zig
There was a problem hiding this comment.
Pull request overview
This PR adds a --multiple-files generation mode to the Zig OpenAPI client generator, splitting output into models.zig, runtime.zig, and client.zig, and wiring that through both the CLI and library API.
Changes:
- Added
--multiple-filesCLI flag and multi-file writing flow in the generator. - Introduced
RuntimeGeneratorand refactoredUnifiedApiGeneratorto support multi-file clients viamodel_prefixandgenerateClientOnly(). - Updated build/test artifacts to generate and compile-check the multi-file v3 output.
Reviewed changes
Copilot reviewed 6 out of 21 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/lib.zig | Exposes RuntimeGenerator and adds generateCodeMultiple() returning a GeneratedFiles struct. |
| src/generators/unified/runtime_generator.zig | New generator that emits shared runtime helpers (Owned/ApiResult/etc) and SSE parsing helpers. |
| src/generators/unified/api_generator.zig | Adds multi-file header mode, model prefixing, and generateClientOnly() for client-only output. |
| src/generator.zig | Implements CLI multi-file generation path and writes models.zig, runtime.zig, client.zig to an output directory. |
| src/cli.zig | Adds --multiple-files parsing, usage text, and a parse test. |
| build.zig | Adds run-generate-v3-multi step and a compile-only test for multi-file generated output. |
| generated/main.zig | Imports and exercises the multi-file client in the generated sample harness. |
| generated/compile_multi_generated.zig | New test ensuring multi-file generated client compiles via refAllDecls. |
| generated/multi/models.zig | Checked-in example multi-file models output for v3 petstore. |
| generated/multi/runtime.zig | Checked-in example multi-file runtime output. |
| generated/multi/client.zig | Checked-in example multi-file client output. |
| generated/openai.zig | Regenerated output reflecting updated generator structure/header formatting. |
| generated/lmstudio.zig | Regenerated output reflecting updated generator structure/header formatting. |
| generated/anthropic.zig | Regenerated output reflecting updated generator structure/header formatting. |
| generated/generated_v32.zig | Regenerated output reflecting updated generator structure/header formatting. |
| generated/generated_v31.zig | Regenerated output reflecting updated generator structure/header formatting. |
| generated/generated_v31_yaml.zig | Regenerated output reflecting updated generator structure/header formatting. |
| generated/generated_v3.zig | Regenerated output reflecting updated generator structure/header formatting. |
| generated/generated_v3_yaml.zig | Regenerated output reflecting updated generator structure/header formatting. |
| generated/generated_v2.zig | Regenerated output reflecting updated generator structure/header formatting. |
| generated/generated_v2_yaml.zig | Regenerated output reflecting updated generator structure/header formatting. |
Adds the
--multiple-filesCLI flag to generate separate files for models (models.zig), runtime helpers (untime.zig), and the API client (client.zig) into a specified output directory.Key changes:
--multiple-filesCLI flag incli.zigRuntimeGeneratorextracting runtime types (Owned, RawResponse, ApiResult, CancellationToken, SSE parsing) into a separate fileUnifiedApiGeneratorwithmodel_prefixandgenerateHeaderMultifor multi-file outputgenerateCodeMultiplein library API returningGeneratedFilesstructgenerated/main.zigupdated to import and verify multi-file clientun-generate-v3-multiUsage:
This creates
output_dir/models.zig,output_dir/runtime.zig, andoutput_dir/client.zig.Summary by CodeRabbit
Summary
--multiple-filesto generate results into an output directory layout.--multiple-filesand its effect on--output.