openapi3gen: Add an option to customize generated field names (properties) for structs#1204
Merged
Merged
Conversation
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.
Description
This PR adds customizable schema property naming.
Currently, openapi3gen derives property names from JSON/YAML tags only, which makes it difficult to integrate with frameworks that use different binding tags (
form,query, etc.) while still relying onopenapi3genfor schema generation.The new option allows callers to override the generated property name before it is added to
schema.Propertiesand any customizers are called.This is particularly useful for generators and framework integrations where request binding semantics differ from JSON serialization semantics, such as Echo's
formandxmlbinding tags.Usage example
Changes
Existing behaviour remains unchanged when
CreateFieldNameGeneratoris not provided.Callback receives
defaultName, allowing custom naming rules to preserve names resolved from existing JSON and YAML tags.Embedded Field Handling
While testing the new feature, I noticed that promoted fields from anonymous embedded structs were generated twice.
Anonymous structs without explicit
jsontag are already recursively exanded byappendFields:kin-openapi/openapi3gen/field_info.go
Lines 39 to 48 in 6c01290
Each promoted field was then additionally processed by the dedicated embedded field branch inside
generateWithoutSaving:kin-openapi/openapi3gen/openapi3gen.go
Lines 347 to 361 in 6c01290
Execution subsequently continued through the common field generation path:
kin-openapi/openapi3gen/openapi3gen.go
Lines 377 to 388 in 6c01290
This caused the same field schema and
SchemaCustomizercallbacks to be processed twice.Previously, the duplication was hidden because both writes used the same map key. Field name customization exposed this behavior, so I removed the redundant processing branch from
generateWithoutSavingand added a regression test.