Skip to content

Conversation

simonjbeaumont
Copy link
Collaborator

Motivation

Users want the ability to add custom comments to generated files. One concrete use case is adding directives like swift-format-ignore-file and swiftlint:disable all to prevent these tools from processing generated code.

Modifications

  • Added additionalFileComments property to Config and UserConfig structs
  • Modified FileTranslator to include additional comments along with the do-not-edit comment
  • Added CLI support with --additional-file-comment option
  • Added config tests to confirm the option propagates and the default is empty
  • Added snippet tests to validate the actual rendering
  • Updated documentation with examples and usage instructions

Result

Users can now configure additional comments to be added to generated files using either the config file or a command line option.

Fixes #738.

Test plan

  • Added unit tests for Config
  • Added snippet tests for rendering
  • Manual tests of generator using config and CLI on real OpenAPI doc:
% swift run swift-openapi-generator generate openapi-documents/petstore.yaml \
  --mode types \
  --output-directory test-output-cli \
  --additional-file-comment "hello world" \
  --additional-file-comment "testing, testing, 1, 2, 3"
Build of product 'swift-openapi-generator' complete! (3.27s)
Swift OpenAPI Generator is running with the following configuration:
- OpenAPI document path: /Users/Si/work/code/swift-openapi-workspace/packages/swift-openapi-generator/openapi-documents/petstore.yaml
- Configuration path: <none>
- Generator modes: types
- Access modifier: internal
- Naming strategy: defensive
- Name overrides: <none>
- Feature flags: <none>
- Output file names: Types.swift
- Output directory: /Users/Si/work/code/swift-openapi-workspace/packages/swift-openapi-generator/test-output-cli
- Diagnostics output path: <none - logs to stderr>
- Current directory: /Users/Si/work/code/swift-openapi-workspace/packages/swift-openapi-generator
- Plugin source: <none>
- Is dry run: false
- Additional imports: <none>
- Additional file comments: hello world, testing, testing, 1, 2, 3
Writing data to file Types.swift...

% head -5 test-output-cli/Types.swift
// Generated by swift-openapi-generator, do not modify.
// hello world
// testing, testing, 1, 2, 3
@_spi(Generated) import OpenAPIRuntime

Users want the ability to add custom comments to generated files. One
concrete use case is adding directives like `swift-format-ignore-file`
and `swiftlint:disable all` to prevent these tools from processing
generated code.

- Added `additionalFileComments` property to `Config` and `UserConfig` structs
- Modified `FileTranslator` to include additional comments along with the do-not-edit comment
- Added CLI support with `--additional-file-comment` option
- Added config tests to confirm the option propagates and the default is empty
- Added snippet tests to validate the actual rendering
- Updated documentation with examples and usage instructions

Users can now configure additional comments to be added to generated
files using either the config file or a command line option.

Fixes apple#738.

- Added unit tests for `Config`
- Added snippet tests for rendering
- Manual tests of generator using config and CLI on real OpenAPI doc:

```console
% swift run swift-openapi-generator generate openapi-documents/petstore.yaml \
  --mode types \
  --output-directory test-output-cli \
  --additional-file-comment "hello world" \
  --additional-file-comment "testing, testing, 1, 2, 3"
Build of product 'swift-openapi-generator' complete! (3.27s)
Swift OpenAPI Generator is running with the following configuration:
- OpenAPI document path: /Users/Si/work/code/swift-openapi-workspace/packages/swift-openapi-generator/openapi-documents/petstore.yaml
- Configuration path: <none>
- Generator modes: types
- Access modifier: internal
- Naming strategy: defensive
- Name overrides: <none>
- Feature flags: <none>
- Output file names: Types.swift
- Output directory: /Users/Si/work/code/swift-openapi-workspace/packages/swift-openapi-generator/test-output-cli
- Diagnostics output path: <none - logs to stderr>
- Current directory: /Users/Si/work/code/swift-openapi-workspace/packages/swift-openapi-generator
- Plugin source: <none>
- Is dry run: false
- Additional imports: <none>
- Additional file comments: hello world, testing, testing, 1, 2, 3
Writing data to file Types.swift...

% head -5 test-output-cli/Types.swift
// Generated by swift-openapi-generator, do not modify.
// hello world
// testing, testing, 1, 2, 3
@_spi(Generated) import OpenAPIRuntime
```
@simonjbeaumont simonjbeaumont added the 🆕 semver/minor Adds new public API. label May 23, 2025
@simonjbeaumont simonjbeaumont requested a review from czechboy0 May 23, 2025 15:01
@czechboy0
Copy link
Contributor

Copy link
Contributor

@czechboy0 czechboy0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry missed that you had already added the docs - lgtm!

@simonjbeaumont simonjbeaumont merged commit 09ed6c4 into apple:main May 23, 2025
30 of 36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🆕 semver/minor Adds new public API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make generated files // swift-format-ignore
2 participants