Skip to content

Add configurable Swift attributes for generated APIProtocol#915

Closed
farzadshbfn wants to merge 1 commit into
apple:mainfrom
farzadshbfn:feature/add-attributes
Closed

Add configurable Swift attributes for generated APIProtocol#915
farzadshbfn wants to merge 1 commit into
apple:mainfrom
farzadshbfn:feature/add-attributes

Conversation

@farzadshbfn

@farzadshbfn farzadshbfn commented Jun 18, 2026

Copy link
Copy Markdown

Motivation

Generated client and server code often needs Swift attributes on the APIProtocol type and its operation requirements—for example @MainActor for concurrency isolation or macro-driven attributes such as @Mockable. Today there is no supported way to express those attributes in generator configuration, so adopters must post-process generated Types.swift or maintain local forks.

Modifications

  • Add AttributeConfiguration and AttributeDescription to model optional Swift attributes in config.
  • Extend Config, _UserConfig, and CLI GenerateOptions with an attributes field.
  • Decode YAML attributes: using protocol and methods keys (each a list of attribute names or quoted strings with arguments).
  • Extend the structured Swift representation and text renderer to emit attribute declarations.
  • Apply configured attributes when translating APIProtocol and its method requirements in Types.swift; OpenAPI-driven @available(*, deprecated, …) remains unchanged and is still applied from operation metadata.
  • Document that consumers remain responsible for wiring any required dependencies in Package.swift, macro plugins, and additionalImports when attributes reference external types or macros.

Result

Adopters can configure attributes in openapi-generator-config.yaml (or equivalent) and regenerate Types.swift with the requested @ attributes on APIProtocol and each operation requirement, without manual edits to generated output.

Example:

attributes:
  protocol:
    - MainActor
  methods:
    - MainActor

Test Plan

  • Added Test_AttributeConfiguration for YAML decoding of protocol / methods, including quoted attribute arguments.
  • Extended Test_Config to cover the new configuration surface.
  • Added testPaths_withConfiguredAttributes snippet reference test for generated APIProtocol output.
  • Ran swift test --filter OpenAPIGeneratorCoreTests (116 tests, all passing).

Related issues

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.

1 participant