Skip to content

Add macroAnnotations config option to attach attributes to generated types - #960

Closed
mackoj wants to merge 2 commits into
apple:mainfrom
mackoj:feat/macro
Closed

mackoj wants to merge 2 commits into
apple:mainfrom
mackoj:feat/macro

Conversation

@mackoj

@mackoj mackoj commented Sep 30, 2026 •

Copy link
Copy Markdown

Motivation

There's no way to put an attribute or macro on a generated type today. The generated files get overwritten on every run, so the only options are post-processing them or forking the generator.

Requested in #383, and discussed on the forums, where adding this was welcomed: https://forums.swift.org/t/customization-and-namespacing-for-swift-openapi-generator-generated-code/81856/4

Addresses #383.

Modifications

  • New macroAnnotations config key:
    • schemas: attributes per component schema name. * matches every schema; its attributes come first.
    • client: attributes for the generated Client struct.
  • New Declaration.annotated case, rendered as one attribute per line before the declaration, after any doc comment.
  • Handle the new case in recursion detection, boxing, and the access modifier accessors.
  • Document the key in "Configuring the generator".

Attributes are emitted as-is, without validation. Modules they need go in additionalImports.

additionalImports:
  - MyMacros
macroAnnotations:
  schemas:
    "*":
      - "@MyMacro"
    Pet:
      - "@MyOtherMacro(option: true)"
  client:
    - "@MyClientMacro"

Result

/// - Remark: Generated from `#/components/schemas/Pet`.
@MyMacro
@MyOtherMacro(option: true)
public struct Pet: Codable, Hashable, Sendable { ... }

Client gets @MyClientMacro. Without macroAnnotations, output is unchanged.

Server and generated methods aren't covered yet; I can add them in a follow-up.

Test Plan

  • Test_TextBasedRenderer.testAnnotated: rendering, including with a doc comment.
  • Test_Config: wildcard and per-schema resolution, attribute placement.
  • SnippetBasedReferenceTests.testMacroAnnotationsSchemas and testMacroAnnotationsClient: end-to-end translation for schemas (struct and enum) and Client.
  • swift test and swift format lint --strict pass locally.

mackoj and others added 2 commits September 30, 2026 22:09
…types

Adds a `macroAnnotations` key to the generator config that emits
user-provided attributes or attached macros before generated declarations:

- `schemas`: per-schema attributes keyed by schema name, with `*` applying
  to every schema (wildcard attributes are emitted first).
- `client`: attributes applied to the generated `Client` struct.

Attributes are emitted verbatim after any doc comment, so modules they
require must be listed in `additionalImports`.
@czechboy0

czechboy0 commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Hi @mackoj,

thanks for getting started on this, but these days we require an issue to be assigned to you before you can open a PR. That's mainly to save you time, ensuring we have alignment before you spend too much time working on the code.

This is definitely a large enough feature that it'll require a proposal (see our docs).

But for now - can we move the discussion to the issue, and dig into what your preferred approach is? One thing I'll say right away - the feature must work both for client and server generation, we wouldn't accept this only for one of them.

@czechboy0 czechboy0 closed this Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants