Support example tag on map[string]Struct fields#2181
Draft
rykpl wants to merge 1 commit into
Draft
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.
Summary
Fields typed as
map[string]SomeStructhave never been able to carry anexampletag:additionalProperties: $refschema is generated, but with no example, so Swagger UI renders an empty{}that tells the reader nothing about the value shape.key:valuecomma parser indefineTypeOfExamplewitharrayType="object", which cannot represent nested struct values and errors out (example value en should format: key:value, or... is unsupported type ...on the recursive value side).This PR makes the
exampletag work on these fields via two opt-in forms, both surfaced through the existingexamplestruct tag. Open since 2019 (#327).Changes
parser.godefineTypeOfExample: when the map value type is itself an object (arrayType="object"), accept eitherPreviously both paths errored.
New
buildExampleFromRef: synthesises an example object from a$refdefinition's properties — pulling each property's ownExample, and for array properties wrapping a single-element slice (recursing into[]SomeStructitem refs, or using the item example for[]primitive). Guards against cycles in mutually-recursive type graphs.field_parser.goComplementSchema: after the normal schema completion, if the field is an inlinemap[string]Struct(object schema with a$refinAdditionalProperties) and the parsed example is a plain string, replace it with{<key>: <synthesised value>}. If nothing can be synthesised, the example is cleared rather than left as a dangling string.Behaviour
Form 1 — Explicit JSON (used verbatim, same as
exampleeverywhere else):Form 2 — Key name + auto-synthesis (value built from the struct's own field examples):
Generates:
Backward compatibility
exampletag is present. Existingmap[string]Structfields with no tag are untouched (still render{}).exampletag for these fields is replaced by meaningful behaviour; no new errors are introduced.map[string]stringandswaggertype:"object,string"paths are unchanged — they never reach thearrayType="object"branch.Known limitation
Named map type aliases are not supported by key-hint synthesis:
A named alias resolves to a
$refbeforeComplementSchemareaches the synthesis block (it takes theIsRefSchemapath), so the inline-map guard never matches. This is a pre-existing architectural constraint affecting all field complementation on named map aliases, not specific to this change. Explicit JSON (Form 1) still works in this case.Testing
parser_test.go—defineTypeOfExamplecases formap[string]Struct: raw-JSON acceptance and plain-string key-hint passthrough;buildExampleFromRefsynthesis including nested arrays.field_parser_test.go— end-to-endComplementSchemacases covering Form 1, Form 2, and the no-tag (unaffected) path.Related issues
exampletag support formap[string]SomeStructfields #2180map[string]interface{}example panic.defineTypeOfExampleerror viaswaggertype:"object,object" example:"key1:{},key2:{}".{}value case in Support map[string]struct{} struct-tag examples #2018; does not addressmap[string]Structfield synthesis.