Skip to content

Commit 38d378f

Browse files
committed
update and add tests
1 parent 2495e4c commit 38d378f

File tree

2 files changed

+242
-20
lines changed

2 files changed

+242
-20
lines changed

tools/cli/internal/openapi/filter/hidden_envs.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,39 +72,37 @@ func (f *HiddenEnvsFilter) removeSchemaIfHiddenForEnv(name string, schema *opena
7272
return nil
7373
}
7474

75-
// if name == "ApiSearchDeploymentResponseView" {
76-
// log.Printf("Schema: %s", schema)
77-
// log.Printf("Schema: %s", schema.Value)
78-
// log.Printf("Schema: %s", schema.Value.Properties)
79-
// }
80-
81-
f.removePropertiesIfHiddenFromEnv(schema)
82-
f.removeItemsIfHiddenForEnv(schema)
75+
// Remove the schema if it is hidden for the target environment
8376
if extension, ok := schema.Extensions[hiddenEnvsExtension]; ok {
8477
log.Printf("Found x-hidden-envs in schema: K: %q, V: %q", hiddenEnvsExtension, extension)
8578
if isHiddenExtensionEqualToTargetEnv(extension, f.metadata.targetEnv) {
8679
log.Printf("Removing schema: %q because is hidden for target env: %q", name, f.metadata.targetEnv)
8780
delete(schemas, name)
88-
} else {
89-
// Remove the Hidden extension from the final OAS
90-
delete(schema.Extensions, hiddenEnvsExtension)
81+
return nil
9182
}
92-
}
9383

94-
if schema.Value == nil || schema.Value.Extensions == nil {
95-
return nil
84+
// Remove the Hidden extension from the final OAS
85+
delete(schema.Extensions, hiddenEnvsExtension)
9686
}
9787

98-
if extension, ok := schema.Value.Extensions[hiddenEnvsExtension]; ok {
99-
log.Printf("Found x-hidden-envs in schema: K: %q, V: %q", hiddenEnvsExtension, extension)
100-
if isHiddenExtensionEqualToTargetEnv(extension, f.metadata.targetEnv) {
101-
log.Printf("Removing schema: %q because is hidden for target env: %q", name, f.metadata.targetEnv)
102-
delete(schemas, name)
103-
} else {
88+
if schema.Value != nil && schema.Value.Extensions != nil {
89+
if extension, ok := schema.Value.Extensions[hiddenEnvsExtension]; ok {
90+
log.Printf("Found x-hidden-envs in schema: K: %q, V: %q", hiddenEnvsExtension, extension)
91+
if isHiddenExtensionEqualToTargetEnv(extension, f.metadata.targetEnv) {
92+
log.Printf("Removing schema: %q because is hidden for target env: %q", name, f.metadata.targetEnv)
93+
delete(schemas, name)
94+
return nil
95+
}
96+
10497
// Remove the Hidden extension from the final OAS
10598
delete(schema.Value.Extensions, hiddenEnvsExtension)
10699
}
107100
}
101+
102+
// Remove properties and items if they are hidden for the target environment
103+
f.removePropertiesIfHiddenFromEnv(schema)
104+
f.removeItemsIfHiddenForEnv(schema)
105+
108106
return nil
109107
}
110108

tools/cli/internal/openapi/filter/hidden_envs_test.go

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,230 @@ func TestRemoveResponseIfHiddenForEnv(t *testing.T) {
729729
}
730730
}
731731

732+
func TestApplyOnSchemas(t *testing.T) {
733+
tests := []struct {
734+
name string
735+
schemas openapi3.Schemas
736+
targetEnv string
737+
expected openapi3.Schemas
738+
}{
739+
{
740+
name: "Remove schema hidden for target environment",
741+
schemas: openapi3.Schemas{
742+
"schema1": {
743+
Extensions: map[string]any{
744+
hiddenEnvsExtension: map[string]any{
745+
"envs": "prod",
746+
},
747+
},
748+
},
749+
"schema2": {
750+
Extensions: map[string]any{
751+
hiddenEnvsExtension: map[string]any{
752+
"envs": "dev",
753+
},
754+
},
755+
},
756+
},
757+
targetEnv: "prod",
758+
expected: openapi3.Schemas{
759+
"schema2": {
760+
Extensions: map[string]any{},
761+
},
762+
},
763+
},
764+
{
765+
name: "Remove properties hidden for target environment",
766+
schemas: openapi3.Schemas{
767+
"schema1": {
768+
Value: &openapi3.Schema{
769+
Properties: map[string]*openapi3.SchemaRef{
770+
"property1": {
771+
Extensions: map[string]any{
772+
hiddenEnvsExtension: map[string]any{
773+
"envs": "prod",
774+
},
775+
},
776+
},
777+
"property2": {
778+
Extensions: map[string]any{
779+
hiddenEnvsExtension: map[string]any{
780+
"envs": "dev",
781+
},
782+
},
783+
},
784+
},
785+
},
786+
},
787+
},
788+
targetEnv: "prod",
789+
expected: openapi3.Schemas{
790+
"schema1": {
791+
Value: &openapi3.Schema{
792+
Properties: map[string]*openapi3.SchemaRef{
793+
"property2": {
794+
Extensions: map[string]any{},
795+
},
796+
},
797+
},
798+
},
799+
},
800+
},
801+
{
802+
name: "Remove properties of properties hidden for target environment",
803+
schemas: openapi3.Schemas{
804+
"schema1": {
805+
Value: &openapi3.Schema{
806+
Properties: map[string]*openapi3.SchemaRef{
807+
"property1": {
808+
Value: &openapi3.Schema{
809+
Properties: map[string]*openapi3.SchemaRef{
810+
"subProperty1": {
811+
Extensions: map[string]any{
812+
hiddenEnvsExtension: map[string]any{
813+
"envs": "prod",
814+
},
815+
},
816+
},
817+
"subProperty2": {
818+
Extensions: map[string]any{
819+
hiddenEnvsExtension: map[string]any{
820+
"envs": "dev",
821+
},
822+
},
823+
},
824+
},
825+
},
826+
},
827+
},
828+
},
829+
},
830+
},
831+
targetEnv: "prod",
832+
expected: openapi3.Schemas{
833+
"schema1": {
834+
Value: &openapi3.Schema{
835+
Properties: map[string]*openapi3.SchemaRef{
836+
"property1": {
837+
Value: &openapi3.Schema{
838+
Properties: map[string]*openapi3.SchemaRef{
839+
"subProperty2": {
840+
Extensions: map[string]any{},
841+
},
842+
},
843+
},
844+
},
845+
},
846+
},
847+
},
848+
},
849+
},
850+
{
851+
name: "Retain schemas and properties not hidden for target environment",
852+
schemas: openapi3.Schemas{
853+
"schema1": {
854+
Extensions: map[string]any{
855+
hiddenEnvsExtension: map[string]any{
856+
"envs": "dev",
857+
},
858+
},
859+
},
860+
"schema2": {
861+
Value: &openapi3.Schema{
862+
Properties: map[string]*openapi3.SchemaRef{
863+
"property1": {
864+
Extensions: map[string]any{
865+
hiddenEnvsExtension: map[string]any{
866+
"envs": "dev",
867+
},
868+
},
869+
},
870+
"property2": {
871+
Extensions: map[string]any{},
872+
},
873+
},
874+
},
875+
},
876+
},
877+
targetEnv: "prod",
878+
expected: openapi3.Schemas{
879+
"schema1": {
880+
Extensions: map[string]any{},
881+
},
882+
"schema2": {
883+
Value: &openapi3.Schema{
884+
Properties: map[string]*openapi3.SchemaRef{
885+
"property1": {
886+
Extensions: map[string]any{},
887+
},
888+
"property2": {
889+
Extensions: map[string]any{},
890+
},
891+
},
892+
},
893+
},
894+
},
895+
},
896+
{
897+
name: "Remove hidden environment extension from final OAS",
898+
schemas: openapi3.Schemas{
899+
"schema1": {
900+
Extensions: map[string]any{
901+
hiddenEnvsExtension: map[string]any{
902+
"envs": "prod",
903+
},
904+
},
905+
},
906+
"schema2": {
907+
Value: &openapi3.Schema{
908+
Properties: map[string]*openapi3.SchemaRef{
909+
"property1": {
910+
Extensions: map[string]any{
911+
hiddenEnvsExtension: map[string]any{
912+
"envs": "prod",
913+
},
914+
},
915+
},
916+
"property2": {
917+
Extensions: map[string]any{},
918+
},
919+
},
920+
},
921+
},
922+
},
923+
targetEnv: "dev",
924+
expected: openapi3.Schemas{
925+
"schema1": {
926+
Extensions: map[string]any{},
927+
},
928+
"schema2": {
929+
Value: &openapi3.Schema{
930+
Properties: map[string]*openapi3.SchemaRef{
931+
"property1": {
932+
Extensions: map[string]any{},
933+
},
934+
"property2": {
935+
Extensions: map[string]any{},
936+
},
937+
},
938+
},
939+
},
940+
},
941+
},
942+
}
943+
944+
for _, tt := range tests {
945+
t.Run(tt.name, func(t *testing.T) {
946+
filter := HiddenEnvsFilter{
947+
metadata: &Metadata{targetEnv: tt.targetEnv},
948+
}
949+
err := filter.applyOnSchemas(tt.schemas)
950+
require.NoError(t, err)
951+
assert.Equal(t, tt.expected, tt.schemas)
952+
})
953+
}
954+
}
955+
732956
func TestApply(t *testing.T) {
733957
metadata := &Metadata{
734958
targetEnv: "prod",

0 commit comments

Comments
 (0)