Skip to content

Commit 0de26ab

Browse files
committed
Fix inline json tag detection
1 parent 73b2175 commit 0de26ab

5 files changed

Lines changed: 41 additions & 3 deletions

File tree

pkg/analysis/helpers/extractjsontags/analyzer.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ func run(pass *analysis.Pass) (any, error) {
9191
return
9292
}
9393

94-
results.insertFieldTagInfo(field, extractTagInfo(field.Tag))
94+
results.insertFieldTagInfo(field, extractTagInfo(field, field.Tag))
9595
})
9696

9797
return results, nil
9898
}
9999

100100
//nolint:cyclop
101-
func extractTagInfo(tag *ast.BasicLit) FieldTagInfo {
101+
func extractTagInfo(field *ast.Field, tag *ast.BasicLit) FieldTagInfo {
102102
if tag == nil || tag.Value == "" {
103103
return FieldTagInfo{Missing: true}
104104
}
@@ -115,6 +115,12 @@ func extractTagInfo(tag *ast.BasicLit) FieldTagInfo {
115115
}
116116

117117
if tagValue == "" {
118+
if field.Names == nil { // Embedded field with `json:""`
119+
emptyJsonTagPrefix := `json:"`
120+
pos := tag.Pos() + token.Pos(strings.Index(tag.Value, emptyJsonTagPrefix)+len(emptyJsonTagPrefix))
121+
end := pos + token.Pos(1)
122+
return FieldTagInfo{Inline: true, RawValue: "", Pos: pos, End: end}
123+
}
118124
return FieldTagInfo{}
119125
}
120126

@@ -169,7 +175,7 @@ type FieldTagInfo struct {
169175
// OmitZero is true if the field has the omitzero option in the json tag.
170176
OmitZero bool
171177

172-
// Inline is true if the field has the inline option in the json tag.
178+
// Inline is true if the json tag is ",inline", or if the field is embedded and the json tag is "".
173179
Inline bool
174180

175181
// Missing is true when the field had no json tag.

pkg/analysis/nonpointerstructs/testdata/src/a/a.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ type A struct {
4545
WithOptionalField `json:",inline"`
4646
WithRequiredAndOptionalField `json:",inline"`
4747
WithOptionalFieldsAndMinProperties `json:",inline"`
48+
WithRequiredFieldEmbedded `json:""`
49+
WithOptionalFieldEmbedded `json:""`
4850
}
4951

5052
type WithRequiredField struct {
@@ -69,3 +71,13 @@ type WithOptionalFieldsAndMinProperties struct {
6971
// +k8s:optional
7072
OptionalField string `json:"optionalField"`
7173
}
74+
75+
type WithRequiredFieldEmbedded struct {
76+
// +required
77+
RequiredField string `json:"requiredField"`
78+
}
79+
80+
type WithOptionalFieldEmbedded struct {
81+
// +optional
82+
OptionalField string `json:"optionalField"`
83+
}

pkg/analysis/nonpointerstructs/testdata/src/a/a.go.golden

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ type A struct {
4949
WithOptionalField `json:",inline"`
5050
WithRequiredAndOptionalField `json:",inline"`
5151
WithOptionalFieldsAndMinProperties `json:",inline"`
52+
WithRequiredFieldEmbedded `json:""`
53+
WithOptionalFieldEmbedded `json:""`
5254
}
5355

5456
type WithRequiredField struct {
@@ -73,3 +75,13 @@ type WithOptionalFieldsAndMinProperties struct {
7375
// +k8s:optional
7476
OptionalField string `json:"optionalField"`
7577
}
78+
79+
type WithRequiredFieldEmbedded struct {
80+
// +required
81+
RequiredField string `json:"requiredField"`
82+
}
83+
84+
type WithOptionalFieldEmbedded struct {
85+
// +optional
86+
OptionalField string `json:"optionalField"`
87+
}

pkg/analysis/optionalorrequired/testdata/src/a/a.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ type OptionalOrRequiredTestStruct struct {
8888

8989
// +optional
9090
C `json:"c,omitempty"`
91+
92+
Embedded `json:""`
9193
}
9294

9395
type A struct{}
@@ -102,3 +104,5 @@ type C struct{}
102104
type Interface interface {
103105
InaccessibleFunction() string
104106
}
107+
108+
type Embedded struct{}

pkg/analysis/optionalorrequired/testdata/src/a/a.go.golden

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ type OptionalOrRequiredTestStruct struct {
8383

8484
// +optional
8585
C `json:"c,omitempty"`
86+
87+
Embedded `json:""`
8688
}
8789

8890
type A struct{}
@@ -97,3 +99,5 @@ type C struct{}
9799
type Interface interface {
98100
InaccessibleFunction() string
99101
}
102+
103+
type Embedded struct{}

0 commit comments

Comments
 (0)