feat: support override attributes and type placeholders in .swaggo#2148
feat: support override attributes and type placeholders in .swaggo#2148isasmendiagus wants to merge 1 commit into
Conversation
Add structured Override type to replace raw string overrides, enabling nullable, format, and other schema attributes to be specified alongside the type name when overriding generic placeholders (e.g. $T). Key changes: - Override struct with Type, Nullable, and Format fields - matchOverride() for comparing type names against overrides - getOverrideForType() to find matching overrides for a given type - applyOverrideAttrs() to apply nullable/format attributes to schemas - parseOverrides() in gen.go with key:value attribute parsing syntax - Comprehensive tests for all new functionality
8c792a6 to
7bda9a0
Compare
|
@sdghchj could you please check this one out when you have some time? It would be a great addition to the tool, being able to handle generic wrapper types globally is something a lot of codebases need. I tested locally on my project and confirmed it works great. Here's a concrete example of what it unlocks. Context: we use a tri-state optional.Value[T] wrapper for PATCH endpoints (absent / null / present), since Go's JSON marshaller does not distinguish null from absent (undefined) fields. Before, we had to maintain a parallel // PatchRequestPayloadDoc is a doc-only type used to generate correct swagger output.
// It mirrors PatchRequestPayload but replaces optional.Value[T] with *T.
type PatchRequestPayloadDoc struct {
Date *int64 `json:"date"`
Status *domain.Status `json:"status"`
} // @name PatchRequestPayload
type PatchRequestPayload struct {
Date optional.Value[int64] `json:"date,omitempty"`
Status optional.Value[domain.Status] `json:"status,omitempty"`
}
...
// @Param body body PatchRequestPayloadDoc true "Patch payload"After, with a single line in The real struct is used directly in the annotation, no duplicate needed: type PatchRequestPayload struct {
Date optional.Value[int64] `json:"date,omitempty"`
Status optional.Value[domain.Status] `json:"status,omitempty"`
}
...
// @Param body body PatchRequestPayload true "Patch payload"Generated output renders each field as its inner type with |
|
Any update ? We are also looking into using optionals with swag. |
|
^ ditto. This would be a real boon for |
|
Hi @sdghchj, could you please take a look ? |
Summary
key:valueattribute syntax to.swaggoreplacedirectives (nullable, optional, format)$Ttype parameter placeholder for generic catch-all overridesOverridestruct to replace raw string type mappingsCloses #2147
Closes #1852
Changes
parser.go—Overridestruct,matchOverride(),getOverrideForType(),applyOverrideAttrs(), updatedgetTypeSchema()andparseStructField()gen/gen.go—parseOverrides()withkey:valueattribute parsingparser_test.go— new tests for nullable, format, placeholder, andmatchOverridegen/gen_test.go— new test cases for attrs, format, malformed input, and placeholdergenerics_test.go— updated to useOverridestructUsage