Skip to content

Commit 2886ae7

Browse files
fredbiclaude
andcommitted
fix(schema): stop reporting a json-named embed's classifier as ignored
An embed carrying an explicit json name is not a promotion: it nests as a single named property built from the embedded type, and that path does consult `swagger:strfmt` / `swagger:type`. The ineffective-annotation warning fired for every annotated embed regardless, so an author naming an embed was told the annotation had been dropped while it was in fact being applied. The warning now sits on the two arms that really do discard it — the allOf member and the promoting plain embed — instead of ahead of the split. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 7a71976 commit 2886ae7

4 files changed

Lines changed: 46 additions & 3 deletions

File tree

‎fixtures/enhancements/annotation-noise/types.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ type IneffectiveOnPlain struct {
4545
Note string `json:"note"`
4646
}
4747

48+
// EffectiveOnNamedEmbed gives the embed a json name, which makes it a single
49+
// named property rather than a promotion — so the classifier IS consulted,
50+
// exactly as on a regular field. Reporting it as ineffective was a false alarm.
51+
//
52+
// swagger:model EffectiveOnNamedEmbed
53+
type EffectiveOnNamedEmbed struct {
54+
// swagger:strfmt uuid
55+
Target `json:"nested"`
56+
57+
// Note is the embedding struct's own field.
58+
Note string `json:"note"`
59+
}
60+
4861
// EffectiveOnField is the control: the same annotations on regular fields, where
4962
// both are honoured.
5063
//

‎fixtures/integration/golden/enhancements_annotation_noise.json‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@
2020
},
2121
"x-go-package": "github.com/go-openapi/codescan/fixtures/enhancements/annotation-noise"
2222
},
23+
"EffectiveOnNamedEmbed": {
24+
"description": "EffectiveOnNamedEmbed gives the embed a json name, which makes it a single\nnamed property rather than a promotion — so the classifier IS consulted,\nexactly as on a regular field. Reporting it as ineffective was a false alarm.",
25+
"type": "object",
26+
"properties": {
27+
"nested": {
28+
"type": "string",
29+
"format": "uuid",
30+
"x-go-name": "Target"
31+
},
32+
"note": {
33+
"description": "Note is the embedding struct's own field.",
34+
"type": "string",
35+
"x-go-name": "Note"
36+
}
37+
},
38+
"x-go-package": "github.com/go-openapi/codescan/fixtures/enhancements/annotation-noise"
39+
},
2340
"IneffectiveOnAllOf": {
2441
"title": "IneffectiveOnAllOf annotates an allOf embed with classifiers the arm ignores.",
2542
"allOf": [

‎internal/builders/schema/allof.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ func (s *Builder) scanEmbeddedFields(
4848
if fd.Ignored {
4949
continue
5050
}
51-
s.warnIneffectiveEmbedAnnotations(afld, fd)
52-
5351
_, ignore, isString, omitEmpty, err := resolvers.ParseFieldTag(afld, fld.Name(), s.Ctx.NameFromTags())
5452
if err != nil {
5553
return nil, false, err
@@ -86,6 +84,7 @@ func (s *Builder) scanEmbeddedFields(
8684
}
8785

8886
hasAllOf = true
87+
s.warnIneffectiveEmbedAnnotations(afld, fd)
8988
if target == nil {
9089
target = &oaispec.Schema{}
9190
}
@@ -169,6 +168,10 @@ func (s *Builder) buildPlainEmbed(
169168
return target, err
170169
}
171170

171+
// Past this point the embed genuinely promotes, and no arm of the promotion walk consults the
172+
// embed's own comment — so a classifier written there is dropped and must be reported.
173+
s.warnIneffectiveEmbedAnnotations(afld, fd)
174+
172175
// A `required:` annotation on the embed applies to the properties it promotes (go-swagger#2701).
173176
// Thread it through the recursion, restoring afterwards so sibling fields are unaffected.
174177
saved := s.embedInherited

‎internal/integration/annotation_noise_test.go‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,17 @@ func TestAnnotationNoise(t *testing.T) {
6161
n++
6262
}
6363
}
64-
assert.Equal(t, 2, n, "one report per annotated embed — the allOf one and the plain one")
64+
// Three embeds in the fixture carry a classifier; only the two that discard it are reported.
65+
assert.Equal(t, 2, n, "one report per DISCARDING embed — the allOf one and the promoting one")
66+
})
67+
68+
t.Run("a json-named embed is honoured, so it is not reported", func(t *testing.T) {
69+
// Naming an embed with a json tag makes it a single named property instead of a promotion, and
70+
// that path DOES consult the classifier. Reporting it was a false alarm: the author was told an
71+
// annotation had been dropped while it was being applied. The count above is what pins it.
72+
props := doc.Definitions["EffectiveOnNamedEmbed"].Properties
73+
assert.Equal(t, "string/uuid", schemaSignature(props["nested"], doc.Definitions, 0),
74+
"the classifier must reach a json-named embed")
6575
})
6676

6777
t.Run("the annotations are still ignored, not applied", func(t *testing.T) {

0 commit comments

Comments
 (0)
⌂ 换网址