@@ -20,9 +20,11 @@ import (
20
20
"encoding/json"
21
21
"errors"
22
22
"fmt"
23
+ "reflect"
23
24
"regexp"
24
25
"strconv"
25
26
"strings"
27
+ "sync"
26
28
27
29
"k8s.io/gengo/v2/types"
28
30
openapi "k8s.io/kube-openapi/pkg/common"
@@ -61,6 +63,34 @@ func (c *CELTag) Validate() error {
61
63
return nil
62
64
}
63
65
66
+ // isKnownTagCommentKey returns true if the given key is a known comment tag key.
67
+ // Known keys are identified by the json field tags in the commentTags struct.
68
+ // If the key is a composite key, only the first key part is checked, and is
69
+ // expected to be separated by the remainder of the key by a ':' or '[' delimiter.
70
+ func isKnownTagCommentKey (key string ) bool {
71
+ split := func (r rune ) bool { return r == ':' || r == '[' }
72
+ commentTags := strings .FieldsFunc (key , split )
73
+ if len (commentTags ) == 0 {
74
+ return false
75
+ }
76
+ _ , ok := tagKeys ()[commentTags [0 ]]
77
+ return ok
78
+ }
79
+
80
+ var tagKeys = sync .OnceValue (func () map [string ]struct {} {
81
+ result := map [string ]struct {}{}
82
+ t := reflect .TypeOf (commentTags {})
83
+ for i := 0 ; i < t .NumField (); i ++ {
84
+ field := t .Field (i )
85
+ if jsonTag := field .Tag .Get ("json" ); jsonTag != "" {
86
+ if key , _ , _ := strings .Cut (jsonTag , "," ); key != "" {
87
+ result [key ] = struct {}{}
88
+ }
89
+ }
90
+ }
91
+ return result
92
+ })
93
+
64
94
// commentTags represents the parsed comment tags for a given type. These types are then used to generate schema validations.
65
95
// These only include the newer prefixed tags. The older tags are still supported,
66
96
// but are not included in this struct. Comment Tags are transformed into a
@@ -385,12 +415,11 @@ func memberWithJSONName(t *types.Type, key string) *types.Member {
385
415
return nil
386
416
}
387
417
388
- // Parses the given comments into a CommentTags type. Validates the parsed comment tags, and returns the result.
418
+ // ParseCommentTags parses the given comments into a CommentTags type. Validates the parsed comment tags, and returns the result.
389
419
// Accepts an optional type to validate against, and a prefix to filter out markers not related to validation.
390
420
// Accepts a prefix to filter out markers not related to validation.
391
421
// Returns any errors encountered while parsing or validating the comment tags.
392
422
func ParseCommentTags (t * types.Type , comments []string , prefix string ) (* spec.Schema , error ) {
393
-
394
423
markers , err := parseMarkers (comments , prefix )
395
424
if err != nil {
396
425
return nil , fmt .Errorf ("failed to parse marker comments: %w" , err )
@@ -610,6 +639,8 @@ func parseMarkers(markerComments []string, prefix string) (map[string]any, error
610
639
611
640
if len (key ) == 0 {
612
641
return nil , fmt .Errorf ("cannot have empty key for marker comment" )
642
+ } else if ! isKnownTagCommentKey (key ) {
643
+ continue
613
644
} else if _ , ok := parseSymbolReference (value , "" ); ok {
614
645
// Skip ref markers
615
646
continue
0 commit comments