From 6db7f38a713db11d669bd0be9d4ea8cd91142f65 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Mon, 19 Jun 2023 13:32:44 +0300 Subject: [PATCH] openapi3filter: simplify validated test --- openapi3filter/validation_test.go | 36 +++++++++++++------------------ 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/openapi3filter/validation_test.go b/openapi3filter/validation_test.go index 311556036..e924e8b4d 100644 --- a/openapi3filter/validation_test.go +++ b/openapi3filter/validation_test.go @@ -497,26 +497,26 @@ func TestRootSecurityRequirementsAreUsedIfNotProvidedAtTheOperationLevel(t *test tc := []struct { name string schemes *[]ExampleSecurityScheme - expectedSchemes *[]ExampleSecurityScheme + expectedSchemes []ExampleSecurityScheme }{ { name: "/inherited-security", schemes: nil, - expectedSchemes: &[]ExampleSecurityScheme{ + expectedSchemes: []ExampleSecurityScheme{ securitySchemes[1], }, }, { name: "/overwrite-without-security", schemes: &[]ExampleSecurityScheme{}, - expectedSchemes: &[]ExampleSecurityScheme{}, + expectedSchemes: []ExampleSecurityScheme{}, }, { name: "/overwrite-with-security", schemes: &[]ExampleSecurityScheme{ securitySchemes[0], }, - expectedSchemes: &[]ExampleSecurityScheme{ + expectedSchemes: []ExampleSecurityScheme{ securitySchemes[0], }, }, @@ -548,7 +548,7 @@ func TestRootSecurityRequirementsAreUsedIfNotProvidedAtTheOperationLevel(t *test // Add the paths from the test cases to the spec's paths for _, tc := range tc { - var securityRequirements *openapi3.SecurityRequirements = nil + var securityRequirements *openapi3.SecurityRequirements if tc.schemes != nil { tempS := openapi3.NewSecurityRequirements() for _, scheme := range *tc.schemes { @@ -572,13 +572,9 @@ func TestRootSecurityRequirementsAreUsedIfNotProvidedAtTheOperationLevel(t *test // Test each case for _, path := range tc { // Make a map of the schemes and whether they're - var schemesValidated *map[*openapi3.SecurityScheme]bool = nil - if path.expectedSchemes != nil { - temp := make(map[*openapi3.SecurityScheme]bool) - schemesValidated = &temp - for _, scheme := range *path.expectedSchemes { - (*schemesValidated)[scheme.Scheme] = false - } + schemesValidated := make(map[*openapi3.SecurityScheme]bool) + for _, scheme := range path.expectedSchemes { + schemesValidated[scheme.Scheme] = false } // Create the request @@ -591,15 +587,13 @@ func TestRootSecurityRequirementsAreUsedIfNotProvidedAtTheOperationLevel(t *test Route: route, Options: &Options{ AuthenticationFunc: func(ctx context.Context, input *AuthenticationInput) error { - if schemesValidated != nil { - if validated, ok := (*schemesValidated)[input.SecurityScheme]; ok { - if validated { - t.Fatalf("The path %q had the schemes %v named %q validated more than once", - path.name, input.SecurityScheme, input.SecuritySchemeName) - } - (*schemesValidated)[input.SecurityScheme] = true - return nil + if validated, ok := schemesValidated[input.SecurityScheme]; ok { + if validated { + t.Fatalf("The path %q had the schemes %v named %q validated more than once", + path.name, input.SecurityScheme, input.SecuritySchemeName) } + schemesValidated[input.SecurityScheme] = true + return nil } t.Fatalf("The path %q had the schemes %v named %q", @@ -614,7 +608,7 @@ func TestRootSecurityRequirementsAreUsedIfNotProvidedAtTheOperationLevel(t *test err = ValidateRequest(context.Background(), &req) require.NoError(t, err) - for securityRequirement, validated := range *schemesValidated { + for securityRequirement, validated := range schemesValidated { if !validated { t.Fatalf("The security requirement %v was unexpected to be validated but wasn't", securityRequirement)