@@ -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,30 @@ func (c *CELTag) Validate() error {
61
63
return nil
62
64
}
63
65
66
+ func isKnownTagCommentKey (key string ) bool {
67
+ split := func (r rune ) bool { return r == ':' || r == '[' }
68
+ commentTags := strings .FieldsFunc (key , split )
69
+ if len (commentTags ) == 0 {
70
+ return false
71
+ }
72
+ _ , ok := tagKeys ()[commentTags [0 ]]
73
+ return ok
74
+ }
75
+
76
+ var tagKeys = sync .OnceValue (func () map [string ]struct {} {
77
+ result := map [string ]struct {}{}
78
+ t := reflect .TypeOf (commentTags {})
79
+ for i := 0 ; i < t .NumField (); i ++ {
80
+ field := t .Field (i )
81
+ if jsonTag := field .Tag .Get ("json" ); jsonTag != "" {
82
+ if key , _ , _ := strings .Cut (jsonTag , "," ); key != "" {
83
+ result [key ] = struct {}{}
84
+ }
85
+ }
86
+ }
87
+ return result
88
+ })
89
+
64
90
// commentTags represents the parsed comment tags for a given type. These types are then used to generate schema validations.
65
91
// These only include the newer prefixed tags. The older tags are still supported,
66
92
// but are not included in this struct. Comment Tags are transformed into a
@@ -385,12 +411,11 @@ func memberWithJSONName(t *types.Type, key string) *types.Member {
385
411
return nil
386
412
}
387
413
388
- // Parses the given comments into a CommentTags type. Validates the parsed comment tags, and returns the result.
414
+ // ParseCommentTags parses the given comments into a CommentTags type. Validates the parsed comment tags, and returns the result.
389
415
// Accepts an optional type to validate against, and a prefix to filter out markers not related to validation.
390
416
// Accepts a prefix to filter out markers not related to validation.
391
417
// Returns any errors encountered while parsing or validating the comment tags.
392
418
func ParseCommentTags (t * types.Type , comments []string , prefix string ) (* spec.Schema , error ) {
393
-
394
419
markers , err := parseMarkers (comments , prefix )
395
420
if err != nil {
396
421
return nil , fmt .Errorf ("failed to parse marker comments: %w" , err )
@@ -610,6 +635,8 @@ func parseMarkers(markerComments []string, prefix string) (map[string]any, error
610
635
611
636
if len (key ) == 0 {
612
637
return nil , fmt .Errorf ("cannot have empty key for marker comment" )
638
+ } else if ! isKnownTagCommentKey (key ) {
639
+ continue
613
640
} else if _ , ok := parseSymbolReference (value , "" ); ok {
614
641
// Skip ref markers
615
642
continue
0 commit comments