@@ -549,3 +549,107 @@ func TestBuildOpenAPIDefinitionsForResourceWithExtensionV2Schema(t *testing.T) {
549549 }
550550 assert .Equal (string (expected_json ), string (actual_json ))
551551}
552+
553+ type TestRecursiveType struct {
554+ Value string `json:"value,omitempty"`
555+ Children []TestRecursiveType `json:"children,omitempty"`
556+ }
557+
558+ func makeRecursiveTypeDefinition (depName string ) openapi.OpenAPIDefinition {
559+ schema := spec.Schema {}
560+ schema .Description = "Test recursive type for JSON pointer escaping"
561+ schema .Properties = map [string ]spec.Schema {
562+ "value" : {
563+ SchemaProps : spec.SchemaProps {
564+ Description : "A test value" ,
565+ Type : []string {"string" },
566+ },
567+ },
568+ "children" : {
569+ SchemaProps : spec.SchemaProps {
570+ Type : []string {"array" },
571+ Items : & spec.SchemaOrArray {
572+ Schema : & spec.Schema {
573+ SchemaProps : spec.SchemaProps {
574+ Ref : spec .MustCreateRef ("#/definitions/" + depName ),
575+ },
576+ },
577+ },
578+ },
579+ },
580+ }
581+ return openapi.OpenAPIDefinition {
582+ Schema : schema ,
583+ Dependencies : []string {depName },
584+ }
585+ }
586+
587+ func TestEscapeJsonPointerInDefinitionName (t * testing.T ) {
588+ testCases := []struct {
589+ name string
590+ definitionName string
591+ expectedName string
592+ shouldNotExist string
593+ }{
594+ {
595+ name : "both slash and tilde" ,
596+ definitionName : "io.k8s/api~v1.TestRecursiveType" ,
597+ expectedName : "io.k8s~1api~0v1.TestRecursiveType" ,
598+ shouldNotExist : "io.k8s/api~v1.TestRecursiveType" ,
599+ },
600+ {
601+ name : "only slashes" ,
602+ definitionName : "io.k8s/api/v1.TestRecursiveType" ,
603+ expectedName : "io.k8s~1api~1v1.TestRecursiveType" ,
604+ shouldNotExist : "io.k8s/api/v1.TestRecursiveType" ,
605+ },
606+ {
607+ name : "only tildes" ,
608+ definitionName : "io.k8s~api~v1.TestRecursiveType" ,
609+ expectedName : "io.k8s~0api~0v1.TestRecursiveType" ,
610+ shouldNotExist : "io.k8s~api~v1.TestRecursiveType" ,
611+ },
612+ {
613+ name : "no special characters" ,
614+ definitionName : "io.k8s.api.v1.TestRecursiveType" ,
615+ expectedName : "io.k8s.api.v1.TestRecursiveType" ,
616+ },
617+ }
618+
619+ for _ , tc := range testCases {
620+ t .Run (tc .name , func (t * testing.T ) {
621+ assert := assert .New (t )
622+
623+ config := & openapi.Config {
624+ ProtocolList : []string {"https" },
625+ Info : & spec.Info {
626+ InfoProps : spec.InfoProps {
627+ Title : "TestAPI" ,
628+ Description : "Test API" ,
629+ Version : "v1" ,
630+ },
631+ },
632+ GetDefinitions : func (_ openapi.ReferenceCallback ) map [string ]openapi.OpenAPIDefinition {
633+ return map [string ]openapi.OpenAPIDefinition {
634+ tc .definitionName : makeRecursiveTypeDefinition (tc .definitionName ),
635+ }
636+ },
637+ GetDefinitionName : func (name string ) (string , spec.Extensions ) {
638+ return name , nil
639+ },
640+ }
641+
642+ definitions , err := BuildOpenAPIDefinitionsForResources (config , tc .definitionName )
643+ assert .NoError (err )
644+ assert .NotNil (definitions )
645+
646+ _ , exists := definitions .Definitions [tc .expectedName ]
647+ assert .True (exists , "Definition should exist with escaped name: %s" , tc .expectedName )
648+
649+ if tc .shouldNotExist != "" {
650+ _ , exists = definitions .Definitions [tc .shouldNotExist ]
651+ assert .False (exists , "Definition should not exist with unescaped name: %s" , tc .shouldNotExist )
652+ }
653+ })
654+ }
655+ }
0 commit comments