Conversation
…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`.
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
macroAnnotationsconfig key:schemas: attributes per component schema name.*matches every schema; its attributes come first.client: attributes for the generatedClientstruct.Declaration.annotatedcase, rendered as one attribute per line before the declaration, after any doc comment.Attributes are emitted as-is, without validation. Modules they need go in
additionalImports.Result
Clientgets@MyClientMacro. WithoutmacroAnnotations, output is unchanged.Serverand 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.testMacroAnnotationsSchemasandtestMacroAnnotationsClient: end-to-end translation for schemas (struct and enum) andClient.swift testandswift format lint --strictpass locally.