Skip to content

feat: struct field tags customization#2448

Open
mromaszewicz wants to merge 1 commit into
oapi-codegen:mainfrom
mromaszewicz:feat/configurable-struct-tags
Open

feat: struct field tags customization#2448
mromaszewicz wants to merge 1 commit into
oapi-codegen:mainfrom
mromaszewicz:feat/configurable-struct-tags

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Adds an output-options.struct-tags configuration that makes struct tag generation user-configurable instead of hardcoded. Each entry is a tag name plus a Go text/template rendered against per-field data (.FieldName, .IsOptional, .OmitEmpty, .OmitZero, .NeedsFormTag); entries merge by name on top of the defaults, so users can override the built-in json/form templates or add new tags (yaml, db, validate, ...). A template that renders empty suppresses the tag.

The default templates reproduce the existing hardcoded output byte-for-byte, so generation is unchanged unless struct-tags is set. The legacy yaml-tags flag is superseded: an explicit yaml entry in struct-tags takes precedence over it. Extension-driven behavior (x-go-json-ignore, x-oapi-codegen-extra-tags) is applied on top of the rendered tags as before. Invalid templates are reported as configuration errors at validation/generation time.

Partially addresses #2368.

@mromaszewicz
mromaszewicz requested a review from a team as a code owner July 10, 2026 15:11
@mromaszewicz mromaszewicz added the enhancement New feature or request label Jul 10, 2026
@greptile-apps

greptile-apps Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR replaces the hardcoded struct-tag assembly in GenFieldsFromProperties and ParameterDefinition.JsonTag with a user-configurable output-options.struct-tags system backed by Go text/template. Default templates reproduce the historical output byte-for-byte so the change is backward-compatible; yaml-tags: true continues to work and is superseded when an explicit yaml entry appears in struct-tags.

  • New structtags.go introduces StructTagsConfig, StructTagTemplate, StructTagInfo, and structTagGenerator. Templates are validated at construction time against all 16 boolean-flag combinations so misconfigured templates surface as errors rather than silently dropping tags during generation.
  • configuration.go and configuration-schema.json are updated in sync, and OutputOptions.Validate() eagerly parses user templates so config errors are reported before any code is generated.
  • New internal/test/options/struct_tags/ integration test (correctly placed under options/) verifies the yaml-override, validate/db additions, empty-render suppression, and form-tag gating end-to-end against a committed generated file.

Confidence Score: 5/5

Safe to merge; the change is fully opt-in, default output is unchanged, and template errors are caught at configuration time.

The new struct-tag generation path is thoroughly tested, backward-compatible by design, and validated eagerly so invalid user config surfaces before any code is written. No changes to generated function/method signatures or wire-format JSON tags are introduced unconditionally.

No files require special attention; the JSON schema, configuration struct, and implementation are kept in sync.

Important Files Changed

Filename Overview
pkg/codegen/structtags.go New file implementing the configurable struct-tag system. Template validation probes exhaustively cover all 16 boolean-flag combinations, silently-skipping was replaced with a stderr warning, and the merge logic correctly supersedes the legacy yaml-tags flag. Minor misleading comments about where alphabetical ordering is enforced.
pkg/codegen/configuration.go Adds StructTagsConfig field and updates Validate() to eagerly parse user templates; JSON schema is updated in lock-step.
pkg/codegen/schema.go Replaces hardcoded json/yaml/form tag assembly with schemaFieldTagGenerator().generateTagsMap(); default templates reproduce the historical output byte-for-byte.
pkg/codegen/operations.go JsonTag() now delegates to paramFieldTagGenerator(), extending struct-tag customization to strict RequestObject parameter fields while preserving previous behavior.
pkg/codegen/codegen.go Eagerly builds both structTagGenerator instances in Generate() so invalid user templates surface as errors rather than being silently skipped.
pkg/codegen/structtags_test.go Comprehensive unit tests covering default config, merge semantics, parse/render errors including conditional branches, generateTagsMap output, and Validate() integration.
internal/test/options/struct_tags/struct_tags.gen.go Generated file correctly shows db/json/validate/yaml tags alphabetically; form tag gated on NeedsFormTag; validate suppressed on optional fields.
configuration-schema.json Schema updated in sync with configuration.go changes; struct-tags object and yaml-tags supersession documented correctly.
internal/test/options/struct_tags/struct_tags_test.go Integration tests verify json default retention, yaml-tags flag supersession, custom tag addition, empty-render suppression, and form-tag gating.
internal/test/options/struct_tags/config.yaml New test fixture under the correct options/ category (not issue-numbered); exercises yaml override and validate/db additions with yaml-tags: true coexisting.

Reviews (2): Last reviewed commit: "feat: struct field tags customization" | Re-trigger Greptile

Comment thread pkg/codegen/structtags.go
Comment thread pkg/codegen/structtags.go
@mromaszewicz mromaszewicz added the notable changes Used for release notes to highlight these more highly label Jul 10, 2026
Adds an output-options.struct-tags configuration that makes struct tag
generation user-configurable instead of hardcoded. Each entry is a tag
name plus a Go text/template rendered against per-field data
(.FieldName, .IsOptional, .OmitEmpty, .OmitZero, .NeedsFormTag);
entries merge by name on top of the defaults, so users can override
the built-in json/form templates or add new tags (yaml, db,
validate, ...). A template that renders empty suppresses the tag.

The default templates reproduce the existing hardcoded output
byte-for-byte, so generation is unchanged unless struct-tags is set.
The legacy yaml-tags flag is superseded: an explicit yaml entry in
struct-tags takes precedence over it. Extension-driven behavior
(x-go-json-ignore, x-oapi-codegen-extra-tags) is applied on top of
the rendered tags as before. Invalid templates are reported as
configuration errors at validation/generation time.

Partially addresses oapi-codegen#2368.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@mromaszewicz
mromaszewicz force-pushed the feat/configurable-struct-tags branch from b6ad025 to 0c9a489 Compare July 10, 2026 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request notable changes Used for release notes to highlight these more highly

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant